Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: multiple log providers using `configureMonologUsing()`?

I'm using configureMonologUsing() to add in two custom loggers. Doing the standard SOLID principal, I have two providers: ConsoleLoggerProvider and MailLogProvider.

Both of these have a register similar to:

public function register()
{
    app()->configureMonologUsing(function(\Monolog\Logger $monolog) {
        $monolog->pushHandler(new HandlerClass());
    });
}

However, I have noticed over logger will overwrite another logger... How do I stack these?

I've tried to use boot() as well, and that didn't work. I couldn't find any other way to add to the Monolog stack.

Preferable, I want to stack onto Laravel's built-in logger as well.

like image 581
guice Avatar asked Dec 23 '22 16:12

guice


2 Answers

I (finally) found the answer my question:

Within my providers, instead of using configureMonologUsing(), I used Log::getMonolog()->pushHandler([..])

That works! All loggers, including built-in Laravel file logger, are firing. Finally!

(I've honestly been looking for days for a way to add onto the Monolog stack; I apparently wasn't searching by the right terms)

like image 146
guice Avatar answered Dec 28 '22 07:12

guice


According to the Laravel documentation:

You should place a call to the configureMonologUsing method in your bootstrap/app.php file right before the $app variable is returned by the file.

In that case, thus should work for you: create two handler classes and add them to monolog this way (in your bootstrap/app.php):

$app->configureMonologUsing(function ($monolog) {
  $monolog->pushHandler(new EmailLogHandler);
  $monolog->pushHandler(new ConsoleLogHandler); 
}); 
return $app;
like image 44
shalvah Avatar answered Dec 28 '22 06:12

shalvah