<?php error_reporting(E_ALL | E_STRICT); require_once '../anewt.lib.php'; anewt_include('logging'); AnewtLog::init(false); AnewtLog::add_handler(new AnewtLogHandlerDefault()); AnewtLog::add_handler(new AnewtLogHandlerFile('test.log')); AnewtLog::add_handler(new AnewtLogHandlerFile('test-debug.log'), ANEWT_LOG_LEVEL_DEBUG); AnewtLog::set_domain('a'); AnewtLog::error('An error occured.'); AnewtLog::set_domain('b'); AnewtLog::error('Error number %d', 3); AnewtLog::reset_domain(); AnewtLog::debug('Debugging message: %d: %s', 4, 'dbg'); AnewtLog::set_domain('c'); AnewtLog::warning('This is a warning message without arguments'); AnewtLog::reset_domain(); AnewtLog::warning('This is a warning message: %d: %s', 2, 'test1'); AnewtLog::reset_domain(); AnewtLog::warning('This is a warning message: %d: %s', array(2, 'test2')); AnewtLog::warning('This is warning with format characters but no values, %s %s %s');
/** * Log a message to the output handlers. * * \param $level * The message log level. This should be one of the ANEWT_LOG_LEVEL_* * constants. * * \param $message * The message itself. sprintf-style placeholders can be specified. * * \param $args * An array with data (optional). These values will be substituted for the * placeholders in $message. */ private static function _log($level, $message, $args = null) { if (!AnewtLog::_is_initialized()) { AnewtLog::init(); } global $anewt_logging_handlers; if (is_null($args)) { $args = array(); } assert('is_int($level) && ($level >= 0) && ($level <= 5)'); assert('is_string($message)'); assert('is_array($args)'); /* Get the variable arguments list */ while (count($args) == 1 && is_array($args[0])) { $args = $args[0]; } if ($args) { $message = vsprintf($message, $args); } $domain = AnewtLog::get_domain(); foreach ($anewt_logging_handlers as $handler) { if ($handler->level >= $level) { $handler->log($domain, $level, $message); } } }