Search code examples
symfonymonolog

How do I set the format of log timestamp within Monolog on Symfony framework


I will premise this with the fact that I am new to Symfony but have been using Laravel for some years.

Is it possible to change the format of the log timestamp string through configuration in Symfony (I'm sure it is). I am getting log files out that look like this…

[2022-10-18T09:11:04.228289+00:00] app.DEBUG: a message [] []

I would like to format them like this…

[2022-10-18 09:11:04] app.DEBUG: a message [] []

I am not sure if this is the relevant setting, I suspect it is part of it, but this is the LineFormatter class within Monolog

class LineFormatter extends NormalizerFormatter
{
    public const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";

I assume this bit is formatting the timestamp [%datetime%] and is being pulled from a system wide configuration. If this is the case, does anyone know how I can override it?

Using Symfony 6 btw

Thanks in advance

Gary


Solution

  • Welcome on stackoverflow Gary.

    Monolog is a library. Symfony imports libraries via a Bundle. You can configure them via the yaml files in the config subdirectory.

    To know which options exists, you have three solutions :

    1. Google method. Search the bundle documentation, but in your case, no words about it.
    2. Brutus method: put any value to get an error message. You should see that an option like use_milliseconds exists (I cannot try it without my dev computer), but I already use the Brutus method :)
    # config/monolog.yaml
    monolog:
        foo: bar
    
    1. Horatio Caine method: look in the source of monolog bundle, there is always a file describing the available options. In your case, I found solution in the `monolog.xml file :
    # vendor/symfony/monolog-bundle/Resources/config/monolog.xml
    <?xml version="1.0" ?>
    
    <container xmlns="http://symfony.com/schema/dic/services"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    
        <services>
            <service id="monolog.logger" parent="monolog.logger_prototype" public="false">
                <argument index="0">app</argument>
                <call method="useMicrosecondTimestamps">
                    <argument>%monolog.use_microseconds%</argument>
                </call>
            </service>
    ...
    

    I cannot try it on my phone, but it should solve your problem:

    # config/monolog.yaml
    monolog:
        use_microseconds: false
    

    edit: Since I found this use_microseconds config options, I just find on a old documentation an article about it: https://symfony.com/doc/3.3/logging/disable_microsecond_precision.html

    If you want to remove UTC, you could create your own Formatter. The handler uses a Formatter to format the record before logging it. All Monolog handlers use an instance of Monolog\Formatter\LineFormatter by default but you can replace it. Your new formatter must implement Monolog\Formatter\FormatterInterface. Here is the doc with a sample.

    You can extends the LineFormatter and overload the constructor to use your format instead of the default one.