Created
April 1, 2013 01:18
-
-
Save stmllr/5282732 to your computer and use it in GitHub Desktop.
Prove of concept for a Deprecation LogWriter for TYPO3.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace TYPO3\CMS\Core\Log\Writer; | |
/*************************************************************** | |
* Copyright notice | |
* | |
* (c) 2013 Steffen Müller (typo3@t3node.com) | |
* All rights reserved | |
* | |
* This script is part of the TYPO3 project. The TYPO3 project is | |
* free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* The GNU General Public License can be found at | |
* http://www.gnu.org/copyleft/gpl.html. | |
* | |
* This script is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* This copyright notice MUST APPEAR in all copies of the script! | |
***************************************************************/ | |
/** | |
* Log writer for the deprecation log. | |
* It preprocesses all deprecation records and then sends them to the LogWriter destination | |
* | |
* @author Steffen Müller <typo3@t3node.com> | |
*/ | |
class DeprecationWriter extends \TYPO3\CMS\Core\Log\Writer\AbstractWriter { | |
/** | |
* Collection of LogWriters to send the deprecation messages to | |
* | |
* @var array | |
*/ | |
protected $logWriters = array(); | |
/** | |
* If set, a token for each log record is generated and stored in the sys_registry. | |
* Only those log records are written, which don't have their token registered. | |
* | |
* @var bool | |
*/ | |
protected $filterDuplicates = FALSE; | |
/** | |
* Writes the log record | |
* | |
* @param \TYPO3\CMS\Core\Log\LogRecord $record Log record | |
* @return \TYPO3\CMS\Core\Log\Writer\WriterInterface $this | |
* @throws \RuntimeException | |
*/ | |
public function writeLog(\TYPO3\CMS\Core\Log\LogRecord $record) { | |
if (!$this->filterDuplicates) { | |
$this->processWriteLog($record); | |
} else { | |
/** @var $registry \TYPO3\CMS\Core\Registry */ | |
$registry = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Registry'); | |
$registeredTokens = $registry->get('core','deprecationLogTokens'); | |
$token = substr(md5($record->getMessage()), 0, 8); | |
if (!in_array($token, $registeredTokens)) { | |
$this->processWriteLog($record); | |
$registeredTokens[] = $token; | |
$registry->set('core','deprecationLogTokens', $registeredTokens); | |
} | |
} | |
return $this; | |
} | |
protected function processWriteLog($record) { | |
$logWriters = $this->getLogWriters(); | |
foreach ($logWriters as $logWriterClassName => $logWriterOptions) { | |
/** @var $logWriter \TYPO3\CMS\Core\Log\Writer\WriterInterface */ | |
$logWriter = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($logWriterClassName, $logWriterOptions); | |
$logWriter->writeLog($record); | |
} | |
} | |
/** | |
* Sets logWriters | |
* | |
* @param array $logWriters | |
* @return void | |
*/ | |
public function setLogWriters($logWriters) { | |
$this->logWriters = $logWriters; | |
} | |
/** | |
* Returns logWriters | |
* | |
* @return array | |
*/ | |
public function getLogWriters() { | |
return $this->logWriters; | |
} | |
/** | |
* Sets filterDuplicates | |
* | |
* @param boolean $filterDuplicates | |
* | |
* @return void | |
*/ | |
public function setFilterDuplicates($filterDuplicates) { | |
$this->filterDuplicates = $filterDuplicates; | |
} | |
/** | |
* Returns filterDuplicates | |
* | |
* @return boolean | |
*/ | |
public function getFilterDuplicates() { | |
return $this->filterDuplicates; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment