Lumen Daily Logs

独自空忆成欢 提交于 2019-12-02 04:59:57

问题


I want to add to my Lumen project a daily Log.

I try this in the app.php (Folder Bootstrap/)

$logFile = 'laravel.log';

Log::useDailyFiles(storage_path().'/logs/'.$logFile);

But this set me that error

Call to undefined method Monolog\logger::useDailyFiles()

Any help I appreciate...Thanks


回答1:


If you look at the framework source code here you can see that it will not do daily logs, but rather write to a single log file lumen.log. There is a public method available configureMonologUsing seen here and referenced here that you can use to override the default behavior without extending the Application.

Lumen just sets a handler to monolog, so another good solution is you could do this:

<?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 = 5;

        $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.
    }
}

Then don't forget to add the service provider to your Lumen bootstrap/app.php:

$app->register(\App\Providers\LogServiceProvider::class);



回答2:


In Lumen 5.6 better way is to configure your default setting in .env as LOG_CHANNEL=daily

By default the setting is LOG_CHANNEL=stack which use single file for logging.



来源:https://stackoverflow.com/questions/32321785/lumen-daily-logs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!