PageRenderTime 84ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/library/Zend/Log/Writer/Syslog.php

https://bitbucket.org/ksekar/campus
PHP | 267 lines | 128 code | 27 blank | 112 comment | 16 complexity | 7f6c09c9e3976686c9256bcd5f584d72 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: Syslog.php 24594 2012-01-05 21:27:01Z matthew $
 21 */
 22
 23/** Zend_Log */
 24require_once 'Zend/Log.php';
 25
 26/** Zend_Log_Writer_Abstract */
 27require_once 'Zend/Log/Writer/Abstract.php';
 28
 29/**
 30 * Writes log messages to syslog
 31 *
 32 * @category   Zend
 33 * @package    Zend_Log
 34 * @subpackage Writer
 35 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 36 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 37 */
 38class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract
 39{
 40    /**
 41     * Maps Zend_Log priorities to PHP's syslog priorities
 42     *
 43     * @var array
 44     */
 45    protected $_priorities = array(
 46        Zend_Log::EMERG  => LOG_EMERG,
 47        Zend_Log::ALERT  => LOG_ALERT,
 48        Zend_Log::CRIT   => LOG_CRIT,
 49        Zend_Log::ERR    => LOG_ERR,
 50        Zend_Log::WARN   => LOG_WARNING,
 51        Zend_Log::NOTICE => LOG_NOTICE,
 52        Zend_Log::INFO   => LOG_INFO,
 53        Zend_Log::DEBUG  => LOG_DEBUG,
 54    );
 55
 56    /**
 57     * The default log priority - for unmapped custom priorities
 58     *
 59     * @var string
 60     */
 61    protected $_defaultPriority = LOG_NOTICE;
 62
 63    /**
 64     * Last application name set by a syslog-writer instance
 65     *
 66     * @var string
 67     */
 68    protected static $_lastApplication;
 69
 70    /**
 71     * Last facility name set by a syslog-writer instance
 72     *
 73     * @var string
 74     */
 75    protected static $_lastFacility;
 76
 77    /**
 78     * Application name used by this syslog-writer instance
 79     *
 80     * @var string
 81     */
 82    protected $_application = 'Zend_Log';
 83
 84    /**
 85     * Facility used by this syslog-writer instance
 86     *
 87     * @var int
 88     */
 89    protected $_facility = LOG_USER;
 90
 91    /**
 92     * Types of program available to logging of message
 93     *
 94     * @var array
 95     */
 96    protected $_validFacilities = array();
 97
 98    /**
 99     * Class constructor
100     *
101     * @param  array $params Array of options; may include "application" and "facility" keys
102     * @return void
103     */
104    public function __construct(array $params = array())
105    {
106        if (isset($params['application'])) {
107            $this->_application = $params['application'];
108        }
109
110        $runInitializeSyslog = true;
111        if (isset($params['facility'])) {
112            $this->setFacility($params['facility']);
113            $runInitializeSyslog = false;
114        }
115
116        if ($runInitializeSyslog) {
117            $this->_initializeSyslog();
118        }
119    }
120
121    /**
122     * Create a new instance of Zend_Log_Writer_Syslog
123     *
124     * @param  array|Zend_Config $config
125     * @return Zend_Log_Writer_Syslog
126     */
127    static public function factory($config)
128    {
129        return new self(self::_parseConfig($config));
130    }
131
132    /**
133     * Initialize values facilities
134     *
135     * @return void
136     */
137    protected function _initializeValidFacilities()
138    {
139        $constants = array(
140            'LOG_AUTH',
141            'LOG_AUTHPRIV',
142            'LOG_CRON',
143            'LOG_DAEMON',
144            'LOG_KERN',
145            'LOG_LOCAL0',
146            'LOG_LOCAL1',
147            'LOG_LOCAL2',
148            'LOG_LOCAL3',
149            'LOG_LOCAL4',
150            'LOG_LOCAL5',
151            'LOG_LOCAL6',
152            'LOG_LOCAL7',
153            'LOG_LPR',
154            'LOG_MAIL',
155            'LOG_NEWS',
156            'LOG_SYSLOG',
157            'LOG_USER',
158            'LOG_UUCP'
159        );
160
161        foreach ($constants as $constant) {
162            if (defined($constant)) {
163                $this->_validFacilities[] = constant($constant);
164            }
165        }
166    }
167
168    /**
169     * Initialize syslog / set application name and facility
170     *
171     * @return void
172     */
173    protected function _initializeSyslog()
174    {
175        self::$_lastApplication = $this->_application;
176        self::$_lastFacility    = $this->_facility;
177        openlog($this->_application, LOG_PID, $this->_facility);
178    }
179
180    /**
181     * Set syslog facility
182     *
183     * @param  int $facility Syslog facility
184     * @return Zend_Log_Writer_Syslog
185     * @throws Zend_Log_Exception for invalid log facility
186     */
187    public function setFacility($facility)
188    {
189        if ($this->_facility === $facility) {
190            return $this;
191        }
192
193        if (!count($this->_validFacilities)) {
194            $this->_initializeValidFacilities();
195        }
196
197        if (!in_array($facility, $this->_validFacilities)) {
198            require_once 'Zend/Log/Exception.php';
199            throw new Zend_Log_Exception('Invalid log facility provided; please see http://php.net/openlog for a list of valid facility values');
200        }
201
202        if ('WIN' == strtoupper(substr(PHP_OS, 0, 3))
203            && ($facility !== LOG_USER)
204        ) {
205            require_once 'Zend/Log/Exception.php';
206            throw new Zend_Log_Exception('Only LOG_USER is a valid log facility on Windows');
207        }
208
209        $this->_facility = $facility;
210        $this->_initializeSyslog();
211        return $this;
212    }
213
214    /**
215     * Set application name
216     *
217     * @param  string $application Application name
218     * @return Zend_Log_Writer_Syslog
219     */
220    public function setApplicationName($application)
221    {
222        if ($this->_application === $application) {
223            return $this;
224        }
225        $this->_application = $application;
226        $this->_initializeSyslog();
227        return $this;
228    }
229
230    /**
231     * Close syslog.
232     *
233     * @return void
234     */
235    public function shutdown()
236    {
237        closelog();
238    }
239
240    /**
241     * Write a message to syslog.
242     *
243     * @param  array $event event data
244     * @return void
245     */
246    protected function _write($event)
247    {
248        if (array_key_exists($event['priority'], $this->_priorities)) {
249            $priority = $this->_priorities[$event['priority']];
250        } else {
251            $priority = $this->_defaultPriority;
252        }
253
254        if ($this->_application !== self::$_lastApplication
255            || $this->_facility !== self::$_lastFacility)
256        {
257            $this->_initializeSyslog();
258        }
259
260        $message = $event['message'];
261        if ($this->_formatter instanceof Zend_Log_Formatter_Interface) {
262            $message = $this->_formatter->format($event);
263        }
264
265        syslog($priority, $message);
266    }
267}