[FOLLOWUP][BUGFIX] Handle exceptions in Logging API
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Log / Writer / SyslogWriter.php
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 */
16 use TYPO3\CMS\Core\Log\LogRecord;
17
18 /**
19 * Log writer that writes to syslog
20 *
21 * @author Ingo Renner <ingo@typo3.org>
22 * @author Steffen Müller <typo3@t3node.com>
23 */
24 class SyslogWriter extends AbstractWriter {
25
26 /**
27 * List of valid syslog facility names.
28 * private as it's not supposed to be changed.
29 *
30 * @var int[] Facilities
31 */
32 private $facilities = array(
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
46 /**
47 * Type of program that is logging to syslog.
48 *
49 * @var int
50 */
51 protected $facility = LOG_USER;
52
53 /**
54 * Constructor, adds facilities on *nix environments.
55 *
56 * @param array $options Configuration options
57 * @throws \RuntimeException if connection to syslog cannot be opened
58 * @see \TYPO3\CMS\Core\Log\Writer\AbstractWriter
59 */
60 public function __construct(array $options = array()) {
61 // additional facilities for *nix environments
62 if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
63 $this->facilities['local0'] = LOG_LOCAL0;
64 $this->facilities['local1'] = LOG_LOCAL1;
65 $this->facilities['local2'] = LOG_LOCAL2;
66 $this->facilities['local3'] = LOG_LOCAL3;
67 $this->facilities['local4'] = LOG_LOCAL4;
68 $this->facilities['local5'] = LOG_LOCAL5;
69 $this->facilities['local6'] = LOG_LOCAL6;
70 $this->facilities['local7'] = LOG_LOCAL7;
71 }
72 parent::__construct($options);
73 if (!openlog('TYPO3', (LOG_ODELAY | LOG_PID), $this->facility)) {
74 $facilityName = array_search($this->facility, $this->facilities);
75 throw new \RuntimeException('Could not open syslog for facility ' . $facilityName, 1321722682);
76 }
77 }
78
79 /**
80 * Destructor, closes connection to syslog.
81 */
82 public function __destruct() {
83 closelog();
84 }
85
86 /**
87 * Sets the facility to use when logging to syslog.
88 *
89 * @param int $facility Facility to use when logging.
90 * @return void
91 */
92 public function setFacility($facility) {
93 if (array_key_exists(strtolower($facility), $this->facilities)) {
94 $this->facility = $this->facilities[strtolower($facility)];
95 }
96 }
97
98 /**
99 * Returns the data of the record in syslog format
100 *
101 * @param LogRecord $record
102 * @return string
103 */
104 public function getMessageForSyslog(LogRecord $record) {
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
125 /**
126 * Writes the log record to syslog
127 *
128 * @param LogRecord $record Log record
129 * @return \TYPO3\CMS\Core\Log\Writer\WriterInterface
130 * @throws \RuntimeException
131 */
132 public function writeLog(LogRecord $record) {
133 if (FALSE === syslog($record->getLevel(), $this->getMessageForSyslog($record))) {
134 throw new \RuntimeException('Could not write log record to syslog', 1345036337);
135 }
136 return $this;
137 }
138
139 }