Laravel5.1 将终端执行的日志输出到其他文件

目前由于在我们的测试环境的项目里面,为了方便,laravel的schedule命令是由root用户运行的,这就会导致较早运行的命令生成的日志文件的owner是root,但是http请求是由apache用户接管的,导致http请求生成的日志无法写到日志文件中

我做了如下的修改,让schedule或者手动执行的命令产生的日志转到指定的日志文件中,http请求的日志还是在原来的日志文件中,两处来的日志互不干扰。

增加文件app/Console/Bootstrap/ConfigureConsoleLogging.php:

<?php

/**
 * This file is part of project alg-shopping.
 *
 * Author: Jake
 * Create: 2019-03-15 10:51:21
 */

namespace App\Console\Bootstrap;

use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Foundation\Bootstrap\ConfigureLogging;

class ConfigureConsoleLogging extends ConfigureLogging
{
    /**
     * Configure the Monolog handlers for the application.
     *
     * @param  \Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application  $app
     * @param  \Illuminate\Log\Writer  $log
     * @return void
     */
    protected function configureDailyHandler(Application $app, Writer $log)
    {
        $log->useDailyFiles(
            $app->storagePath().'/logs/console.log',
            $app->make('config')->get('app.log_max_files', 10)
        );
    }
}

app/Console/Kernel.php中增加如下方法:

   public function bootstrap()
    {
        $this->app->bind(
            'Illuminate\Foundation\Bootstrap\ConfigureLogging',
            'App\Console\Bootstrap\ConfigureConsoleLogging'
        );

        parent::bootstrap();
    }

这样终端下产生的日志会写入到storage/logs/console-xxxx-xx-xx.log中,而原来的api请求的日志则还是写入到原来的storage/logs/laravel-xxxx-xx-xx.log中,互不干扰。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注