流明 5.4 日志信息级别到单独的文件
Lumen 5.4 Log info level to separate file
使用 Lumen Framework 5.4,我试图将 Log::info('etc')
写入一个单独的文件 storage/logs/info.log
。但是,我发现 logs 日志级别信息及以上 的代码进入单独的文件,而我只想将信息日志级别记录到我的自定义文件中。
在bootstrap/app.php
中:
$app->configureMonologUsing(function($monolog) {
$handler = new Monolog\Handler\StreamHandler(storage_path('logs/info.log'), Monolog\Logger::INFO, false);
$handler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true));
$monolog->pushHandler($handler);
return $monolog;
});
如何确保 Lumen 将日志级别信息记录到 storage/logs/info.log
并将所有其他日志级别信息记录到默认日志文件 storage/logs/lumen.log
?
You Can use multiple StreamHandlers
for handling different level of
log.
试试这个:- 它会在 INFO
中记录 info.log
文件,其他记录在 logs.log
文件中
$app->configureMonologUsing(function($monolog) {
$infoHandler = new Monolog\Handler\StreamHandler( storage_path("logs/info.log"), Monolog\Logger::INFO, false);
$noticeHandler = new Monolog\Handler\StreamHandler( storage_path("/logs/logs.log"), Monolog\Logger::NOTICE, false);
$monolog->pushHandler($infoHandler);
$monolog->pushHandler($noticeHandler);
$infoHandler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true));
$noticeHandler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true));
return $monolog;
});
注:-
One note on that snippet, the order you push the streamhandlers on is
important: push the most restrictive on last, so that it is hit first.
Otherwise, say if you pushed the infoHandler on last, it would
log everything above it also and an error wouldn't make it to the
error handler.
所以让你的处理人员提高严重程度
我有一个类似的情况,想要一个单独的日志文件来调用通过嵌入式 iframe 调用完成的我的应用程序(我为另一个站点提供了一些小部件)
发现我使用的是 Lumen 版本 >= 5.6,所以我创建了一个自定义日志通道(通过将 logging.php 添加到 app/config-dir)。
复制每日频道,并连同文件名重命名为“widget”。
来源:Lumen Daily Logs
我在我的控制器中添加了
use Log;
我也在控制器中记录这样的消息:
Log::channel('widget')->info('message from custom channel');
使用 Lumen Framework 5.4,我试图将 Log::info('etc')
写入一个单独的文件 storage/logs/info.log
。但是,我发现 logs 日志级别信息及以上 的代码进入单独的文件,而我只想将信息日志级别记录到我的自定义文件中。
在bootstrap/app.php
中:
$app->configureMonologUsing(function($monolog) {
$handler = new Monolog\Handler\StreamHandler(storage_path('logs/info.log'), Monolog\Logger::INFO, false);
$handler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true));
$monolog->pushHandler($handler);
return $monolog;
});
如何确保 Lumen 将日志级别信息记录到 storage/logs/info.log
并将所有其他日志级别信息记录到默认日志文件 storage/logs/lumen.log
?
You Can use multiple
StreamHandlers
for handling different level of log.
试试这个:- 它会在 INFO
中记录 info.log
文件,其他记录在 logs.log
文件中
$app->configureMonologUsing(function($monolog) {
$infoHandler = new Monolog\Handler\StreamHandler( storage_path("logs/info.log"), Monolog\Logger::INFO, false);
$noticeHandler = new Monolog\Handler\StreamHandler( storage_path("/logs/logs.log"), Monolog\Logger::NOTICE, false);
$monolog->pushHandler($infoHandler);
$monolog->pushHandler($noticeHandler);
$infoHandler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true));
$noticeHandler->setFormatter(new \Monolog\Formatter\LineFormatter(null, null, true));
return $monolog;
});
注:-
One note on that snippet, the order you push the streamhandlers on is important: push the most restrictive on last, so that it is hit first. Otherwise, say if you pushed the infoHandler on last, it would log everything above it also and an error wouldn't make it to the error handler.
所以让你的处理人员提高严重程度
我有一个类似的情况,想要一个单独的日志文件来调用通过嵌入式 iframe 调用完成的我的应用程序(我为另一个站点提供了一些小部件)
发现我使用的是 Lumen 版本 >= 5.6,所以我创建了一个自定义日志通道(通过将 logging.php 添加到 app/config-dir)。
复制每日频道,并连同文件名重命名为“widget”。
来源:Lumen Daily Logs
我在我的控制器中添加了
use Log;
我也在控制器中记录这样的消息:
Log::channel('widget')->info('message from custom channel');