Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Syslog.php
Go to the documentation of this file.
1 <?php
24 #require_once 'Zend/Log.php';
25 
27 #require_once 'Zend/Log/Writer/Abstract.php';
28 
39 {
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 
61  protected $_defaultPriority = LOG_NOTICE;
62 
68  protected static $_lastApplication;
69 
75  protected static $_lastFacility;
76 
82  protected $_application = 'Zend_Log';
83 
89  protected $_facility = LOG_USER;
90 
96  protected $_validFacilities = array();
97 
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 
127  static public function factory($config)
128  {
129  return new self(self::_parseConfig($config));
130  }
131 
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 
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 
187  public function setFacility($facility)
188  {
189  if ($this->_facility === $facility) {
190  return $this;
191  }
192 
193  if (!count($this->_validFacilities)) {
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 
221  {
222  if ($this->_application === $application) {
223  return $this;
224  }
225  $this->_application = $application;
226  $this->_initializeSyslog();
227  return $this;
228  }
229 
235  public function shutdown()
236  {
237  closelog();
238  }
239 
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 }
__construct(array $params=array())
Definition: Syslog.php:104
const WARN
Definition: Log.php:46
static $_lastApplication
Definition: Syslog.php:68
$config
Definition: fraud_order.php:17
setFacility($facility)
Definition: Syslog.php:187
const DEBUG
Definition: Log.php:49
const CRIT
Definition: Log.php:44
const ERR
Definition: Log.php:45
$message
static factory($config)
Definition: Syslog.php:127
$application
Definition: bootstrap.php:58
const INFO
Definition: Log.php:48
const EMERG
Definition: Log.php:42
const ALERT
Definition: Log.php:43
static _parseConfig($config)
Definition: Abstract.php:126
setApplicationName($application)
Definition: Syslog.php:220
const NOTICE
Definition: Log.php:47
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18