MediaSearch
[ class tree: MediaSearch ] [ index: MediaSearch ] [ all elements ]

Source for file Log.php

Documentation is available at Log.php

  1. <?php
  2. /**
  3.  * MediaSearch
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author$
  14.  * $Revision$
  15.  *
  16.  * @category    MediaSearch
  17.  * @package     MediaSearch
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. /**
  22.  * This pure static class provides logging facility.
  23.  *
  24.  * First you need to specify a global log level. After that you need
  25.  * to add minimum one log backend object (implementation of MediaSearch_Log_Interface).
  26.  * Then you can use the logger.
  27.  *
  28.  * Example
  29.  * <code>
  30.  * MediaSearch_Log::setLevel(MediaSearch_Log::ALL);
  31.  * // shows only info logs on cli
  32.  * MediaSearch_Log::MediaSearch_Log_Cli(MediaSearch_Log::INFO);
  33.  * // logs everything to the log file
  34.  * MediaSearch_Log::MediaSearch_Log_Cli('path/to/log', MediaSearch_Log_File::ALL);
  35.  * // ..
  36.  * MediaSearch_Log::info('log info message');
  37.  * // ..
  38.  * MediaSearch_Log::error('log an error');
  39.  * // ..
  40.  * MediaSearch_Log::Debug('log debug message', array($debugVar1);
  41.  * MediaSearch_Log::Debug('log debug message', array($debugVar1, $debugVar2);
  42.  * </code>
  43.  *
  44.  * @final
  45.  * @category   MediaSearch
  46.  * @package    MediaSearch
  47.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  48.  */
  49. final class MediaSearch_Log {
  50.     const TRACE_LELVEL 57;
  51.  
  52.     /**
  53.      * Disable all logging.
  54.      */
  55.     const DISABLED  0;
  56.     /**
  57.      * Enables loglevel info.
  58.      */
  59.     const INFO      1;
  60.     /**
  61.      * Enables loglevel trace.
  62.      */
  63.     const TRACE     2;
  64.     /**
  65.      * Enables loglevel debug.
  66.      */
  67.     const DEBUG     4;
  68.     /**
  69.      * Enables loglevel warn.
  70.      */
  71.     const WARN      8;
  72.     /**
  73.      * Enables loglevel error.
  74.      */
  75.     const ERROR     16;
  76.     /**
  77.      * Enables loglevel fatal.
  78.      */
  79.     const FATAL     32;
  80.     /**
  81.      * Enables all loglevels together.
  82.      */
  83.     const ALL       63;
  84.  
  85.     /**
  86.      * Global log level.
  87.      * Some bitwise combination of the loglevel constants.
  88.      *
  89.      * @static
  90.      * @access private
  91.      * @var int 
  92.      */
  93.     private static $logLevel self::DISABLED;
  94.     /**
  95.      * Holds logging backend classes.
  96.      * These are implemenations of MediaSearch_Log_Interface.
  97.      *
  98.      * @static
  99.      * @access private
  100.      * @var array 
  101.      */
  102.     private static $logger array();
  103.  
  104.     /**
  105.      * This clas is pure static.
  106.      *
  107.      * @access private
  108.      */
  109.     private function __construct({}
  110.  
  111.     /**
  112.      * Determines if the given loglevel is enabled in the global
  113.      * loglevel.
  114.      *
  115.      * @static
  116.      * @access public
  117.      * @param int $logLevel 
  118.      * @return bool 
  119.      */
  120.     public static function isEnabled($logLevel{
  121.         return (self::$logLevel $logLevel=== $logLevel;
  122.     }
  123.  
  124.     /**
  125.      * Applys the logging methods (info(), trace(), debug() etc.)
  126.      * on the registered log backends (implementations of MediaSearch_Log_Interface).
  127.      *
  128.      * @static
  129.      * @access private
  130.      * @param string $method 
  131.      * @param string $message 
  132.      * @param mixed $additional int or array
  133.      */
  134.     private static function applyOnLogger($method$message$additional null{
  135.         if (empty(self::$logger)) {
  136.             return;
  137.         }
  138.  
  139.         foreach (self::$logger as $logger{
  140.             if ('trace' === $method || 'debug' === $method{
  141.                 $logger->$method($message$additional);
  142.             else {
  143.                 $logger->$method($message);
  144.             }
  145.         }
  146.     }
  147.  
  148.     /**
  149.      * Sets the global log level. Use anny bitewise combination
  150.      * of the class constants.
  151.      *
  152.      * Examples:
  153.      * <code>
  154.      * MediaSearch_Log::setLevel(MediaSearch_Log::DISABLED);
  155.      * MediaSearch_Log::setLevel(MediaSearch_Log::ALL);
  156.      * MediaSearch_Log::setLevel(MediaSearch_Log::INFO & MediaSearch_Log::DEBUG);
  157.      * // ...
  158.      * </code>
  159.      *
  160.      * @static
  161.      * @access public
  162.      * @param int $integer Defauklt is MediaSearch_Log::DISABLED
  163.      */
  164.     public static function setLevel($integer self::DISABLED{
  165.         self::$logLevel $integer;
  166.     }
  167.  
  168.     /**
  169.      * Returns the global log level.
  170.      *
  171.      * @static
  172.      * @access public
  173.      * @return int 
  174.      */
  175.     public static function getLevel({
  176.         return self::$logLevel;
  177.     }
  178.  
  179.     /**
  180.      * Adds log backends. You can add as man as you want.
  181.      * Each logger obejct can have its own log level. So you can
  182.      * log e.g. all info() and trace() messages to file and
  183.      * all fatal() to en email implemtation.
  184.      *
  185.      * @static
  186.      * @access public
  187.      * @param MediaSearch_Log_Interface $logger 
  188.      */
  189.     public static function add(MediaSearch_Log_Interface $logger{
  190.         if (null === $logger->getLevel()) {
  191.             $logger->setLevel(self::getLevel());
  192.         }
  193.  
  194.         self::$logger[$logger;
  195.     }
  196.  
  197.     /**
  198.      * Call this method to log a info message.
  199.      *
  200.      * @static
  201.      * @access public
  202.      * @param string $message 
  203.      */
  204.     public static function info($message{
  205.         if (!self::isEnabled(self::INFO)) {
  206.             return;
  207.         }
  208.  
  209.         self::applyOnLogger('info'$message);
  210.     }
  211.  
  212.     /**
  213.      * Call this method to log a trace message.
  214.      * You can specify a trace level. You can grep for this
  215.      * like: grep "TRACE-11' if your tracelevel is 11.
  216.      * 
  217.      * @static
  218.      * @access public
  219.      * @param string $message 
  220.      * @param int $traceLevel 
  221.      */
  222.     public static function trace($message$traceLevel 0{
  223.         if (!self::isEnabled(self::TRACE)) {
  224.             return;
  225.         }
  226.  
  227.         self::applyOnLogger('trace'$message$traceLevel);
  228.     }
  229.  
  230.     /**
  231.      * Call this method to log a debug message.
  232.      * Optional passed variables are dumped in the logfile.
  233.      *
  234.      * @static
  235.      * @access public
  236.      * @param string $message 
  237.      * @param array $variables Additional variabls to debug
  238.      */
  239.     public static function debug($messagearray $variables null{
  240.         if (!self::isEnabled(self::DEBUG)) {
  241.             return;
  242.         }
  243.  
  244.         self::applyOnLogger('debug'$message$variables);
  245.     }
  246.  
  247.     /**
  248.      * Call this method to log a warn message.
  249.      *
  250.      * @static
  251.      * @access public
  252.      * @param string $message 
  253.      */
  254.     public static function warn($message{
  255.         if (!self::isEnabled(self::WARN)) {
  256.             return;
  257.         }
  258.  
  259.         self::applyOnLogger('warn'$message);
  260.     }
  261.  
  262.     /**
  263.      * Call this method to log a eroor message.
  264.      *
  265.      * @static
  266.      * @access public
  267.      * @param string $message 
  268.      */
  269.     public static function error($message{
  270.         if (!self::isEnabled(self::ERROR)) {
  271.             return;
  272.         }
  273.  
  274.         self::applyOnLogger('error'$message);
  275.     }
  276.  
  277.     /**
  278.      * Call this method to log a fatal message.
  279.      *
  280.      * @static
  281.      * @access public
  282.      * @param string $message 
  283.      */
  284.     public static function fatal($message{
  285.         if (!self::isEnabled(self::FATAL)) {
  286.             return;
  287.         }
  288.  
  289.         self::applyOnLogger('fatal'$message);
  290.     }
  291. }

Documentation generated on Mon, 17 Aug 2009 14:53:36 +0200 by phpDocumentor 1.4.2