覆盖格式日志
Override format log
我需要覆盖使用 Laravel 5.3
制作的项目的格式日志
我以此为指导 post 但没有用。
先创建一个bootstrap/ConfigureLogging.phpclass
<?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 日志的名称。
但是当我尝试为覆盖格式添加代码时,我迷路了,原始 post 上的代码不起作用。
我看到了 Monolog 的代码,我看到我需要发送我的格式,但我不知道。
据我所知,您所指的 link 应该有效。你错过了 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);
}
我需要覆盖使用 Laravel 5.3
制作的项目的格式日志我以此为指导 post 但没有用。
先创建一个bootstrap/ConfigureLogging.phpclass
<?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 日志的名称。 但是当我尝试为覆盖格式添加代码时,我迷路了,原始 post 上的代码不起作用。
我看到了 Monolog 的代码,我看到我需要发送我的格式,但我不知道。
据我所知,您所指的 link 应该有效。你错过了 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);
}