PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

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