[TASK] Re-work/simplify copyright header in PHP files - Part 3
[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 /**
17 * Log writer that writes to syslog
18 *
19 * @author Ingo Renner <ingo@typo3.org>
20 * @author Steffen Müller <typo3@t3node.com>
21 */
22 class SyslogWriter extends \TYPO3\CMS\Core\Log\Writer\AbstractWriter {
23
24 /**
25 * List of valid syslog facility names.
26 * private as it's not supposed to be changed.
27 *
28 * @var array Facilities
29 */
30 private $facilities = array(
31 'auth' => LOG_AUTH,
32 'authpriv' => LOG_AUTHPRIV,
33 'cron' => LOG_CRON,
34 'daemon' => LOG_DAEMON,
35 'kern' => LOG_KERN,
36 'lpr' => LOG_LPR,
37 'mail' => LOG_MAIL,
38 'news' => LOG_NEWS,
39 'syslog' => LOG_SYSLOG,
40 'user' => LOG_USER,
41 'uucp' => LOG_UUCP
42 );
43
44 /**
45 * Type of program that is logging to syslog.
46 *
47 * @var integer
48 */
49 protected $facility = LOG_USER;
50
51 /**
52 * Constructor, adds facilities on *nix environments.
53 *
54 * @param array $options Configuration options
55 * @throws \RuntimeException if connection to syslog cannot be opened
56 * @see \TYPO3\CMS\Core\Log\Writer\AbstractWriter
57 */
58 public function __construct(array $options = array()) {
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
77 /**
78 * Destructor, closes connection to syslog.
79 */
80 public function __destruct() {
81 closelog();
82 }
83
84 /**
85 * Sets the facility to use when logging to syslog.
86 *
87 * @param integer $facility Facility to use when logging.
88 * @return void
89 */
90 public function setFacility($facility) {
91 if (array_key_exists(strtolower($facility), $this->facilities)) {
92 $this->facility = $this->facilities[strtolower($facility)];
93 }
94 }
95
96 /**
97 * Returns the data of the record in syslog format
98 *
99 * @param \TYPO3\CMS\Core\Log\LogRecord $record
100 * @return string
101 */
102 public function getMessageForSyslog(\TYPO3\CMS\Core\Log\LogRecord $record) {
103 $data = $record->getData();
104 $data = !empty($data) ? '- ' . json_encode($data) : '';
105 $message = sprintf('[request="%s" component="%s"] %s %s', $record->getRequestId(), $record->getComponent(), $record->getMessage(), $data);
106 return $message;
107 }
108
109 /**
110 * Writes the log record to syslog
111 *
112 * @param \TYPO3\CMS\Core\Log\LogRecord $record Log record
113 * @return \TYPO3\CMS\Core\Log\Writer\WriterInterface
114 * @throws \RuntimeException
115 */
116 public function writeLog(\TYPO3\CMS\Core\Log\LogRecord $record) {
117 if (FALSE === syslog($record->getLevel(), $this->getMessageForSyslog($record))) {
118 throw new \RuntimeException('Could not write log record to syslog', 1345036337);
119 }
120 return $this;
121 }
122
123 }