简体   繁体   中英

Laravel Log useFiles method is making Log write in multiple files

I am using Laravel Log Facade in my app. And I have several services like Mandrill, Twilio, Stripe, etc., that need to be logged in separate file. But when I use Log::useFiles() to set separate file for one of the service wrapper class, like this:

Class Mailer
{
    static function init()
    {
        Log::useFiles(storage_path('logs/mandrill-'.date("Y-m-d").'.log'));
    }

    static function send()
    {
        // some code here...

        Log::error("Email not sent");
    }
}

And I am ending up with log being written in both Laravel log file, and this Mandrill log file.

Is there a way to tell Log to write logs only in one file?

It's generally strange that it does that, because when I use directly Monolog , it writes only in one file, as it should. As far as I know Log Facade is using Monolog.

First of all, keep in mind that if you change the log handlers in your Mailer class you'll change them for the whole application.

Secondly, the reason that after your changes you get logs written to 2 files is that useFiles() does not overwrite the default log handler but adds a new handler to the handlers that Monolog will use. Therefore, you just add a second handler to the list and both of them handle the log message by saving them into different files.

Thirdly, Laravel's Log facade does not provide a way to replace the default handler - if you want to use it you need to use Monolog directly. You can access it by calling Log::getMonolog() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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