On 2011-05-15 12:17, Ivan Habunek wrote:
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 actually wasn't aware of that when I wrote my initial mail.
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.
That was actually the main reason why our old logging framework had
that method (to add a timestamp to each line of the output).
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
Well, I think I will just accept the fact that this is how things are :)
Especially since I just converted our frontend code to log4net, which
behaves the same way.
Thank you for your help.
--
Oliver Salzburg – senior software developer
Mad Hat GmbH, Ludwigstraße 182a, 63067 Offenbach, Germany
»Tel.: +49 (0)69 829 003 26 »Fax: +49 (0)69 829 003 23
»mobile: +49 (0)151 195 656 26
»oli...@madhat.de »www.madhat.de
Geschäftsführer: Thomas Lutz, Gernot Pflüger
Amtsgericht Offenbach – HRB: 12485