Created
February 26, 2013 11:47
-
-
Save agitso/5037922 to your computer and use it in GitHub Desktop.
Add a custom logger backend to TYPO3 Flow
This file contains hidden or 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 Ag\Utility\Log\Backend; | |
use TYPO3\Flow\Annotations as Flow; | |
class EmailBackend extends \TYPO3\Flow\Log\Backend\AbstractBackend { | |
/** | |
* @var string|array | |
*/ | |
protected $emails; | |
/** | |
* Carries out all actions necessary to prepare the logging backend, such as opening | |
* the log file or opening a database connection. | |
* | |
* @return void | |
* @api | |
*/ | |
public function open() { | |
$this->severityLabels = array( | |
LOG_EMERG => 'EMERGENCY', | |
LOG_ALERT => 'ALERT ', | |
LOG_CRIT => 'CRITICAL ', | |
LOG_ERR => 'ERROR ', | |
LOG_WARNING => 'WARNING ', | |
LOG_NOTICE => 'NOTICE ', | |
LOG_INFO => 'INFO ', | |
LOG_DEBUG => 'DEBUG ', | |
); | |
} | |
/** | |
* Appends the given message along with the additional information into the log. | |
* | |
* @param string $message The message to log | |
* @param integer $severity One of the LOG_* constants | |
* @param mixed $additionalData A variable containing more information about the event to be logged | |
* @param string $packageKey Key of the package triggering the log (determined automatically if not specified) | |
* @param string $className Name of the class triggering the log (determined automatically if not specified) | |
* @param string $methodName Name of the method triggering the log (determined automatically if not specified) | |
* @return void | |
* @api | |
*/ | |
public function append($message, $severity = LOG_INFO, $additionalData = NULL, $packageKey = NULL, $className = NULL, $methodName = NULL) { | |
if ($severity > $this->severityThreshold) { | |
return; | |
} | |
$severityLabel = (isset($this->severityLabels[$severity])) ? $this->severityLabels[$severity] : 'UNKNOWN '; | |
$output = strftime('%y-%m-%d %H:%M:%S', time()) . ' ' . $severityLabel . ' ' . str_pad($packageKey, 20) . ' ' . $message; | |
if ($className !== NULL || $methodName !== NULL) { | |
$output .= ' [logged in ' . $className . '::' . $methodName . '()]'; | |
} | |
if (!empty($additionalData)) { | |
$output .= PHP_EOL . $this->getFormattedVarDump($additionalData); | |
} | |
if(is_string($this->getEmails())) { | |
mail($this->getEmails(), 'Log message', $output, 'From: [email protected]'); | |
} | |
elseif(is_array($this->getEmails())) { | |
foreach($this->getEmails() as $email) { | |
mail($email, 'Log message', $output, 'From: [email protected]'); | |
} | |
} | |
} | |
/** | |
* Carries out all actions necessary to cleanly close the logging backend, such as | |
* closing the log file or disconnecting from a database. | |
* | |
* @return void | |
* @api | |
*/ | |
public function close() { | |
} | |
/** | |
* @param string|array $emails | |
* @return EmailBackend | |
*/ | |
public function setEmails($emails) { | |
$this->emails = $emails; | |
return $this; | |
} | |
/** | |
* @return string|array | |
*/ | |
public function getEmails() { | |
return $this->emails; | |
} | |
} | |
?> |
This file contains hidden or 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
TYPO3: | |
Flow: | |
log: | |
systemLogger: | |
backend: | |
- TYPO3\Flow\Log\Backend\FileBackend | |
- Ag\Utility\Log\Backend\EmailBackend | |
backendOptions: | |
0: | |
logFileURL: %FLOW_PATH_DATA%Logs/System.log | |
createParentDirectories: TRUE | |
severityThreshold: %LOG_INFO% | |
maximumLogFileSize: 10485760 | |
logFilesToKeep: 1 | |
logMessageOrigin: FALSE | |
1: | |
severityThreshold: %LOG_CRIT% | |
emails: | |
- '[email protected]' | |
- '[email protected]' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment