‪TYPO3CMS  9.5
SyslogWriter.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
17 
22 {
29  private ‪$facilities = [
30  'auth' => LOG_AUTH,
31  'authpriv' => LOG_AUTHPRIV,
32  'cron' => LOG_CRON,
33  'daemon' => LOG_DAEMON,
34  'kern' => LOG_KERN,
35  'lpr' => LOG_LPR,
36  'mail' => LOG_MAIL,
37  'news' => LOG_NEWS,
38  'syslog' => LOG_SYSLOG,
39  'user' => LOG_USER,
40  'uucp' => LOG_UUCP
41  ];
42 
48  protected ‪$facility = LOG_USER;
49 
57  public function ‪__construct(array $options = [])
58  {
59  // additional facilities for *nix environments
60  if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
61  $this->facilities['local0'] = LOG_LOCAL0;
62  $this->facilities['local1'] = LOG_LOCAL1;
63  $this->facilities['local2'] = LOG_LOCAL2;
64  $this->facilities['local3'] = LOG_LOCAL3;
65  $this->facilities['local4'] = LOG_LOCAL4;
66  $this->facilities['local5'] = LOG_LOCAL5;
67  $this->facilities['local6'] = LOG_LOCAL6;
68  $this->facilities['local7'] = LOG_LOCAL7;
69  }
70  parent::__construct($options);
71  if (!openlog('TYPO3', LOG_ODELAY | LOG_PID, $this->facility)) {
72  $facilityName = array_search($this->facility, $this->facilities);
73  throw new \RuntimeException('Could not open syslog for facility ' . $facilityName, 1321722682);
74  }
75  }
76 
80  public function ‪__destruct()
81  {
82  closelog();
83  }
84 
90  public function ‪setFacility(‪$facility)
91  {
92  if (array_key_exists(strtolower(‪$facility), $this->facilities)) {
93  $this->facility = $this->facilities[strtolower(‪$facility)];
94  }
95  }
96 
103  public function ‪getMessageForSyslog(‪LogRecord $record)
104  {
105  $data = '';
106  $recordData = $record->‪getData();
107  if (!empty($recordData)) {
108  // According to PSR3 the exception-key may hold an \Exception
109  // Since json_encode() does not encode an exception, we run the _toString() here
110  if (isset($recordData['exception']) && $recordData['exception'] instanceof \‪Exception) {
111  $recordData['exception'] = (string)$recordData['exception'];
112  }
113  $data = '- ' . json_encode($recordData);
114  }
115  $message = sprintf(
116  '[request="%s" component="%s"] %s %s',
117  $record->‪getRequestId(),
118  $record->‪getComponent(),
119  $record->‪getMessage(),
120  $data
121  );
122  return $message;
123  }
124 
132  public function ‪writeLog(‪LogRecord $record)
133  {
134  if (false === syslog($record->‪getLevel(), $this->getMessageForSyslog($record))) {
135  throw new \RuntimeException('Could not write log record to syslog', 1345036337);
136  }
137  return $this;
138  }
139 }
‪TYPO3\CMS\Core\Log\Writer\SyslogWriter\getMessageForSyslog
‪string getMessageForSyslog(LogRecord $record)
Definition: SyslogWriter.php:101
‪TYPO3\CMS\Core\Log\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Core\Log\Writer\SyslogWriter\setFacility
‪setFacility($facility)
Definition: SyslogWriter.php:88
‪TYPO3\CMS\Core\Log\Writer\SyslogWriter\$facilities
‪int[] $facilities
Definition: SyslogWriter.php:28
‪TYPO3\CMS\Core\Log\Writer\SyslogWriter\writeLog
‪TYPO3 CMS Core Log Writer WriterInterface writeLog(LogRecord $record)
Definition: SyslogWriter.php:130
‪TYPO3\CMS\Core\Log\Writer\SyslogWriter\__destruct
‪__destruct()
Definition: SyslogWriter.php:78
‪TYPO3\CMS\Core\Log\LogRecord\getData
‪array getData()
Definition: LogRecord.php:186
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:21
‪TYPO3\CMS\Core\Log\LogRecord\getMessage
‪string getMessage()
Definition: LogRecord.php:221
‪TYPO3\CMS\Core\Log\LogRecord\getComponent
‪string getComponent()
Definition: LogRecord.php:117
‪TYPO3\CMS\Core\Log\Writer
Definition: AbstractWriter.php:2
‪TYPO3\CMS\Core\Log\Writer\SyslogWriter
Definition: SyslogWriter.php:22
‪TYPO3\CMS\Core\Log\Writer\AbstractWriter
Definition: AbstractWriter.php:24
‪TYPO3\CMS\Core\Log\LogRecord\getLevel
‪int getLevel()
Definition: LogRecord.php:164
‪TYPO3\CMS\Core\Log\Writer\SyslogWriter\$facility
‪int $facility
Definition: SyslogWriter.php:46
‪TYPO3\CMS\Core\Log\LogRecord\getRequestId
‪string getRequestId()
Definition: LogRecord.php:243
‪TYPO3\CMS\Core\Log\Writer\SyslogWriter\__construct
‪__construct(array $options=[])
Definition: SyslogWriter.php:55