Logging Interfaces of PHP Frameworks

2014-11-13 12:21:55 -0500

Conclusion First

The frameworks / libs described in this article are Zend Framework, Monolog, Apache's log4php, Slim and Yii

They all support the major logging concepts: Level, Writer and Formatter

Level

All of them support levels defined in http://tools.ietf.org/html/rfc5424 fully or partly

In RFC 5424 we have eight levels: debug, info, notice, warning, error, critical, alert, emergency

Zend Framework supports custom levels. But IMO custom levels are evil as they create undocumented things. Nobody knows what is "FOO"

Writer (Yii calls it Routing)

Write tells the logger where the logs should be written to: Local files, syslog, database(Like MySQL), stdout, browse console, NewRelic

Monolog(used by Laravel) seems has the best coverage of writers.

Formatter (Yii has this thing Context which is similar to Formatter)

If you want to add the pid as part of the log line, or if you want to write the log as JSON, formatter is your friend

Interfaces and Examples

Zend Framework

http://framework.zend.com/manual/1.12/en/zend.log.html

$logger = new Zend_Log();

$writer = new Zend_Log_Writer_Stream('php://output');
//Or
//$writer = new Zend_Log_Writer_Stream('/path/to/logfile');
//Or
//$db = Zend_Db::factory('PDO_MYSQL', $params)
//$columnMapping = array('lvl' => 'priority', 'msg' => 'message'); 
//$writer = new Zend_Log_Writer_Db($db, 'log_table_name', $columnMapping)
//Or 
//$writer = new Zend_Log_Writer_Firebug();
//Or
//$writer = new Zend_Log_Writer_Syslog(array('application' => 'FooBar'));

$logger->addWriter($writer);
$logger->log('Informational message', Zend_Log::INFO);
$logger->log('Informational message', Zend_Log::INFO);
$logger->info('Informational message');
$logger->log('Emergency message', Zend_Log::EMERG);
$logger->emerg('Emergency message');

//By default log line includes timestamp, message, priority, and priorityName. If you want to add more information in the log line:
$logger->setEventItem('pid', getmypid());

//You can also customize the log line by setting formatter to the writter
$format = '%timestamp% %priorityName% (%priority%): %message%' . PHP_EOL;
$formatter = new Zend_Log_Formatter_Simple($format);
$writer->setFormatter($formatter);

Laravel

http://laravel.com/docs/4.2/errors

//By default it writes to app/storage/logs/laravel.log
Log::info('This is some useful information.');
Log::warning('Something could be going wrong.');
Log::error('Something is really going wrong.');

//Laravel is using https://github.com/Seldaek/monolog as its underlying logging lib. So you can get the power of monolog by accessing it directly:
$log = Log::getMonolog();

// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));

// add records to the log
$log->addWarning('Foo');
$log->addError('Bar');

Log4php

http://logging.apache.org/log4php/

// Insert the path where you unpacked log4php
include('log4php/Logger.php');

// Tell log4php to use our configuration file.
Logger::configure('config.xml');

// Fetch a logger, it will inherit settings from the root logger
$log = Logger::getLogger('myLogger');

// Start logging
$log->trace("My first message.");   // Not logged because TRACE < WARN
$log->debug("My second message.");  // Not logged because DEBUG < WARN
$log->info("My third message.");    // Not logged because INFO < WARN
$log->warn("My fourth message.");   // Logged because WARN >= WARN
$log->error("My fifth message.");   // Logged because ERROR >= WARN
$log->fatal("My sixth message.");   // Logged because FATAL >= WARN

Yii

http://www.yiiframework.com/doc/guide/1.1/en/topics.logging

Yii::log($message, $level, $category);

Slim

http://docs.slimframework.com/#Logging-Overview

//Enable logging
$app->log->setEnabled(true);
$app->log->setLevel(\Slim\Log::WARN);

$app->log->debug(mixed $object);
$app->log->info(mixed $object);
$app->log->notice(mixed $object);
$app->log->warning(mixed $object);
$app->log->error(mixed $object);
$app->log->critical(mixed $object);
$app->log->alert(mixed $object);
$app->log->emergency(mixed $object);
«Newer      Older»
Comment:
Name:

Back to home

Subscribe | Register | Login | N