Source for file Log.php
Documentation is available at Log.php
* "THE BEER-WARE LICENSE" (Revision 42):
* "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
* As long as you retain this notice you can do whatever you want with
* this stuff. If we meet some day, and you think this stuff is worth it,
* you can buy me a beer in return.
* @copyright Copyright (c) 2009 Sven Strittmatter
* This pure static class provides logging facility.
* First you need to specify a global log level. After that you need
* to add minimum one log backend object (implementation of MediaSearch_Log_Interface).
* Then you can use the logger.
* MediaSearch_Log::setLevel(MediaSearch_Log::ALL);
* // shows only info logs on cli
* MediaSearch_Log::MediaSearch_Log_Cli(MediaSearch_Log::INFO);
* // logs everything to the log file
* MediaSearch_Log::MediaSearch_Log_Cli('path/to/log', MediaSearch_Log_File::ALL);
* MediaSearch_Log::info('log info message');
* MediaSearch_Log::error('log an error');
* MediaSearch_Log::Debug('log debug message', array($debugVar1);
* MediaSearch_Log::Debug('log debug message', array($debugVar1, $debugVar2);
* @copyright Copyright (c) 2009 Sven Strittmatter
* Enables loglevel trace.
* Enables loglevel debug.
* Enables loglevel error.
* Enables loglevel fatal.
* Enables all loglevels together.
* Some bitwise combination of the loglevel constants.
private static $logLevel = self::DISABLED;
* Holds logging backend classes.
* These are implemenations of MediaSearch_Log_Interface.
private static $logger = array();
* This clas is pure static.
private function __construct() {}
* Determines if the given loglevel is enabled in the global
public static function isEnabled($logLevel) {
return (self::$logLevel & $logLevel) === $logLevel;
* Applys the logging methods (info(), trace(), debug() etc.)
* on the registered log backends (implementations of MediaSearch_Log_Interface).
* @param mixed $additional int or array
private static function applyOnLogger($method, $message, $additional = null) {
if (empty(self::$logger)) {
foreach (self::$logger as $logger) {
if ('trace' === $method || 'debug' === $method) {
$logger->$method($message, $additional);
$logger->$method($message);
* Sets the global log level. Use anny bitewise combination
* of the class constants.
* MediaSearch_Log::setLevel(MediaSearch_Log::DISABLED);
* MediaSearch_Log::setLevel(MediaSearch_Log::ALL);
* MediaSearch_Log::setLevel(MediaSearch_Log::INFO & MediaSearch_Log::DEBUG);
* @param int $integer Defauklt is MediaSearch_Log::DISABLED
public static function setLevel($integer = self::DISABLED) {
self::$logLevel = $integer;
* Returns the global log level.
* Adds log backends. You can add as man as you want.
* Each logger obejct can have its own log level. So you can
* log e.g. all info() and trace() messages to file and
* all fatal() to en email implemtation.
* @param MediaSearch_Log_Interface $logger
public static function add(MediaSearch_Log_Interface $logger) {
if (null === $logger->getLevel()) {
$logger->setLevel(self::getLevel());
self::$logger[] = $logger;
* Call this method to log a info message.
public static function info($message) {
if (!self::isEnabled(self::INFO)) {
self::applyOnLogger('info', $message);
* Call this method to log a trace message.
* You can specify a trace level. You can grep for this
* like: grep "TRACE-11' if your tracelevel is 11.
public static function trace($message, $traceLevel = 0) {
if (!self::isEnabled(self::TRACE)) {
self::applyOnLogger('trace', $message, $traceLevel);
* Call this method to log a debug message.
* Optional passed variables are dumped in the logfile.
* @param array $variables Additional variabls to debug
public static function debug($message, array $variables = null) {
if (!self::isEnabled(self::DEBUG)) {
self::applyOnLogger('debug', $message, $variables);
* Call this method to log a warn message.
public static function warn($message) {
if (!self::isEnabled(self::WARN)) {
self::applyOnLogger('warn', $message);
* Call this method to log a eroor message.
public static function error($message) {
if (!self::isEnabled(self::ERROR)) {
self::applyOnLogger('error', $message);
* Call this method to log a fatal message.
public static function fatal($message) {
if (!self::isEnabled(self::FATAL)) {
self::applyOnLogger('fatal', $message);
|