如何在Silex中为Monolog设置不同的文件

时间:2013-03-27 11:07:48

标签: logging silex channels monolog

主题。我想在不同的文件中记录一些信息,在Symfony 2中搜索很多例子,但是我使用Silex和yaml config。如何设置?

# console logging options
monolog.logfile: '/var/log/daemon.log'
monolog.level: error
monolog.name: 'daemon'

我当前的配置

2 个答案:

答案 0 :(得分:1)

你需要添加一个额外的提供者来使这个像这样(我只是从头开始写,所以,我会工作,我希望):

config.yaml:

monolog.extra.logfile: '/var/log/other.log'
monolog.extra.level: error

ExtendedMonologProvider.php:

<?php
use Silex\ServiceProviderInterface;
use Silex\Application;

use Silex\Provider\MonologServiceProvider;

class ExtendedMonologProvider implements ServiceProviderInterface {
    public function register(Application $app) {
        $app->register(new MonologServiceProvider());

        $app['monolog.extra.handler'] = function () use ($app) {
            return new StreamHandler($app['monolog.extra.logfile'], $app['monolog.extra.level']);
        };

        $app['monolog'] = $app->share($app->extend('monolog', function($log, $app) {
            $log->pushHandler($app['monolog.extra.handler']);
            return $log;
        }));
    }

    public function boot(Application $app) { }
}

答案 1 :(得分:1)

只需在注册提供程序之后扩展monolog并推送新的处理程序,就可以使用任何monolog处理程序或自定义处理程序。

use Silex\Provider\MonologServiceProvider;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

$app->register(new MonologServiceProvider(), [
    'monolog.logfile' => /your/log/path/file.log',
]);

//extend monolog
$app->extend('monolog', function ($monolog, $app) {
    $monolog->pushHandler(new StreamHandler('/your/log/path/other.log', Logger::DEBUG));
    return $monolog;
});