lumen 日志按天生成文件 - leighj的个人空间 - 开源中国

[lumen 日志按天生成文件 - leighj的个人空间 - 开源中国]

lumen 默认是记录在一个文件中,如果需要更改记录方式,首先

 改配置            --不会这么容易吧,一试果然没什么卵用。

查看一下 class RotatingFileHandler

    /**
     * @param string   $filename
     * @param int      $maxFiles       The maximal amount of files to keep (0 means unlimited)
     * @param int      $level          The minimum logging level at which this handler will be triggered
     * @param Boolean  $bubble         Whether the messages that are handled can bubble up the stack or not
     * @param int|null $filePermission Optional file permissions (default (0644) are only for owner read/write)
     * @param Boolean  $useLocking     Try to lock log file before doing any writes
     */
    public function __construct($filename, $maxFiles = 0, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $useLocking = false)
    {
        $this->filename = $filename;
        $this->maxFiles = (int) $maxFiles;
        $this->nextRotation = new \DateTime('tomorrow');
        $this->filenameFormat = '{filename}-{date}';
        $this->dateFormat = 'Y-m-d';

        parent::__construct($this->getTimedFilename(), $level, $bubble, $filePermission, $useLocking);
    }

$maxFiles 可个性化设置,担心文件过多,可自定义咯!

直接上代码:

提供Providers

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

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

最后在bootstrap/app.php  register

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

完工,生效


Original url: Access

Created at: 2018-10-10 17:18:05

Category: default

Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论