0

I tried so much.

 $monolog = Log::getLogger();

        $slackHandler = new \Monolog\Handler\SlackHandler(env("LOG_SLACK_WEBHOOK_URL"), 'translive', null, true, null, \Monolog\Logger::INFO, true, false ,false);
        $monolog->pushHandler($slackHandler);

        $dailyHandlerInfo = new \Monolog\Handler\RotatingFileHandler(storage_path("logs/daily/info/info.log"), 0, \Monolog\Logger::INFO, false, 0664);
        $monolog->pushHandler($dailyHandlerInfo);



        $dailyHandlerError = new \Monolog\Handler\RotatingFileHandler(storage_path("logs/daily/error/error.log"), 0, \Monolog\Logger::ERROR, false, 0664);
        $monolog->pushHandler($dailyHandlerError);

        $dailyHandlerCritical = new \Monolog\Handler\RotatingFileHandler(storage_path("logs/daily/critical/critical.log"), 0, \Monolog\Logger::CRITICAL, false, 0664);
        $monolog->pushHandler($dailyHandlerCritical);

This is what I have in my laravel's provider's register method.

Error is: if I use only slackHandler, it works and sends log to slack. If I use RotatingFIleHandler 3 times as you see(info,error, critical have different pathes) , they work, but the problem is when I write like this (4 of them) , log don't go to slack at all.

Nika Kurashvili
  • 6,006
  • 8
  • 57
  • 123

1 Answers1

0

The $bubble argument passed when initializing the RotatingFileHandler is set to false.

This means that when it handles records, it'll not propagate them anymore. [1]

Set $bubble to true if you'll like records handled in the RotatingFileHandler to be propagated on to the next log handler.

$dailyHandlerInfo = new \Monolog\Handler\RotatingFileHandler(
    storage_path("logs/daily/info/info.log"), 0, \Monolog\Logger::INFO, true, 0664);
$monolog->pushHandler($dailyHandlerInfo);

Update

With regards to your comment about your intent setting $bubble to false in the RotatingFileHandler.

You can still go about maintaining the separation for between handlers and have them log to messages with different severity to separate files by being careful in the order the handlers are registered.

Register the SlackHandler and the top of the stack (i.e. let it come later after registering the RotatingFileHandler).

This way it is called first to handle the record before the others are called.

Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
  • Thank you so much. I knew that, but if I set it to true, then when I fire \Log::error("error message") , it prints this message in my info.log file and also in my error.log file. What i want is log::info should print message in my /info.log file only and log:error should print message in my error.log file even though its hierarchy is higher than info(Error>Info so it prints this message everywhere whose level is below it, but i don't want that.) – Nika Kurashvili Dec 24 '18 at 16:31
  • I see. You can keep the separation for records in separate files by registering the SlackHandler last. Note that handlers are invoked last in first out. See the updated section in my answer. – Oluwafemi Sule Dec 24 '18 at 16:43
  • So what you say is first register RotatingFileHandler 3 times with different file pathes and $buble set to true and after these 3 registering, I should register slackHandler with buble with true? – Nika Kurashvili Dec 24 '18 at 16:44
  • Register like you do in your question with `$bubble` set to `false` and register `SlackHandler` last. – Oluwafemi Sule Dec 24 '18 at 16:45
  • I also use laravel and I wanted not to register slackhandler at all, because I'd have it in logging.php file. like this - 'stack' => [ 'driver' => 'stack', 'channels' => ['slack', 'daily'] ], – Nika Kurashvili Dec 24 '18 at 16:49
  • The bad thing is what really happens is if I use laravel's logging.php file to put slack there, it goes to the list as the first, and then I register my own ones. so when I log, my own ones have buble false and they no longer give it to slack handler. do you understand how tricky it is? – Nika Kurashvili Dec 24 '18 at 16:54
  • If you're using laravel's logging configuration file, have `'slack'` as the last `Monolog` driver in the `channels` array configured for the `stack` driver. – Oluwafemi Sule Dec 24 '18 at 16:59
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185735/discussion-between-nika-khurashvili-and-oluwafemi-sule). – Nika Kurashvili Dec 24 '18 at 17:02
  • It won't work like that because whatever code I posted here gets written in AppServiceProvider and I guess it gets executed after laravel's logging's array gets executed. that's why it doesn't work. – Nika Kurashvili Dec 24 '18 at 17:04