/** * Log a message. * * \param $domain * \param $level * \param $message * * \see AnewtLogHandlerBase::log */ public function log($domain, $level, $message) { $name = AnewtLog::loglevel_to_string($level); $output = $this->format_log_message($domain, $level, $message); /* Optionally prefix with timestamp */ if ($this->timestamps) { $date = AnewtDateTime::iso8601(AnewtDateTime::now()); $output = sprintf('[%s] %s', $date, $output); } /* Make sure there is a trailing newline */ if (!str_has_prefix($output, NL)) { $output .= NL; } fwrite($this->fp, $output); }
<?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 debug message with a custom domain. * * \param $domain * \param $message * \param $args * \see AnewtLog::error_with_domain */ static function debug_with_domain($domain, $message, $args = null) { $args = func_get_args(); $domain = array_shift($args); $message = array_shift($args); AnewtLog::_log_with_domain($domain, ANEWT_LOG_LEVEL_DEBUG, $message, $args); }