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