error_log all the things! (in Prestashop)

William Edwards Deming once said: "You can’t manage what you can’t measure.“

I find this statement pretty similar to: "If I can’t var_dump I can’t fix”.

Actually this statement is just a bit stronger than Deming’s one but I find it true in most of the cases.

It doesn’t matter if code is properly written and TDD and/or BDD are actually used. When a program begins to work unexpectedly, one of the things that is usually done to shed some lights on the problem is to put some “log” directives here and there and look at the results.

This kind of debugging strategy, while being rather primitive, can be very useful in many contexts.

For example if you’re developing something with PrestaShop (PS) (so you’re writing PHP…) you may feel the need to log something inside the apache error log.

This can be easily accomplished by using the PHP function error_log.

But this function  accepts only strings, so in order to log a complex structure like an hash you should first convert it to a string using something like var_dump or print_r.

Sincerely I was expecting some kind of support from PS core functionality but unfortunately this wasn’t the case.

As a result I end up writing a bunch of lines of code to log whatever I need right inside the apache error log.

Here they are:


class Tools extends ToolsCore
{
  public static function console_log()
  {
    $args = func_get_args();
    $log = '';
    foreach ($args as $arg) {
      $log .= print_r($arg, true);
    }
    error_log($log);
  }
}

I simply added a public static function to the Tools class (by properly overriding it) that takes any number of arguments and builds a string by using the print_r function.

The string is eventually logged on the apache error log.

I hope this can be useful to the ones willing to go with the old style debugging 😛

Cheers!

Leave a Reply

wpDiscuz