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