Silex - only log to Monolog ErrorLogHandler

1k views Asked by At

I'd like to log everything using Monolog to webserver (apache's) error log - the same that using error_log() would do.

However, if I set up the logging as follows:

$app->register(new Silex\Provider\MonologServiceProvider(), array(
    'monolog.logfile' => sys_get_temp_dir().'/app.log'
));

$app['monolog'] = $app->share($app->extend('monolog', function($monolog, $app) {
    $monolog->pushHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Logger::WARNING));
    return $monolog;
}));

everything is logged to /tmp/app.log and warning+ to apache error log. The log levels here are not really relevant, I'd like to be able to log to apache log ONLY (mainly for the out-of-the-box support for logrotation).

I guess there are two options:

  • Already using the correct path for MonologServiceProvider monolog.logfile (I could define it with 'php_value error_log' in apache vhost config and then use ini_get('error_log') I suppose, though I'd prefer some other solution which doesn't require me to add extra entries to vhost file)
  • Somehow overriding the original logging config (http://silex.sensiolabs.org/doc/providers/monolog.html#customization does mention changing the handler but doesn't give an example how to)
1

There are 1 answers

0
Tobias Sjösten On BEST ANSWER

Instead of appending your error log handler, how about you overwrite the default handlers using only that one?

$app['monolog'] = $app->share($app->extend('monolog', function($monolog, $app) {
    $monolog-> setHandlers([new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Logger::WARNING)]);
    return $monolog;
}));