Ask your Symfony questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Is there a hook for the event of writing to the log? Symfony


I'd like to have an email sent to myself every time a certain phrase appears in the log file. There are 3 things I can do, which I do not want to do:

1.) Add an if() statement, checking for the phrase, to every place where I write to the log.

2.) Refactor the code so that all writes to the log file go through a single method. Add an if() statement checking for the phrase there.

3.) Create a cron script that checks the log file once an hour and sends me a notification if it finds the phrase in the last few lines, or maybe parse the lines, figure out the dates and times of each line, and send then check just the lines written in the last hour.

I will eventually get around to #2, but for now what I'd really like is to simply have a function that is triggered automatically whenever anything that gets written to the log. Is there a hook or event call for writes to the log? Anything I could hook a filter to?

Answers (2)


Daniel Lohse answers:

Try listening to the application.log event. This is used by the symfony core classes to write to the log (like when registering routes and so on).

Here's the code, preferably put in your ProjectConfiguration's setup method:

array($this, 'listenToLogEvent'));

Then add the listenToLogEvent method to the same class and you get notified when symfony writes something to the log. :) Then you can send your email.

Lawrence Krubner comments:

This is perfect. Thanks, Daniel.


Szabolcs Heilig answers:

What version of Symfony do you use for that project? As of symfony 1.1 you can configure what logger implementation you want to use. You can configure that in your app's factories.yml. So if you are using symfony 1.1 or later you can create your own logger (based in sfFileLogger) and configure your frontend production environment to use it.

An example of your logger class:


class myCustomLogger extends sfFileLogger
protected function doLog($message, $priority)
if (<your phrase finding expression>)
// sending message by mail

// call sfFileLogger::doLog() to log to logfile too

Lawrence Krubner comments:

Sorry, I should have said that sooner:

Symfony 1.2.5

Running on a Ubuntu server.