Sunday, October 3, 2010

Cannot write output to stream ... error on Zend_Log logger

I got a "Cannot write output to stream ..." when using Zend_Log and Zend_Log_Writer_Stream.

I have been frustrated by this problem for several long hours, and the cause is because I created a new logger instance in a local variable inside a method of a lot objects. If that sounds confusing, this is the example problem:

class Car {   public function drive() {     $logger = Zend_Log::factory(/*some config*/);     ...   } }
To solve the problem, I had to do this:

class Car {   protected static $_logger;            protected static function getLogger() {     if (!self::$_logger)       self::$_logger = Zend_Log::factory(/*some config*/);     return self::$_logger;   }   public function drive() {     $logger = self::getLogger();     ...   } }

Annoying, but that's the workaround that works. If I have the choice I definitely prefer SLF4J.

No comments:

Post a Comment