a SensioLabs Product

The PHP micro-framework
based on the Symfony Components

You are reading the documentation for Silex 2.0. Switch to the documentation for Silex 1.3.

Questions & Feedback

License

Creative Commons License Silex documentation is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.

Using multiple Monolog LoggersΒΆ

Having separate instances of Monolog for different parts of your system is often desirable and allows you to configure them independently, allowing for fine grained control of where your logging goes and in what detail.

This simple example allows you to quickly configure several monolog instances, using the bundled handler, but each with a different channel.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$app['monolog.factory'] = $app->protect(function ($name) use ($app) {
    $log = new $app['monolog.logger.class']($name);
    $log->pushHandler($app['monolog.handler']);

    return $log;
});

foreach (array('auth', 'payments', 'stats') as $channel) {
    $app['monolog.'.$channel] = function ($app) use ($channel) {
        return $app['monolog.factory']($channel);
    };
}

As your application grows, or your logging needs for certain areas of the system become apparent, it should be straightforward to then configure that particular service separately, including your customizations.

1
2
3
4
5
6
7
8
9
use Monolog\Handler\StreamHandler;

$app['monolog.payments'] = function ($app) {
    $log = new $app['monolog.logger.class']('payments');
    $handler = new StreamHandler($app['monolog.payments.logfile'], $app['monolog.payment.level']);
    $log->pushHandler($handler);

    return $log;
};

Alternatively, you could attempt to make the factory more complicated, and rely on some conventions, such as checking for an array of handlers registered with the container with the channel name, defaulting to the bundled handler.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

$app['monolog.factory'] = $app->protect(function ($name) use ($app) {
    $log = new $app['monolog.logger.class']($name);

    $handlers = isset($app['monolog.'.$name.'.handlers'])
        ? $app['monolog.'.$name.'.handlers']
        : array($app['monolog.handler']);

    foreach ($handlers as $handler) {
        $log->pushHandler($handler);
    }

    return $log;
});

$app['monolog.payments.handlers'] = function ($app) {
    return array(
        new StreamHandler(__DIR__.'/../payments.log', Logger::DEBUG),
    );
};
Website powered by Symfony and Twig, deployed on
The Silex logo is © 2010-2017 SensioLabs