douji6896 2016-01-06 08:07
浏览 37
已采纳

为什么要同时使用laravel.log和tweets.log?

Consider the following task:

<?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));
    }
}

Which is then set up as such:

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

When I look, on production and even in local, I see that both the laravel.log and the tweets.log file have the contents that I printing out to tweets.log.

Why is this? How do I make it ONLY print out to tweets.log?

  • 写回答

1条回答 默认 最新

  • doufusi2013 2016-01-06 08:39
    关注

    pushHandler() does not replace existing log handler. Instead, it adds a new log handler to the existing, predefined list of handlers. That's the reason why you're now getting your message logged in 2 log files now.

    You need to call setHandlers() to overwrite the list of handlers:

    $handler = new StreamHandler(storage_path('logs/tweets.log'), Logger::INFO);
    $logInstance = Log::getMonolog();
    $logInstance->setHandlers(array($handler));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行