On 14.5.2011. 14:11, Oliver Salzburg wrote:
I'm currently in the process of moving from our own proprietary logging
solution to log4php in one of our projects. Our own solution has a
helper method which I appended to this mail. The purpose of the method
is to write the contents of a variable (and any of it's members
recursively) to the log.
The equivalent place for this method in log4php would be the Logger
class. But I wonder what the proper way would be to integrate the
functionality.

One of the built-in features in log4php is that if you log variable which does not contain a string, such as an array or object, then it is equivalent to logging var_export($var, true). The whole variable will recursively be logged.

For example, this line:
$log->info(array(1, 2, 3));

Will, given default configuration, produce the following output :
05/15/11 11:57:14,548 [2404] INFO root - array (
  0 => 1,
  1 => 2,
  2 => 3,
)

Is this enough for to cover your needs?

I can see two main differences between this solution and your dump() method. One is that the output formatting is different, and you can specify the indent.

The other is that dump() logs the variable dump line-by-line instead on all at once. In other words, the output would be more similar to:
05/15/11 11:57:14,548 [2404] INFO root - array (
05/15/11 11:57:14,548 [2404] INFO root -   0 => 1,
05/15/11 11:57:14,548 [2404] INFO root -   1 => 2,
05/15/11 11:57:14,548 [2404] INFO root -   2 => 3,
05/15/11 11:57:14,548 [2404] INFO root - )

The question is how much you really need these two features.

Should I just derive from Logger and extend it? Or is there a way to
"plug in" this functionality.

Unfortunatelly, extending the Logger class will not help you much in this case. Even if you extend it (e.g. you create a MyLogger class), MyLogger::getLogger() method will still return an instance of Logger, and not MyLogger class.

Regards,
Ivan

Reply via email to