Logging only debug level messages to file in symfony 1.4

Tag: debugging , logging , symfony1 , production Author: dracoewade Date: 2011-09-14

Most of my app logging is done at debug level because in sf 1.4 it's not used by symfony itself, and that makes it easy to see just the messages I'm interested in using something like:

tail -f log/frontend_dev.php | grep "\[debug\]"

This is great in the dev environment while I'm sat there watching it scroll past, but I now want to log only these debug messages in the production environment.

If I set the log level for the production log to debug, then obviously I'll get everything down to and including the debug level, which is far too much data.

Is it possible to write a logger that will just record [debug] messages and nothing else?

Best Answer

Of course it is possible - you can extend sfFileLogger and override log($message, $priority) function (which sfFileLogger inherits from sfLogger class).

public function log($message, $priority = self::DEBUG)
  if ($this->getLogLevel() != $priority)
    return false;

  return $this->doLog($message, $priority);

Now you have to configure logging in your app to use your new logger class, configuration is located in app/<your app>/config/factories.yml:

    class:   sfAggregateLogger
      level:   DEBUG
          class: myDebugOnlyLoggerClass
            level: DEBUG
            file: %SF_LOG_DIR%/%SF_APP%_%SF_ENVIRONMENT%.log

This logger will save only messages logged with the same priority (and only the same, not the same or higher) as configured in factories.yml.


There's a small typo in there that you might want to fix for future visitors to this page - it needs to be != rather than == but other than that it's exactly what I needed, than you.
Ok, I wrote it in a hurry and didn't properly test the code - now it should be ok, thx!