PHP Classes
elePHPant
Icontem

File: library/EasyLogger.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Nikola Posa  >  EasyLogger  >  library/EasyLogger.php  >  Download  
File: library/EasyLogger.php
Role: Class source
Content type: text/plain
Description: Logger class
Class: EasyLogger
Log events to files
Author: By
Last change:
Date: 2 years ago
Size: 4,063 bytes
 

Contents

Class file image Download
<?php
/**
 * EasyLogger
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 */

require_once 'EasyLogger/Handler/HandlerInterface.php';

/**
 * Exposes methods for logging events based on their level
 * of importance.
 *
 * Contains a stack of log handlers used for storing records
 * themselves.
 *
 * @author Nikola Posa <posa.nikola@gmail.com>
 */
class EasyLogger
{
    const
LEVEL_DEBUG = 10;
    const
LEVEL_INFO = 20;
    const
LEVEL_NOTICE = 30;
    const
LEVEL_WARNING = 40;
    const
LEVEL_ERROR = 50;
    const
LEVEL_CRITICAL = 60;
    const
LEVEL_ALERT = 70;
    const
LEVEL_EMERGENCY = 80;

   
/**
     * Log record handlers.
     *
     * @var array
     */
   
private $_handlers = array();
   
   
/**
     * Supported log levels.
     *
     * @var array
     */
   
private static $_logLevels = array(
       
self::LEVEL_DEBUG=>'debug',
       
self::LEVEL_INFO=>'info',
       
self::LEVEL_NOTICE=>'notice',
       
self::LEVEL_WARNING=>'warning',
       
self::LEVEL_ERROR=>'error',
       
self::LEVEL_CRITICAL=>'critical',
       
self::LEVEL_ALERT=>'alert',
       
self::LEVEL_EMERGENCY=>'emergency'
   
);
   
   
/**
     * Constructs logger instance.
     *
     * @param type $handlers
     */
   
public function __construct($handlers = null)
    {
        if (!empty(
$handlers)) {
            if (!
is_array($handlers)) {
               
$handlers = array($handlers);
            }
           
$this->addHandlers($handlers);
        }
    }
   
   
/**
     * @param EasyLogger_Handler_HandlerInterface $handler
     * @return \EasyLogger
     */
   
public function addHandler(EasyLogger_Handler_HandlerInterface $handler)
    {
       
$this->_handlers[] = $handler;
       
        return
$this;
    }
   
   
/**
     * @param array $handlers
     * @return \EasyLogger
     */
   
public function addHandlers(array $handlers)
    {
        foreach (
$handlers as $handler) {
           
$this->addHandler($handler);
        }
       
        return
$this;
    }
   
    public static function
getLevelName($level)
    {
        if (!
array_key_exists($level, self::$_logLevels)) {
            throw new
InvalidArgumentException("Unsupported log level: $level.");
        }
       
        return
self::$_logLevels[$level];
    }

    private function
_buildRecord($level, $message)
    {
        return array(
           
'timestamp' => time(),
           
'message' => $message,
           
'level' => $level,
           
'levelName' => self::$_logLevels[$level]
        );
    }
   
   
/**
     * Logs message with an arbitrary level.
     *
     * @param string $level
     * @param string $message
     * @return void
     */
   
public function log($level, $message)
    {
        if (empty(
$this->_handlers)) {
            throw new
LogicException('Handlers stack is empty, thus logging will not be performed.');
        }
       
       
$level = (int)$level;
       
        if (!
array_key_exists($level, self::$_logLevels)) {
            throw new
InvalidArgumentException("Unsupported log level: $level.");
        }
       
       
$record = $this->_buildRecord($level, $message);
       
        foreach (
$this->_handlers as $handler) {
           
$handler->handle($record);
        }
    }
   
   
/**
     * Provides shortcut notation when using logger, for example:
     * $logger->info(), $logger->notice(), etc.
     *
     * @param string $name
     * @param array $arguments
     * @return bool
     */
   
public function __call($name, $arguments)
    {
       
$message = array_shift($arguments);
        if (empty(
$message)) {
            throw new
LogicException("Log message must be supplied.");
        }
       
       
$level = array_search($name, self::$_logLevels);
       
        return
$this->log($level, $message);
    }
}