Ask your Symfony questions! Pay money and get answers fast! (more info)

How to log to syslog? Symfony

  • SOLVED

Is there a way to get the Symfony builtin logger to log to a server's syslog? The server is running linux and apache.

Answers (4)

2010-05-10

michalg answers:

Hello,

Try this, adapt openlog to your needs. Look at http://www.php.net/manual/en/function.openlog.php
In this example, it's logging to /var/log/message (because of LOG_KERN in openlog).

I didn't translated priority codes because they are same i think. But maybe it would be nice to have some translation.

sfSysLogLogger.php

class sfSysLogLogger extends sfLogger {

public function initialize(sfEventDispatcher $dispatcher, $options = array())
{
openlog("myScriptLog", LOG_PID | LOG_PERROR, LOG_KERN);

return parent::initialize($dispatcher, $options);
}

/**
* Logs a message.
*
* @param string $message Message
* @param string $priority Message priority
*/
protected function doLog($message, $priority)
{
syslog($priority, $message);
}

/**
* Executes the shutdown method.
*/
public function shutdown()
{
closelog();
}
}


Save it for example in your project lib/ directory. Edit factories like Sergey suggested.


michalg comments:

Here's example of factories.yml

all:
[..]
logger:
class: sfAggregateLogger
param:
level: debug
loggers:
sf_web_debug:
class: sfWebDebugLogger
param:
level: debug
condition: %SF_WEB_DEBUG%
xdebug_logging: false
web_debug_class: sfWebDebug
sf_syslog_debug:
class: sfSysLogLogger
param:
level: debug

2010-05-10

Alex Filatov answers:

http://trac.symfony-project.org/browser/trunk/lib/symfony/log/Log/syslog.class.php?rev=1

2010-05-10

Sergey Kushniruk answers:

Adapt this class to your needs:
http://trac.symfony-project.org/browser/trunk/lib/log/sfLog/syslog.class.php?rev=1415

…and replace the default logger with it in factories.yml (more info @ http://www.symfony-project.org/reference/1_4/en/05-Factories#chapter_05_logger)

2010-05-11

GlobalOrangeLab answers:

Adapt this class to your needs:
http://trac.symfony-project.org/browser/trunk/lib/log/sfLog/syslog.class.php?rev=1415

and replace the default logger with it in factories.yml as below

all:

[..]

logger:

class: sfAggregateLogger

param:

level: debug

loggers:

sf_web_debug:

class: sfWebDebugLogger

param:

level: debug

condition: %SF_WEB_DEBUG%

xdebug_logging: false

web_debug_class: sfWebDebug

sf_syslog_debug:

class: sfLog_syslog

param:

level: debug