【发布时间】:2016-09-17 11:12:20
【问题描述】:
文档说:
默认情况下,Lumen 配置为为您的应用程序创建每日日志文件,这些文件存储在 storage/logs 目录中。
但我的应用程序仍然会生成一个没有每日日志的 lumen.log。
我的版本:Laravel Framework 版本 Lumen (5.1.6) (Laravel Components 5.1.*) 我来自 5.1 安装。
如何生成带有日常文件的日志?
【问题讨论】:
文档说:
默认情况下,Lumen 配置为为您的应用程序创建每日日志文件,这些文件存储在 storage/logs 目录中。
但我的应用程序仍然会生成一个没有每日日志的 lumen.log。
我的版本:Laravel Framework 版本 Lumen (5.1.6) (Laravel Components 5.1.*) 我来自 5.1 安装。
如何生成带有日常文件的日志?
【问题讨论】:
因为this commit 有一个configureMonologUsing 方法。你应该在你的 bootstrap/app.php 文件中调用这个方法
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
$app->configureMonologUsing(function ($monolog) {
$maxFiles = 7;
$rotatingLogHandler = (new RotatingFileHandler(storage_path('logs/lumen.log'), $maxFiles))
->setFormatter(new LineFormatter(null, null, true, true));
$monolog->setHandlers([$rotatingLogHandler]);
return $monolog;
});
您可以创建一个服务提供者,它创建一个新的轮换日志处理程序,然后替换 Monolog 处理程序。
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
class LogServiceProvider extends ServiceProvider
{
public function boot()
{
app('Psr\Log\LoggerInterface')->setHandlers([$this->getRotatingLogHandler()]);
}
public function getRotatingLogHandler($maxFiles = 7)
{
return (new RotatingFileHandler(storage_path('logs/lumen.log'), $maxFiles))
->setFormatter(new LineFormatter(null, null, true, true));
}
public function register()
{
}
}
您还可以扩展 Application 并替换 getMonologHandler 或 registerLogBindings 方法。以下是替换前者的示例。
在 bootstrap/start.php 中替换
// This
$app = new Laravel\Lumen\Application(
realpath(__DIR__.'/../')
);
// With this
$app = new App\Application(
realpath(__DIR__.'/../')
);
并创建 App\Application.php
<?php
namespace App;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Laravel\Lumen\Application as LumenApplication;
class Application extends LumenApplication
{
/**
* {@inheritdoc}
*/
protected function getMonologHandler()
{
$maxRotatedFiles = 3
return (new RotatingFileHandler(storage_path('logs/lumen.log'), $maxRotatedFiles))
->setFormatter(new LineFormatter(null, null, true, true));
}
}
【讨论】: