web/lib/Zend/Log/Writer/Abstract.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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   Zend
       
    16  * @package    Zend_Log
       
    17  * @subpackage Writer
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Abstract.php 22567 2010-07-16 03:32:31Z ramon $
       
    21  */
       
    22 
       
    23 /** Zend_Log_Filter_Priority */
       
    24 require_once 'Zend/Log/Filter/Priority.php';
       
    25 
       
    26 /**
       
    27  * @category   Zend
       
    28  * @package    Zend_Log
       
    29  * @subpackage Writer
       
    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: Abstract.php 22567 2010-07-16 03:32:31Z ramon $
       
    33  */
       
    34 abstract class Zend_Log_Writer_Abstract implements Zend_Log_FactoryInterface
       
    35 {
       
    36     /**
       
    37      * @var array of Zend_Log_Filter_Interface
       
    38      */
       
    39     protected $_filters = array();
       
    40 
       
    41     /**
       
    42      * Formats the log message before writing.
       
    43      * @var Zend_Log_Formatter_Interface
       
    44      */
       
    45     protected $_formatter;
       
    46 
       
    47     /**
       
    48      * Add a filter specific to this writer.
       
    49      *
       
    50      * @param  Zend_Log_Filter_Interface  $filter
       
    51      * @return void
       
    52      */
       
    53     public function addFilter($filter)
       
    54     {
       
    55         if (is_integer($filter)) {
       
    56             $filter = new Zend_Log_Filter_Priority($filter);
       
    57         }
       
    58 
       
    59         if (!$filter instanceof Zend_Log_Filter_Interface) {
       
    60             /** @see Zend_Log_Exception */
       
    61             require_once 'Zend/Log/Exception.php';
       
    62             throw new Zend_Log_Exception('Invalid filter provided');
       
    63         }
       
    64 
       
    65         $this->_filters[] = $filter;
       
    66         return $this;
       
    67     }
       
    68 
       
    69     /**
       
    70      * Log a message to this writer.
       
    71      *
       
    72      * @param  array     $event  log data event
       
    73      * @return void
       
    74      */
       
    75     public function write($event)
       
    76     {
       
    77         foreach ($this->_filters as $filter) {
       
    78             if (! $filter->accept($event)) {
       
    79                 return;
       
    80             }
       
    81         }
       
    82 
       
    83         // exception occurs on error
       
    84         $this->_write($event);
       
    85     }
       
    86 
       
    87     /**
       
    88      * Set a new formatter for this writer
       
    89      *
       
    90      * @param  Zend_Log_Formatter_Interface $formatter
       
    91      * @return void
       
    92      */
       
    93     public function setFormatter(Zend_Log_Formatter_Interface $formatter)
       
    94     {
       
    95         $this->_formatter = $formatter;
       
    96         return $this;
       
    97     }
       
    98 
       
    99     /**
       
   100      * Perform shutdown activites such as closing open resources
       
   101      *
       
   102      * @return void
       
   103      */
       
   104     public function shutdown()
       
   105     {}
       
   106 
       
   107     /**
       
   108      * Write a message to the log.
       
   109      *
       
   110      * @param  array  $event  log data event
       
   111      * @return void
       
   112      */
       
   113     abstract protected function _write($event);
       
   114 
       
   115     /**
       
   116      * Validate and optionally convert the config to array
       
   117      *
       
   118      * @param  array|Zend_Config $config Zend_Config or Array
       
   119      * @return array
       
   120      * @throws Zend_Log_Exception
       
   121      */
       
   122     static protected function _parseConfig($config)
       
   123     {
       
   124         if ($config instanceof Zend_Config) {
       
   125             $config = $config->toArray();
       
   126         }
       
   127 
       
   128         if (!is_array($config)) {
       
   129             require_once 'Zend/Log/Exception.php';
       
   130             throw new Zend_Log_Exception(
       
   131 				'Configuration must be an array or instance of Zend_Config'
       
   132 			);
       
   133         }
       
   134 
       
   135         return $config;
       
   136     }
       
   137 }