0

I am in a project in which I use a custom handler to make the logs of my project. For this I use Monolog, one of the main reasons to use this is to format the message that I keep for the log. So I do something like this in the logging.php

'channels' => [
    'custom' => [
        'driver' => 'stack',
        'tap' => [MyCustomLogger::class],
        'channels' => '[daily]',
    ],
],

And the MyCustomLoggerlooks like this

class MyCustomLogger
{
    public function _invoke($logger)
    {
       //stuff to work with the rotating

      foreach($logger->getHandlers() as $handler){
        $handler->setFormatter(new LineFormatter('TS=%datetime% - APP=my_app - Level=%level% %level_name% - Message=%message%'.PHP_EOL));
      }
  }
}

This is working fine, I send the logs as Log::error("Error message") or Log::info("Info Error") and it prints them correctly on my log file.

What I need now is to change the format of the message according to whether it is error or info (it may be that I will have to do it in the future for debugging too), is there a way to do this inside my custom class?

What I tried was to get the handler level with $handler->getLevel() but the problem is that I always get debug level 100. And yes, that's because I use the daily channel as a base, but if I, for example, put the level => 'error' I always get 400, even though I'm sending it as Log::info. That means changing the level in the logging.php, it doesn't seem like the solution, I need to obtain it in the class to do something like this

if($handler->getLevel() == 400){
 //print error message
}else{
 //print other
}

But I can't get the correct level, which I don't understand, because when %level% passes to the line formatter if it passes it with the correct data, I mean, it has to be there, right? My question is how to get it, or if there is any other way, I would appreciate it.

0 Answers0