zerolog
zerolog copied to clipboard
ConsoleWriter function 'consoleDefaultFormatLevel' should use the level value globals instead of hardcoded strings
Currently, if a user changes the value for particular levels using the globals LevelTraceValue, LevelWarnValue, etc, the console writer doesn't obey these changes when writing the level to the output. It instead compares to hardcoded values "trace", "debug", "info", "warn", "error", "fatal" and "panic". This causes the three letter colorized output to stop working correctly.
For example, if we change the LevelWarnValue from "warn" to "warning", the log outputs "???" instead of "WRN". However the underlying JSON buffer does have the correct value ("warning") because the level String function correctly uses the global LevelWarnValue.
Good catch. PR welcome.
@rocos-jeremy-scott - let me know if you plan on creating a PR for this - I'm happy to give it a shot if you don't have time or whatever.
Cheers @gilcrest, I've just been a bit busy with work commitments but I should be able to sort it out today.
No rush at all - great catch!
@rs
I'm just adding some tests and a couple of the other tests fail due to the global state variables, such as "LevelWarnValue", being shared across test cases.
The test cases that fail for me are named "Write colorized" and "Write colorized fields" because I set the "LevelWarnValue" to "warning" and they are writing "warn" directly into the writers buffer. This causes the "consoleDefaultFormatLevel" function to not match and then fall through to the default case.
How would you like to resolve resetting the global state in tests? I can add a defer func into my tests to reset the global state back to the original values, which essentially boils down to each test is responsible for resetting the state that it changes. Another option would be a tearDown function that each test can call but this would mean that the tearDown function would need to be kept in sync with what global variables are defined. If you have another idea, please let me know.
Defer is preferred. Each test is responsible to restore the env.