‪TYPO3CMS  11.5
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): void
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): string
107  {
108  $data = '';
109  $context = $record->getData();
110  $message = $record->getMessage();
111  if (!empty($context)) {
112  // Fold an exception into the message, and string-ify it into context so it can be jsonified.
113  if (isset($context['exception']) && $context['exception'] instanceof \Throwable) {
114  $message .= $this->‪formatException($context['exception']);
115  $context['exception'] = (string)$context['exception'];
116  }
117  $data = '- ' . json_encode($context);
118  }
119 
120  return sprintf(
121  '[request="%s" component="%s"] %s %s',
122  $record->getRequestId(),
123  $record->getComponent(),
124  $this->interpolate($message, $context),
125  $data
126  );
127  }
128 
136  public function ‪writeLog(LogRecord $record)
137  {
138  if (syslog(‪LogLevel::normalizeLevel($record->getLevel()), $this->getMessageForSyslog($record)) === false) {
139  throw new \RuntimeException('Could not write log record to syslog', 1345036337);
140  }
141  return $this;
142  }
143 }
‪TYPO3\CMS\Core\Log\Writer\SyslogWriter\getMessageForSyslog
‪string getMessageForSyslog(LogRecord $record)
Definition: SyslogWriter.php:104
‪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:134
‪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\Writer\AbstractWriter\formatException
‪formatException(\Throwable $ex)
Definition: AbstractWriter.php:83
‪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