TYPO3 CMS  TYPO3_6-2
SyslogWriter.php
Go to the documentation of this file.
1 <?php
3 
18 
26 
33  private $facilities = array(
34  'auth' => LOG_AUTH,
35  'authpriv' => LOG_AUTHPRIV,
36  'cron' => LOG_CRON,
37  'daemon' => LOG_DAEMON,
38  'kern' => LOG_KERN,
39  'lpr' => LOG_LPR,
40  'mail' => LOG_MAIL,
41  'news' => LOG_NEWS,
42  'syslog' => LOG_SYSLOG,
43  'user' => LOG_USER,
44  'uucp' => LOG_UUCP
45  );
46 
52  protected $facility = LOG_USER;
53 
61  public function __construct(array $options = array()) {
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  closelog();
85  }
86 
93  public function setFacility($facility) {
94  if (array_key_exists(strtolower($facility), $this->facilities)) {
95  $this->facility = $this->facilities[strtolower($facility)];
96  }
97  }
98 
105  public function getMessageForSyslog(LogRecord $record) {
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  if (FALSE === syslog($record->getLevel(), $this->getMessageForSyslog($record))) {
135  throw new \RuntimeException('Could not write log record to syslog', 1345036337);
136  }
137  return $this;
138  }
139 
140 }
__construct(array $options=array())