Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Syslog-Appender for agavi

Edit logging.xml and add as new appender:

<appender name="syslog" class="AgaviSyslogLoggerAppender" layout="default">
    <ae:parameter name="tag">myapp</ae:parameter>
</appender>

Now register the appender:

<logger name="application" class="AgaviFileLogger">
    <appenders>
         <appender>syslog</appender>
    </appenders>
</logger>

Finished.

Each message will appear in your syslog like that:

Sep 26 13:39:44 root-server myapp[254]: A message!
<?php
/**
* AgaviSyslogLoggerAppender allows you to log right into a specific tag of the syslog.
*
* @package agavi
* @subpackage logging
*
* Licensed under the Public Domain
*
* @author Jan Schütze <JanS@DracoBlue.de>
*/
class AgaviSyslogLoggerAppender extends AgaviLoggerAppender
{
function shutdown() {
/*
* Nothing to do here.
*/
}
function write($message) {
if(($layout = $this->getLayout()) === null) {
throw new AgaviLoggingException('No Layout set');
}
$str = sprintf("%s\n", $this->getLayout()->format($message));
openlog($this->getParameter('tag', 'agavi'), LOG_PID | LOG_ODELAY, LOG_USER);
$log_level_mapping = array(
AgaviILogger::FATAL => LOG_ALERT,
AgaviILogger::ERROR => LOG_ERR,
AgaviILogger::WARN => LOG_WARNING,
AgaviILogger::INFO => LOG_INFO,
AgaviILogger::DEBUG => LOG_DEBUG
);
$log_level = LOG_ALERT;
if (isset($log_level_mapping[$message->getLevel()])) {
$log_level = $log_level_mapping[$message->getLevel()];
}
if(syslog($log_level, $str) === false) {
throw new AgaviLoggingException('Cannot write to syslog.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.