TYPO3 CMS  TYPO3_6-2
Logger.php
Go to the documentation of this file.
1 <?php
3 
24 class Logger implements \Psr\Log\LoggerInterface {
25 
34  protected $name = '';
35 
42 
48  protected $writers = array();
49 
55  protected $processors = array();
56 
63  public function __construct($name) {
64  $this->name = $name;
65  }
66 
73  protected function setMinimumLogLevel($level) {
75  $this->minimumLogLevel = $level;
76  return $this;
77  }
78 
84  protected function getMinimumLogLevel() {
86  }
87 
93  public function getName() {
94  return $this->name;
95  }
96 
104  public function addWriter($minimumLevel, \TYPO3\CMS\Core\Log\Writer\WriterInterface $writer) {
106  // Cycle through all the log levels which are as severe as or higher
107  // than $minimumLevel and add $writer to each severity level
108  for ($logLevelWhichTriggersWriter = \TYPO3\CMS\Core\Log\LogLevel::EMERGENCY; $logLevelWhichTriggersWriter <= $minimumLevel; $logLevelWhichTriggersWriter++) {
109  if (!isset($this->writers[$logLevelWhichTriggersWriter])) {
110  $this->writers[$logLevelWhichTriggersWriter] = array();
111  }
112  $this->writers[$logLevelWhichTriggersWriter][] = $writer;
113  }
114  if ($minimumLevel > $this->getMinimumLogLevel()) {
115  $this->setMinimumLogLevel($minimumLevel);
116  }
117  return $this;
118  }
119 
125  public function getWriters() {
126  return $this->writers;
127  }
128 
136  public function addProcessor($minimumLevel, \TYPO3\CMS\Core\Log\Processor\ProcessorInterface $processor) {
138  // Cycle through all the log levels which are as severe as or higher
139  // than $minimumLevel and add $processor to each severity level
140  for ($logLevelWhichTriggersProcessor = \TYPO3\CMS\Core\Log\LogLevel::EMERGENCY; $logLevelWhichTriggersProcessor <= $minimumLevel; $logLevelWhichTriggersProcessor++) {
141  if (!isset($this->processors[$logLevelWhichTriggersProcessor])) {
142  $this->processors[$logLevelWhichTriggersProcessor] = array();
143  }
144  $this->processors[$logLevelWhichTriggersProcessor][] = $processor;
145  }
146  if ($minimumLevel > $this->getMinimumLogLevel()) {
147  $this->setMinimumLogLevel($minimumLevel);
148  }
149  }
150 
156  public function getProcessors() {
157  return $this->processors;
158  }
159 
168  public function log($level, $message, array $data = array()) {
169  $level = LogLevel::normalizeLevel($level);
171  if ($level > $this->minimumLogLevel) {
172  return $this;
173  }
175  $record = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Log\\LogRecord', $this->name, $level, $message, $data);
176  $record = $this->callProcessors($record);
177  $this->writeLog($record);
178  return $this;
179  }
180 
188  protected function callProcessors(\TYPO3\CMS\Core\Log\LogRecord $record) {
189  if (!empty($this->processors[$record->getLevel()])) {
190  foreach ($this->processors[$record->getLevel()] as $processor) {
191  $processedRecord = $processor->processLogRecord($record);
192  if (!$processedRecord instanceof \TYPO3\CMS\Core\Log\LogRecord) {
193  throw new \RuntimeException('Processor ' . get_class($processor) . ' returned invalid data. Instance of TYPO3\\CMS\\Core\\Log\\LogRecord expected', 1343593398);
194  }
195  $record = $processedRecord;
196  }
197  }
198  return $record;
199  }
200 
207  protected function writeLog(\TYPO3\CMS\Core\Log\LogRecord $record) {
208  if (!empty($this->writers[$record->getLevel()])) {
209  foreach ($this->writers[$record->getLevel()] as $writer) {
210  $writer->writeLog($record);
211  }
212  }
213  }
214 
222  public function emergency($message, array $data = array()) {
223  return $this->log(\TYPO3\CMS\Core\Log\LogLevel::EMERGENCY, $message, $data);
224  }
225 
233  public function alert($message, array $data = array()) {
234  return $this->log(\TYPO3\CMS\Core\Log\LogLevel::ALERT, $message, $data);
235  }
236 
244  public function critical($message, array $data = array()) {
245  return $this->log(\TYPO3\CMS\Core\Log\LogLevel::CRITICAL, $message, $data);
246  }
247 
255  public function error($message, array $data = array()) {
256  return $this->log(\TYPO3\CMS\Core\Log\LogLevel::ERROR, $message, $data);
257  }
258 
266  public function warning($message, array $data = array()) {
267  return $this->log(\TYPO3\CMS\Core\Log\LogLevel::WARNING, $message, $data);
268  }
269 
277  public function notice($message, array $data = array()) {
278  return $this->log(\TYPO3\CMS\Core\Log\LogLevel::NOTICE, $message, $data);
279  }
280 
288  public function info($message, array $data = array()) {
289  return $this->log(\TYPO3\CMS\Core\Log\LogLevel::INFO, $message, $data);
290  }
291 
299  public function debug($message, array $data = array()) {
300  return $this->log(\TYPO3\CMS\Core\Log\LogLevel::DEBUG, $message, $data);
301  }
302 
303 }
error($message, array $data=array())
Definition: Logger.php:255
addWriter($minimumLevel, \TYPO3\CMS\Core\Log\Writer\WriterInterface $writer)
Definition: Logger.php:104
emergency($message, array $data=array())
Definition: Logger.php:222
static validateLevel($level)
Definition: LogLevel.php:134
notice($message, array $data=array())
Definition: Logger.php:277
setMinimumLogLevel($level)
Definition: Logger.php:73
static normalizeLevel($level)
Definition: LogLevel.php:146
alert($message, array $data=array())
Definition: Logger.php:233
addProcessor($minimumLevel, \TYPO3\CMS\Core\Log\Processor\ProcessorInterface $processor)
Definition: Logger.php:136
callProcessors(\TYPO3\CMS\Core\Log\LogRecord $record)
Definition: Logger.php:188
writeLog(\TYPO3\CMS\Core\Log\LogRecord $record)
Definition: Logger.php:207
debug($message, array $data=array())
Definition: Logger.php:299
critical($message, array $data=array())
Definition: Logger.php:244
warning($message, array $data=array())
Definition: Logger.php:266
info($message, array $data=array())
Definition: Logger.php:288