覆盖格式的日志
问题描述:
我需要重写格式的日志上Laravel 5.3覆盖格式的日志
我为导向,这post使用,但不工作做项目。
首先创建一个引导/ ConfigureLogging.php类
<?php
namespace Bootstrap;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger as Monolog;
use Monolog\Formatter\LineFormatter;
use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging as BaseConfigureLogging;
use Monolog\Handler\StreamHandler;
use Carbon\Carbon;
class ConfigureLogging extends BaseConfigureLogging
{
/**
* Configure the Monolog handlers for the application.
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureDailyHandler(Application $app, Writer $log)
{
$config = $app->make('config');
$maxFiles = $config->get('app.log_max_files');
// Formatting
// the default output format is "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
$logFormat = "[%datetime%] [%level_name%]: %message% %context% %extra%\n";
$formatter = new LineFormatter($logFormat);
//$logStreamHandler->setFormatter($formatter); // Here I'm lost
$log->useDailyFiles(
$app->storagePath().'/logs/cprsync.log', is_null($maxFiles) ? 5 : $maxFiles, $config->get('app.log_level', 'debug')
);
}
这种变化(覆盖)为我的应用程序laravel日志的名称。 但是,当我尝试添加代码覆盖格式,我迷路了,并且原始帖子上的代码不起作用。
我看到了Monolog的代码,我发现我需要发送我的格式,但我不知道。
答
你所指的链接应该是我的知识。您缺少pushHandler
protected function configureDailyHandler(Application $app, Writer $log) {
$config = $app->make('config');
$maxFiles = $config->get('app.log_max_files');
$path = $app->storagePath().'/logs/cprsync.log';
// Daily handler
$handler = new RotatingFileHandler($path, is_null($maxFiles) ? 5 : $maxFiles,
$config->get('app.log_level', 'debug'));
// Formatting
$logFormat = "%datetime% [%level_name%] (%channel%): %message% %context% %extra%\n";
$formatter = new LineFormatter($logFormat);
$handler->setFormatter($formatter);
// Push handler
$log->getMonolog()->pushHandler($handler);
}
非常感谢。用你的例子很好地理解推送处理程序的机制。并且工作正常。 – abkrim
那么,我使用'$ formatter = new LineFormatter($ logFormat,null,true,true);'在日志没有特殊信息时删除行尾的额外括号。 – abkrim