Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel customize log formatter

I want to customize the log line format of only the errorlog channel. Is it possible to define it in the config file (config/logging.php), or i have to made a class for it as the doc says? But it shows me no hint..

Can somebody show me some example?

Many thanks in advance!

like image 951
LakiGeri Avatar asked Jan 01 '23 22:01

LakiGeri


1 Answers

The easiest method would be passing your own format to the LineFormatter class:

[
    'driver' => 'monolog',
    'formatter' => Monolog\Formatter\LineFormatter::class,
    'formatter_with' => [
        'format' => "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
    ],
]

You can look at the LineFormatter to see that $format is accepted in the constructor.

You can create your own class implementing the FormatterInterface and look to the ~20 other formatters as examples in the Monolog/Formatter directory.

like image 170
Devon Avatar answered Jan 05 '23 14:01

Devon