繁体   English   中英

为什么同时进入laravel.log和tweets.log?

[英]Why is it going to both laravel.log and tweets.log?

请考虑以下任务:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring;
use App\Etis\Domain\Services\TwitterService;
use \Twitter;
use Log;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

class FetchTweets extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fetch_tweets';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'fetches the latest ten tweets';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $logInstance = Log::getMonolog();
        $logInstance->pushHandler(new StreamHandler(storage_path('logs/tweets.log'), Logger::INFO));

        $tweets = Twitter::getUserTimeline([
            'screen_name' => env('TWITTER_USER_NAME'),
            'count'       => env('TWITTER_TWEET_AMOUNT'),
            'format'      => 'json'
        ]);

        $logInstance->addInfo('Tweets', [$tweets]);

        $twitterService = new TwitterService();
        $twitterService->processTweets(json_decode($tweets, true));
    }
}

然后将其设置为:

    $schedule->command('fetch_tweets')
        ->everyMinute()
        ->withoutOverlapping()
        ->appendOutputTo('storage/logs/tweets.log');

当我在生产环境甚至本地环境中查看时,都看到laravel.logtweets.log文件都具有我打印到tweets.log的内容。

为什么是这样? 我如何使其打印到tweets.log?

pushHandler()不会替换现有的日志处理程序。 相反,它将新的日志处理程序添加到现有的预定义处理程序列表中。 这就是为什么现在要在2个日志文件中记录您的消息的原因。

您需要调用setHandlers()来覆盖处理程序列表:

$handler = new StreamHandler(storage_path('logs/tweets.log'), Logger::INFO);
$logInstance = Log::getMonolog();
$logInstance->setHandlers(array($handler));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM