‪TYPO3CMS  ‪main
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 
103  public function ‪getMessageForSyslog(LogRecord ‪$record): string
104  {
105  $data = '';
106  $context = ‪$record->getData();
107  $message = ‪$record->getMessage();
108  if (!empty($context)) {
109  // Fold an exception into the message, and string-ify it into context so it can be jsonified.
110  if (isset($context['exception']) && $context['exception'] instanceof \Throwable) {
111  $message .= $this->‪formatException($context['exception']);
112  $context['exception'] = (string)$context['exception'];
113  }
114  $data = '- ' . json_encode($context);
115  }
116 
117  return sprintf(
118  '[request="%s" component="%s"] %s %s',
119  ‪$record->getRequestId(),
120  ‪$record->getComponent(),
121  $this->interpolate($message, $context),
122  $data
123  );
124  }
125 
133  public function ‪writeLog(LogRecord ‪$record)
134  {
135  if (syslog(‪LogLevel::normalizeLevel(‪$record->getLevel()), $this->getMessageForSyslog(‪$record)) === false) {
136  throw new \RuntimeException('Could not write log record to syslog', 1345036337);
137  }
138  return $this;
139  }
140 }
‪TYPO3\CMS\Core\Log\Writer\SyslogWriter\setFacility
‪setFacility($facility)
Definition: SyslogWriter.php:91
‪TYPO3\CMS\Core\Log\Writer\SyslogWriter\getMessageForSyslog
‪getMessageForSyslog(LogRecord $record)
Definition: SyslogWriter.php:101
‪TYPO3\CMS\Core\Log\LogLevel\normalizeLevel
‪static normalizeLevel($level)
Definition: LogLevel.php:90
‪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:131
‪TYPO3\CMS\Core\Log\Writer\AbstractWriter\__construct
‪__construct(array $options=[])
Definition: AbstractWriter.php:34
‪TYPO3\CMS\Core\Log\LogRecord
Definition: LogRecord.php:24
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪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:82
‪TYPO3\CMS\Core\Log\LogLevel
Definition: LogLevel.php:24