Dennis
Dennis

Reputation: 8111

Static Logger in Zend Framework 2?

Laravel has a nice static logger interface that I would like to use (http://four.laravel.com/docs/errors#logging)

I want one just like that, but to use from the confines of Zend Framework 2. Most specifically, I want all logging in source code anywhere to simply be:

Log::alert('string'); 
Log::debug('string');

Is there an existing package? Can I write/extend my own? Can I pull that codebase from Laravel?

Upvotes: 2

Views: 1748

Answers (3)

Dennis
Dennis

Reputation: 8111

Class:

class Log
{
    public static function __callStatic($method, $args)
    {
        $logger = new \Zend\Log\Logger();                           
        $writer = new \Zend\Log\Writer\Stream('data/logfile.txt');  
        $logger->addWriter($writer); 

        return $logger->$method($args[0]);
    }
};

To call:

Log::info("my Zendastatic Log Message");

Sources:

Upvotes: 4

Pradeep
Pradeep

Reputation: 2497

You can extend the Zend\Log\Logger with your own class and decorate it (like the same way). You can even put that into your Service Manager's services and get it.

$myLogger = new MyNamespace\MyLogger();
$myLogger->info('Informational message');

class MyLogger extends Zend\Log\Logger
{
  public function __construct() 
  {
    $writer = new Zend\Log\Writer\Stream('data/logfile');  //define log file placement
    $this->addWriter($writer);             //add the writer
  }
}

Upvotes: 2

Remi Thomas
Remi Thomas

Reputation: 1528

You mean like this ?

 \Zend\Debug\Debug::dump('string');

Or this http://framework.zend.com/manual/2.0/en/modules/zend.log.overview.html

Modules

Upvotes: 0

Related Questions