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

Source for file Log.php

Documentation is available at Log.php

  1. <?php
  2. /**
  3.  * Zend Framework
  4.  *
  5.  * LICENSE
  6.  *
  7.  * This source file is subject to the new BSD license that is bundled
  8.  * with this package in the file LICENSE.txt.
  9.  * It is also available through the world-wide-web at this URL:
  10.  * http://framework.zend.com/license/new-bsd
  11.  * If you did not receive a copy of the license and are unable to
  12.  * obtain it through the world-wide-web, please send an email
  13.  * to license@zend.com so we can send you a copy immediately.
  14.  *
  15.  * @category   Microsoft
  16.  * @package    Microsoft_Log
  17.  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18.  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  19.  * @version    $Id: Log.php 22632 2010-07-18 18:30:08Z ramon $
  20.  */
  21.  
  22. /**
  23.  * @see Microsoft_AutoLoader
  24.  */
  25. require_once dirname(__FILE__'/AutoLoader.php';
  26.  
  27. /**
  28.  * @category   Microsoft
  29.  * @package    Microsoft_Log
  30.  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31.  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  32.  * @version    $Id: Log.php 22632 2010-07-18 18:30:08Z ramon $
  33.  */
  34. {
  35.     const EMERG   0;  // Emergency: system is unusable
  36.     const ALERT   1;  // Alert: action must be taken immediately
  37.     const CRIT    2;  // Critical: critical conditions
  38.     const ERR     3;  // Error: error conditions
  39.     const WARN    4;  // Warning: warning conditions
  40.     const NOTICE  5;  // Notice: normal but significant condition
  41.     const INFO    6;  // Informational: informational messages
  42.     const DEBUG   7;  // Debug: debug messages
  43.  
  44.     /**
  45.      * @var array of priorities where the keys are the
  46.      *  priority numbers and the values are the priority names
  47.      */
  48.     protected $_priorities = array();
  49.  
  50.     /**
  51.      * @var array of Microsoft_Log_Writer_Abstract
  52.      */
  53.     protected $_writers = array();
  54.  
  55.     /**
  56.      * @var array of Microsoft_Log_Filter_Interface
  57.      */
  58.     protected $_filters = array();
  59.  
  60.     /**
  61.      * @var array of extra log event
  62.      */
  63.     protected $_extras = array();
  64.  
  65.     /**
  66.      *
  67.      * @var string 
  68.      */
  69.     protected $_defaultWriterNamespace = 'Microsoft_Log_Writer';
  70.  
  71.     /**
  72.      *
  73.      * @var string 
  74.      */
  75.     protected $_defaultFilterNamespace = 'Microsoft_Log_Filter';
  76.  
  77.  
  78.     /**
  79.      * Class constructor.  Create a new logger
  80.      *
  81.      * @param Microsoft_Log_Writer_Abstract|null $writer  default writer
  82.      */
  83.     public function __construct(Microsoft_Log_Writer_Abstract $writer null)
  84.     {
  85.         $r new ReflectionClass($this);
  86.         $this->_priorities = array_flip($r->getConstants());
  87.  
  88.         if ($writer !== null{
  89.             $this->addWriter($writer);
  90.         }
  91.     }
  92.  
  93.     /**
  94.      * Factory to construct the logger and one or more writers
  95.      * based on the configuration array
  96.      *
  97.      * @param  array $config 
  98.      * @return Microsoft_Log 
  99.      */
  100.     static public function factory($config array())
  101.     {
  102.         if (!is_array($config|| empty($config)) {
  103.             /** @see Microsoft_Log_Exception */
  104.             require_once 'Microsoft/Log/Exception.php';
  105.             throw new Microsoft_Log_Exception('Configuration must be an array');
  106.         }
  107.  
  108.         $log new Microsoft_Log;
  109.  
  110.         if (!is_array(current($config))) {
  111.             $log->addWriter(current($config));
  112.         else {
  113.             foreach($config as $writer{
  114.                 $log->addWriter($writer);
  115.             }
  116.         }
  117.  
  118.         return $log;
  119.     }
  120.  
  121.  
  122.     /**
  123.      * Construct a writer object based on a configuration array
  124.      *
  125.      * @param  array $spec config array with writer spec
  126.      * @return Microsoft_Log_Writer_Abstract 
  127.      */
  128.     protected function _constructWriterFromConfig($config)
  129.     {
  130.         $writer $this->_constructFromConfig('writer'$config$this->_defaultWriterNamespace);
  131.  
  132.         if (!$writer instanceof Microsoft_Log_Writer_Abstract{
  133.             $writerName is_object($writer)
  134.                         ? get_class($writer)
  135.                         : 'The specified writer';
  136.             /** @see Microsoft_Log_Exception */
  137.             require_once 'Microsoft/Log/Exception.php';
  138.             throw new Microsoft_Log_Exception("{$writerName} does not extend Microsoft_Log_Writer_Abstract!");
  139.         }
  140.  
  141.         if (isset($config['filterName'])) {
  142.             $filter $this->_constructFilterFromConfig($config);
  143.             $writer->addFilter($filter);
  144.         }
  145.  
  146.         return $writer;
  147.     }
  148.  
  149.     /**
  150.      * Construct filter object from configuration array or Microsoft_Config object
  151.      *
  152.      * @param  array $config 
  153.      * @return Microsoft_Log_Filter_Interface 
  154.      */
  155.     protected function _constructFilterFromConfig($config)
  156.     {
  157.         $filter $this->_constructFromConfig('filter'$config$this->_defaultFilterNamespace);
  158.  
  159.         if (!$filter instanceof Microsoft_Log_Filter_Interface{
  160.              $filterName is_object($filter)
  161.                          ? get_class($filter)
  162.                          : 'The specified filter';
  163.             /** @see Microsoft_Log_Exception */
  164.             require_once 'Microsoft/Log/Exception.php';
  165.             throw new Microsoft_Log_Exception("{$filterName} does not implement Microsoft_Log_Filter_Interface");
  166.         }
  167.  
  168.         return $filter;
  169.     }
  170.  
  171.     /**
  172.      * Construct a filter or writer from config
  173.      *
  174.      * @param string $type 'writer' of 'filter'
  175.      * @param array $config 
  176.      * @param string $namespace 
  177.      * @return object 
  178.      */
  179.     protected function _constructFromConfig($type$config$namespace)
  180.     {
  181.         if (!is_array($config|| empty($config)) {
  182.             require_once 'Microsoft/Log/Exception.php';
  183.             throw new Microsoft_Log_Exception('Configuration must be an array');
  184.         }
  185.  
  186.         $params    = isset($config$type .'Params' ]$config$type .'Params' array();
  187.         $className $this->getClassName($config$type$namespace);
  188.         if (!class_exists($className)) {
  189.             require_once 'Microsoft/Loader.php';
  190.             Microsoft_Loader::loadClass($className);
  191.         }
  192.  
  193.         $reflection new ReflectionClass($className);
  194.         if (!$reflection->implementsInterface('Microsoft_Log_FactoryInterface')) {
  195.             require_once 'Zend/Log/Exception.php';
  196.             throw new Microsoft_Log_Exception(
  197.                 'Driver does not implement Microsoft_Log_FactoryInterface and can not be constructed from config.'
  198.             );
  199.         }
  200.  
  201.         return call_user_func(array($className'factory')$params);
  202.     }
  203.  
  204.     /**
  205.      * Get the writer or filter full classname
  206.      *
  207.      * @param array $config 
  208.      * @param string $type filter|writer
  209.      * @param string $defaultNamespace 
  210.      * @return string full classname
  211.      */
  212.     protected function getClassName($config$type$defaultNamespace)
  213.     {
  214.         if (!isset($config$type 'Name' ])) {
  215.             require_once 'Zend/Log/Exception.php';
  216.             throw new Microsoft_Log_Exception("Specify {$type}Name in the configuration array");
  217.         }
  218.         $className $config$type 'Name' ];
  219.  
  220.         $namespace $defaultNamespace;
  221.         if (isset($config$type 'Namespace' ])) {
  222.             $namespace $config$type 'Namespace' ];
  223.         }
  224.  
  225.         $fullClassName $namespace '_' $className;
  226.         return $fullClassName;
  227.     }
  228.  
  229.     /**
  230.      * Class destructor.  Shutdown log writers
  231.      *
  232.      * @return void 
  233.      */
  234.     public function __destruct()
  235.     {
  236.         foreach($this->_writers as $writer{
  237.             $writer->shutdown();
  238.         }
  239.     }
  240.  
  241.     /**
  242.      * Undefined method handler allows a shortcut:
  243.      *   $log->priorityName('message')
  244.      *     instead of
  245.      *   $log->log('message', Microsoft_Log::PRIORITY_NAME)
  246.      *
  247.      * @param  string  $method  priority name
  248.      * @param  string  $params  message to log
  249.      * @return void 
  250.      * @throws Microsoft_Log_Exception
  251.      */
  252.     public function __call($method$params)
  253.     {
  254.         $priority strtoupper($method);
  255.         if (($priority array_search($priority$this->_priorities)) !== false{
  256.             switch (count($params)) {
  257.                 case 0:
  258.                     /** @see Microsoft_Log_Exception */
  259.                     require_once 'Microsoft_/Log/Exception.php';
  260.                     throw new Microsoft_Log_Exception('Missing log message');
  261.                 case 1:
  262.                     $message array_shift($params);
  263.                     $extras null;
  264.                     break;
  265.                 default:
  266.                     $message array_shift($params);
  267.                     $extras  array_shift($params);
  268.                     break;
  269.             }
  270.             $this->log($message$priority$extras);
  271.         else {
  272.             /** @see Microsoft_Log_Exception */
  273.             require_once 'Microsoft/Log/Exception.php';
  274.             throw new Microsoft_Log_Exception('Bad log priority');
  275.         }
  276.     }
  277.  
  278.     /**
  279.      * Log a message at a priority
  280.      *
  281.      * @param  string   $message   Message to log
  282.      * @param  integer  $priority  Priority of message
  283.      * @param  mixed    $extras    Extra information to log in event
  284.      * @return void 
  285.      * @throws Microsoft_Log_Exception
  286.      */
  287.     public function log($message$priority$extras null)
  288.     {
  289.         // sanity checks
  290.         if (empty($this->_writers)) {
  291.             /** @see Microsoft_Log_Exception */
  292.             require_once 'Microsoft/Log/Exception.php';
  293.             throw new Microsoft_Log_Exception('No writers were added');
  294.         }
  295.  
  296.         if (isset($this->_priorities[$priority])) {
  297.             /** @see Microsoft_Log_Exception */
  298.             require_once 'Microsoft/Log/Exception.php';
  299.             throw new Microsoft_Log_Exception('Bad log priority');
  300.         }
  301.  
  302.         // pack into event required by filters and writers
  303.         $event array_merge(array('timestamp'    => date('c'),
  304.                                     'message'      => $message,
  305.                                     'priority'     => $priority,
  306.                                     'priorityName' => $this->_priorities[$priority]),
  307.                               $this->_extras);
  308.  
  309.         // Check to see if any extra information was passed
  310.         if (!empty($extras)) {
  311.             $info array();
  312.             if (is_array($extras)) {
  313.                 foreach ($extras as $key => $value{
  314.                     if (is_string($key)) {
  315.                         $event[$key$value;
  316.                     else {
  317.                         $info[$value;
  318.                     }
  319.                 }
  320.             else {
  321.                 $info $extras;
  322.             }
  323.             if (!empty($info)) {
  324.                 $event['info'$info;
  325.             }
  326.         }
  327.  
  328.         // abort if rejected by the global filters
  329.         foreach ($this->_filters as $filter{
  330.             if ($filter->accept($event)) {
  331.                 return;
  332.             }
  333.         }
  334.  
  335.         // send to each writer
  336.         foreach ($this->_writers as $writer{
  337.             $writer->write($event);
  338.         }
  339.     }
  340.  
  341.     /**
  342.      * Add a custom priority
  343.      *
  344.      * @param  string   $name      Name of priority
  345.      * @param  integer  $priority  Numeric priority
  346.      */
  347.     public function addPriority($name$priority)
  348.     {
  349.         // Priority names must be uppercase for predictability.
  350.         $name strtoupper($name);
  351.  
  352.         if (isset($this->_priorities[$priority])
  353.             || false !== array_search($name$this->_priorities)) {
  354.             /** @see Microsoft_Log_Exception */
  355.             require_once 'Microsoft/Log/Exception.php';
  356.             throw new Microsoft_Log_Exception('Existing priorities cannot be overwritten');
  357.         }
  358.  
  359.         $this->_priorities[$priority$name;
  360.     }
  361.  
  362.     /**
  363.      * Add a filter that will be applied before all log writers.
  364.      * Before a message will be received by any of the writers, it
  365.      * must be accepted by all filters added with this method.
  366.      *
  367.      * @param  int|Microsoft_Log_Filter_Interface$filter 
  368.      * @return void 
  369.      */
  370.     public function addFilter($filter)
  371.     {
  372.         if (is_integer($filter)) {
  373.             /** @see Microsoft_Log_Filter_Priority */
  374.             require_once 'Microsoft/Log/Filter/Priority.php';
  375.             $filter new Microsoft_Log_Filter_Priority($filter);
  376.  
  377.         elseif ($filter instanceof Microsoft_Config || is_array($filter)) {
  378.             $filter $this->_constructFilterFromConfig($filter);
  379.  
  380.         elseif($filter instanceof Microsoft_Log_Filter_Interface{
  381.             /** @see Microsoft_Log_Exception */
  382.             require_once 'Microsoft/Log/Exception.php';
  383.             throw new Microsoft_Log_Exception('Invalid filter provided');
  384.         }
  385.  
  386.         $this->_filters[$filter;
  387.     }
  388.  
  389.     /**
  390.      * Add a writer.  A writer is responsible for taking a log
  391.      * message and writing it out to storage.
  392.      *
  393.      * @param  mixed $writer Microsoft_Log_Writer_Abstract or Config array
  394.      * @return void 
  395.      */
  396.     public function addWriter($writer)
  397.     {
  398.         if (is_array($writer)) {
  399.             $writer $this->_constructWriterFromConfig($writer);
  400.         }
  401.  
  402.         if (!$writer instanceof Microsoft_Log_Writer_Abstract{
  403.             /** @see Microsoft_Log_Exception */
  404.             require_once 'Microsoft/Log/Exception.php';
  405.             throw new Microsoft_Log_Exception(
  406.                 'Writer must be an instance of Microsoft_Log_Writer_Abstract'
  407.                 . ' or you should pass a configuration array'
  408.             );
  409.         }
  410.  
  411.         $this->_writers[$writer;
  412.     }
  413.  
  414.     /**
  415.      * Set an extra item to pass to the log writers.
  416.      *
  417.      * @param  $name    Name of the field
  418.      * @param  $value   Value of the field
  419.      * @return void 
  420.      */
  421.     public function setEventItem($name$value)
  422.     {
  423.         $this->_extras = array_merge($this->_extrasarray($name => $value));
  424.     }
  425. }

Documentation generated on Wed, 18 May 2011 12:06:40 +0200 by phpDocumentor 1.4.3