0

I am using log4php and within my objects, I would like to log the object variables upon entry and exit of a method (for debug purposes). I came across log4php's renderer functionality and thought it would be a great idea but I am unable to get it to work.

public function someMethod() {
   $this->logger->debug($this); //entry log
   ... do something ...
   $this->logger->debug($this); //exit log
}

The application throws an error stating that log4php cannot convert the object to a string. Should I be doing something else in order for the renderer to work?

A point to note - the object does have several private and protected variable - no public variables. Perhaps this is an issue - but then I didnt note that statement anywhere in the docs.

Thanks in advance

ChicagoSky
  • 1,290
  • 3
  • 22
  • 50
  • What is your custom renderer you are using? Without one, and especially with an object with private or protected variables, Log4PHP cannot render them with the default renderer. – Sven Sep 04 '14 at 23:02
  • i didnt know that we needed to code a custom renderer - the docs state that default renderer that comes with Log4PHP should be enough to log any object but when I try - nothing at all gets logged. – ChicagoSky Sep 05 '14 at 05:30
  • Theoretically this is correct, the object indeed gets logged. Does your object reveal its contents when using `return print_r($input, true);` on it as the rendering? Because that's what the default renderer is using (see https://github.com/apache/logging-log4php/blob/2.3.0/src/main/php/renderers/LoggerRendererDefault.php ) – Sven Sep 06 '14 at 00:56

1 Answers1

0

Unless you extending or inheriting from it, private shouldn't be a problem.

And unless you're directly accessing a protected function inside the class it resides, that can't be either.

In any case, I'm guessing that you're trying to print something out. That error happens when you're trying to echo out an object(PDO thought me that one).

not being able to comment kinda of sucks

Andrei P.
  • 302
  • 3
  • 9