TYPO3  7.6
SyslogWriter.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Log\Writer;
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 = array(
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 = array())
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 
91  public function setFacility($facility)
92  {
93  if (array_key_exists(strtolower($facility), $this->facilities)) {
94  $this->facility = $this->facilities[strtolower($facility)];
95  }
96  }
97 
104  public function getMessageForSyslog(LogRecord $record)
105  {
106  $data = '';
107  $recordData = $record->getData();
108  if (!empty($recordData)) {
109  // According to PSR3 the exception-key may hold an \Exception
110  // Since json_encode() does not encode an exception, we run the _toString() here
111  if (isset($recordData['exception']) && $recordData['exception'] instanceof \Exception) {
112  $recordData['exception'] = (string)$recordData['exception'];
113  }
114  $data = '- ' . json_encode($recordData);
115  }
116  $message = sprintf(
117  '[request="%s" component="%s"] %s %s',
118  $record->getRequestId(),
119  $record->getComponent(),
120  $record->getMessage(),
121  $data
122  );
123  return $message;
124  }
125 
133  public function writeLog(LogRecord $record)
134  {
135  if (false === syslog($record->getLevel(), $this->getMessageForSyslog($record))) {
136  throw new \RuntimeException('Could not write log record to syslog', 1345036337);
137  }
138  return $this;
139  }
140 }