Phil Moorhouse
Phil Moorhouse

Reputation: 870

Logging only debug level messages to file in symfony 1.4

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?

Upvotes: 0

Views: 1704

Answers (1)

klob
klob

Reputation: 687

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:

prod:
  logger:
    class:   sfAggregateLogger
    param:
      level:   DEBUG
      loggers:
        sf_file_debug:
          class: myDebugOnlyLoggerClass
          param:
            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.

Upvotes: 2

Related Questions