web/lib/Zend/Log/Writer/Syslog.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Log
    16  * @package    Zend_Log
    17  * @subpackage Writer
    17  * @subpackage Writer
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    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
    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 $
    20  * @version    $Id: Syslog.php 25024 2012-07-30 15:08:15Z rob $
    21  */
    21  */
    22 
    22 
    23 /** Zend_Log */
    23 /** Zend_Log */
    24 require_once 'Zend/Log.php';
    24 require_once 'Zend/Log.php';
    25 
    25 
    30  * Writes log messages to syslog
    30  * Writes log messages to syslog
    31  *
    31  *
    32  * @category   Zend
    32  * @category   Zend
    33  * @package    Zend_Log
    33  * @package    Zend_Log
    34  * @subpackage Writer
    34  * @subpackage Writer
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    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
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    37  */
    37  */
    38 class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract
    38 class Zend_Log_Writer_Syslog extends Zend_Log_Writer_Abstract
    39 {
    39 {
    40     /**
    40     /**
    41      * Maps Zend_Log priorities to PHP's syslog priorities
    41      * Maps Zend_Log priorities to PHP's syslog priorities
       
    42      *
    42      * @var array
    43      * @var array
    43      */
    44      */
    44     protected $_priorities = array(
    45     protected $_priorities = array(
    45         Zend_Log::EMERG  => LOG_EMERG,
    46         Zend_Log::EMERG  => LOG_EMERG,
    46         Zend_Log::ALERT  => LOG_ALERT,
    47         Zend_Log::ALERT  => LOG_ALERT,
    52         Zend_Log::DEBUG  => LOG_DEBUG,
    53         Zend_Log::DEBUG  => LOG_DEBUG,
    53     );
    54     );
    54 
    55 
    55     /**
    56     /**
    56      * The default log priority - for unmapped custom priorities
    57      * The default log priority - for unmapped custom priorities
       
    58      *
    57      * @var string
    59      * @var string
    58      */
    60      */
    59     protected $_defaultPriority = LOG_NOTICE;
    61     protected $_defaultPriority = LOG_NOTICE;
    60 
    62 
    61     /**
    63     /**
    62      * Last application name set by a syslog-writer instance
    64      * Last application name set by a syslog-writer instance
       
    65      *
    63      * @var string
    66      * @var string
    64      */
    67      */
    65     protected static $_lastApplication;
    68     protected static $_lastApplication;
    66 
    69 
    67     /**
    70     /**
    68      * Last facility name set by a syslog-writer instance
    71      * Last facility name set by a syslog-writer instance
       
    72      *
    69      * @var string
    73      * @var string
    70      */
    74      */
    71     protected static $_lastFacility;
    75     protected static $_lastFacility;
    72 
    76 
    73     /**
    77     /**
    74      * Application name used by this syslog-writer instance
    78      * Application name used by this syslog-writer instance
       
    79      *
    75      * @var string
    80      * @var string
    76      */
    81      */
    77     protected $_application = 'Zend_Log';
    82     protected $_application = 'Zend_Log';
    78 
    83 
    79     /**
    84     /**
    80      * Facility used by this syslog-writer instance
    85      * Facility used by this syslog-writer instance
       
    86      *
    81      * @var int
    87      * @var int
    82      */
    88      */
    83     protected $_facility = LOG_USER;
    89     protected $_facility = LOG_USER;
    84 
    90 
    85     /**
    91     /**
    86      * _validFacilities
    92      * Types of program available to logging of message
    87      *
    93      *
    88      * @var array
    94      * @var array
    89      */
    95      */
    90     protected $_validFacilities = array();
    96     protected $_validFacilities = array();
    91 
    97 
    92     /**
    98     /**
    93      * Class constructor
    99      * Class constructor
    94      *
   100      *
    95      * @param  array $options Array of options; may include "application" and "facility" keys
   101      * @param  array $params Array of options; may include "application" and "facility" keys
    96      * @return void
   102      * @return void
    97      */
   103      */
    98     public function __construct(array $params = array())
   104     public function __construct(array $params = array())
    99     {
   105     {
   100         if (isset($params['application'])) {
   106         if (isset($params['application'])) {
   101             $this->_application = $params['application'];
   107             $this->_application = $params['application'];
   102         }
   108         }
   103 
   109 
   104         $runInitializeSyslog = true;
   110         $runInitializeSyslog = true;
   105         if (isset($params['facility'])) {
   111         if (isset($params['facility'])) {
   106             $this->_facility = $this->setFacility($params['facility']);
   112             $this->setFacility($params['facility']);
   107             $runInitializeSyslog = false;
   113             $runInitializeSyslog = false;
   108         }
   114         }
   109 
   115 
   110         if ($runInitializeSyslog) {
   116         if ($runInitializeSyslog) {
   111             $this->_initializeSyslog();
   117             $this->_initializeSyslog();
   115     /**
   121     /**
   116      * Create a new instance of Zend_Log_Writer_Syslog
   122      * Create a new instance of Zend_Log_Writer_Syslog
   117      *
   123      *
   118      * @param  array|Zend_Config $config
   124      * @param  array|Zend_Config $config
   119      * @return Zend_Log_Writer_Syslog
   125      * @return Zend_Log_Writer_Syslog
   120      * @throws Zend_Log_Exception
       
   121      */
   126      */
   122     static public function factory($config)
   127     static public function factory($config)
   123     {
   128     {
   124         return new self(self::_parseConfig($config));
   129         return new self(self::_parseConfig($config));
   125     }
   130     }
   174 
   179 
   175     /**
   180     /**
   176      * Set syslog facility
   181      * Set syslog facility
   177      *
   182      *
   178      * @param  int $facility Syslog facility
   183      * @param  int $facility Syslog facility
   179      * @return void
   184      * @return Zend_Log_Writer_Syslog
   180      * @throws Zend_Log_Exception for invalid log facility
   185      * @throws Zend_Log_Exception for invalid log facility
   181      */
   186      */
   182     public function setFacility($facility)
   187     public function setFacility($facility)
   183     {
   188     {
   184         if ($this->_facility === $facility) {
   189         if ($this->_facility === $facility) {
   208 
   213 
   209     /**
   214     /**
   210      * Set application name
   215      * Set application name
   211      *
   216      *
   212      * @param  string $application Application name
   217      * @param  string $application Application name
   213      * @return void
   218      * @return Zend_Log_Writer_Syslog
   214      */
   219      */
   215     public function setApplicationName($application)
   220     public function setApplicationName($application)
   216     {
   221     {
   217         if ($this->_application === $application) {
   222         if ($this->_application === $application) {
   218             return $this;
   223             return $this;
   233     }
   238     }
   234 
   239 
   235     /**
   240     /**
   236      * Write a message to syslog.
   241      * Write a message to syslog.
   237      *
   242      *
   238      * @param  array $event  event data
   243      * @param  array $event event data
   239      * @return void
   244      * @return void
   240      */
   245      */
   241     protected function _write($event)
   246     protected function _write($event)
   242     {
   247     {
   243         if (array_key_exists($event['priority'], $this->_priorities)) {
   248         if (array_key_exists($event['priority'], $this->_priorities)) {
   250             || $this->_facility !== self::$_lastFacility)
   255             || $this->_facility !== self::$_lastFacility)
   251         {
   256         {
   252             $this->_initializeSyslog();
   257             $this->_initializeSyslog();
   253         }
   258         }
   254 
   259 
   255         syslog($priority, $event['message']);
   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);
   256     }
   266     }
   257 }
   267 }