I am using the Monolog LineFormatter as follows:
$output = "[%datetime%] %level_name% %channel%: %message%\n";
$stream_handler->setFormatter(new LineFormatter($output));
This results in the following log lines:
[2023-07-03T05:30:31.327443+02:00] DEBUG test: This is a debug message.
[2023-07-03T05:30:31.327555+02:00] INFO test: This is an info level message.
[2023-07-03T05:30:31.327683+02:00] WARNING test: This is a warning level message.
[2023-07-03T05:30:31.327806+02:00] ERROR test: This is an error level message.
How would you set the %level% width (like padding in printf)? I am trying to get this output:
[2023-07-03T05:30:31.327443+02:00] DEBUG test: This is a debug message.
[2023-07-03T05:30:31.327555+02:00] INFO test: This is an info level message.
[2023-07-03T05:30:31.327683+02:00] WARNING test: This is a warning level message.
[2023-07-03T05:30:31.327806+02:00] ERROR test: This is an error level message.
I tried adding "-10s " to the $output %-10s level_name% but this just added the literal text -10s to the log lines.
Thanks in advance.
To set the width (padding) for the
%level_name%placeholder in Monolog'sLineFormatter, you can use the%-10sformat specifier. However, you need to make sure you pass it as a string to theLineFormatterconstructor.Here's the modified code to achieve the desired output:
By using
%-10s, you indicate that%level_name%should be left-aligned with a width of10characters. The padding will be added after the log level name to reach the desired width.