Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 762
Mladen Janjetovic Avatar asked Oct 19 '22 03:10

Mladen Janjetovic


1 Answers

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().

like image 112
jedrzej.kurylo Avatar answered Oct 21 '22 23:10

jedrzej.kurylo