borntyping / rust-simple_logger

A rust logger that prints all messages with a readable output format.
https://crates.io/crates/simple_logger
MIT License
220 stars 48 forks source link

Ensure log levels are appropriately padded even on coloured outputs. #69

Closed etemesi254 closed 1 year ago

etemesi254 commented 1 year ago

Hi,

First of all thanks for the library.

There was an issue when having colored output on Linux(Manjaro Linux with KDE to be specific), where coloured outputs had mis-padded logging

image

The issue came about with how coloring is done on Linux, i.e we add special characters in the command line, e.g printing color red would add \033[39m,

Later on, when you tried to format the message with appropriate padding, for log levels

            let message = format!(
                "{}{:<5} [{}{}] {}",
                timestamp,
                level_string,
                target,...);

The colored log levels would not be padded because the string is longer than 5 characters, hence the misalignment above.

The fix was to simply do the padding before the coloring which fixes that misalignment, now with this pr the printed output looks like

image

This is now in tandem with the no color output which would print with the appropriate padding

Hope it helps.

borntyping commented 1 year ago

Thanks!

This is currently failing to pass the linter, though the suggested diff is horrid enough that I'm going to look at configuring it to allow slightly longer lines before reflowing code instead of changing this MR.