zerolog icon indicating copy to clipboard operation
zerolog copied to clipboard

ConsoleWriter function 'consoleDefaultFormatLevel' should use the level value globals instead of hardcoded strings

Open rocos-jeremy-scott opened this issue 4 years ago • 6 comments

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.

rocos-jeremy-scott avatar May 25 '21 00:05 rocos-jeremy-scott

Good catch. PR welcome.

rs avatar May 25 '21 02:05 rs

@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.

gilcrest avatar May 28 '21 01:05 gilcrest

Cheers @gilcrest, I've just been a bit busy with work commitments but I should be able to sort it out today.

rocos-jeremy-scott avatar May 28 '21 02:05 rocos-jeremy-scott

No rush at all - great catch!

gilcrest avatar May 28 '21 03:05 gilcrest

@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.

rocos-jeremy-scott avatar May 28 '21 03:05 rocos-jeremy-scott

Defer is preferred. Each test is responsible to restore the env.

rs avatar May 28 '21 10:05 rs