在 Lumen 5.2 中配置日志轮换
Configuring log rotate in Lumen 5.2
我尝试在 Lumen 上配置日志轮换,但我卡住了。我知道如何使用 Laravel 执行此操作,但是(又一次)我迷失了 Lumen。
我试图在 app/bootstrap.php
中添加这个,因为我在某处读到它:
$app->configureMonologUsing(function(Monolog\Logger $monolog) use ($app) {
$monolog->pushHandler(
new \Monolog\Handler\RotatingFileHandler($app->storagePath().'/logs/lumen.log', 5)
);
});
而且我可以查看这个错误:
Fatal error: Uncaught Error: Call to a member function error() on null in /x/vendor/laravel/lumen-framework/src/Exceptions/Handler.php:36
Stack trace:
#0 /x/app/Exceptions/Handler.php(36): Laravel\Lumen\Exceptions\Handler->report(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#1 /x/vendor/laravel/lumen-framework/src/Concerns/RegistersExceptionHandlers.php(123): App\Exceptions\Handler->report(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#2 /x/vendor/laravel/lumen-framework/src/Concerns/RegistersExceptionHandlers.php(51): Laravel\Lumen\Application->handleUncaughtException(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#3 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(Object(Error))
#4 {main}
thrown in /x/vendor/laravel/lumen-framework/src/Exceptions/Handler.php on line 36
我不明白这是什么意思,我应该怎么做。
有人在 lumen 5.2 中轮换日志成功了吗?
您忘记返回对象,它应该是:
$app->configureMonologUsing(function(Monolog\Logger $monolog) use ($app) {
return $monolog->pushHandler(
new \Monolog\Handler\RotatingFileHandler($app->storagePath().'/logs/lumen.log', 5)
);
});
另一个想法:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
class LogServiceProvider extends ServiceProvider
{
/**
* Configure logging on boot.
*
* @return void
*/
public function boot()
{
$maxFiles = 7;
$handlers[] = (new RotatingFileHandler(storage_path('logs/lumen.log'), $maxFiles))
->setFormatter(new LineFormatter(null, null, true, true));
$this->app['log']->setHandlers($handlers);
}
/**
* Register the log service.
*
* @return void
*/
public function register()
{
// Log binding already registered in vendor/laravel/lumen-framework/src/Application.php.
}
}
然后不要忘记将服务提供商添加到您的 Lumen bootstrap/app。php:
$app->register(\App\Providers\LogServiceProvider::class);
我尝试在 Lumen 上配置日志轮换,但我卡住了。我知道如何使用 Laravel 执行此操作,但是(又一次)我迷失了 Lumen。
我试图在 app/bootstrap.php
中添加这个,因为我在某处读到它:
$app->configureMonologUsing(function(Monolog\Logger $monolog) use ($app) {
$monolog->pushHandler(
new \Monolog\Handler\RotatingFileHandler($app->storagePath().'/logs/lumen.log', 5)
);
});
而且我可以查看这个错误:
Fatal error: Uncaught Error: Call to a member function error() on null in /x/vendor/laravel/lumen-framework/src/Exceptions/Handler.php:36
Stack trace:
#0 /x/app/Exceptions/Handler.php(36): Laravel\Lumen\Exceptions\Handler->report(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#1 /x/vendor/laravel/lumen-framework/src/Concerns/RegistersExceptionHandlers.php(123): App\Exceptions\Handler->report(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#2 /x/vendor/laravel/lumen-framework/src/Concerns/RegistersExceptionHandlers.php(51): Laravel\Lumen\Application->handleUncaughtException(Object(Symfony\Component\Debug\Exception\FatalThrowableError))
#3 [internal function]: Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}(Object(Error))
#4 {main}
thrown in /x/vendor/laravel/lumen-framework/src/Exceptions/Handler.php on line 36
我不明白这是什么意思,我应该怎么做。
有人在 lumen 5.2 中轮换日志成功了吗?
您忘记返回对象,它应该是:
$app->configureMonologUsing(function(Monolog\Logger $monolog) use ($app) {
return $monolog->pushHandler(
new \Monolog\Handler\RotatingFileHandler($app->storagePath().'/logs/lumen.log', 5)
);
});
另一个想法:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
class LogServiceProvider extends ServiceProvider
{
/**
* Configure logging on boot.
*
* @return void
*/
public function boot()
{
$maxFiles = 7;
$handlers[] = (new RotatingFileHandler(storage_path('logs/lumen.log'), $maxFiles))
->setFormatter(new LineFormatter(null, null, true, true));
$this->app['log']->setHandlers($handlers);
}
/**
* Register the log service.
*
* @return void
*/
public function register()
{
// Log binding already registered in vendor/laravel/lumen-framework/src/Application.php.
}
}
然后不要忘记将服务提供商添加到您的 Lumen bootstrap/app。php:
$app->register(\App\Providers\LogServiceProvider::class);