PageRenderTime 34ms CodeModel.GetById 28ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Log/Writer/Abstract.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 138 lines | 49 code | 14 blank | 75 comment | 5 complexity | 0d9185194d914829042bc7a6baf20e89 MD5 | raw file
  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-2012 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 24593 2012-01-05 20:35:02Z matthew $
 21 */
 22
 23/** Zend_Log_Filter_Priority */
 24require_once 'Zend/Log/Filter/Priority.php';
 25
 26/**
 27 * @category   Zend
 28 * @package    Zend_Log
 29 * @subpackage Writer
 30 * @copyright  Copyright (c) 2005-2012 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 24593 2012-01-05 20:35:02Z matthew $
 33 */
 34abstract 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     *
 44     * @var Zend_Log_Formatter_Interface
 45     */
 46    protected $_formatter;
 47
 48    /**
 49     * Add a filter specific to this writer.
 50     *
 51     * @param  Zend_Log_Filter_Interface  $filter
 52     * @return Zend_Log_Writer_Abstract
 53     */
 54    public function addFilter($filter)
 55    {
 56        if (is_int($filter)) {
 57            $filter = new Zend_Log_Filter_Priority($filter);
 58        }
 59
 60        if (!$filter instanceof Zend_Log_Filter_Interface) {
 61            /** @see Zend_Log_Exception */
 62            require_once 'Zend/Log/Exception.php';
 63            throw new Zend_Log_Exception('Invalid filter provided');
 64        }
 65
 66        $this->_filters[] = $filter;
 67        return $this;
 68    }
 69
 70    /**
 71     * Log a message to this writer.
 72     *
 73     * @param  array $event log data event
 74     * @return void
 75     */
 76    public function write($event)
 77    {
 78        foreach ($this->_filters as $filter) {
 79            if (! $filter->accept($event)) {
 80                return;
 81            }
 82        }
 83
 84        // exception occurs on error
 85        $this->_write($event);
 86    }
 87
 88    /**
 89     * Set a new formatter for this writer
 90     *
 91     * @param  Zend_Log_Formatter_Interface $formatter
 92     * @return Zend_Log_Writer_Abstract
 93     */
 94    public function setFormatter(Zend_Log_Formatter_Interface $formatter)
 95    {
 96        $this->_formatter = $formatter;
 97        return $this;
 98    }
 99
100    /**
101     * Perform shutdown activites such as closing open resources
102     *
103     * @return void
104     */
105    public function shutdown()
106    {}
107
108    /**
109     * Write a message to the log.
110     *
111     * @param  array  $event  log data event
112     * @return void
113     */
114    abstract protected function _write($event);
115
116    /**
117     * Validate and optionally convert the config to array
118     *
119     * @param  array|Zend_Config $config Zend_Config or Array
120     * @return array
121     * @throws Zend_Log_Exception
122     */
123    static protected function _parseConfig($config)
124    {
125        if ($config instanceof Zend_Config) {
126            $config = $config->toArray();
127        }
128
129        if (!is_array($config)) {
130            require_once 'Zend/Log/Exception.php';
131            throw new Zend_Log_Exception(
132                'Configuration must be an array or instance of Zend_Config'
133            );
134        }
135
136        return $config;
137    }
138}