feat(trace): Log output readability and consistency #583
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The motivation of this change is to improve the readability of the logs without introducing a new formatter or breaking our existing compatibility with the logfmt standard.
Timestamp
Currently our log messages have the timestamp at the end of the line. This means the timestamp is in a different position for every log message, making it difficult to find when you're quickly skimming the logs.
This PR moves the timestamp to the beginning of the line. This is typically where loggers display the time, so it's what most people will be used to.
Putting fixed-size items at the beginning of the line improves the alignment of the log contents, which makes the logs more organized and consistent. This helps the logs look a little more like a table instead of randomly scattered text.
It's typical to look at the timestamp for multiple messages at once, to understand how long certain processes are taking. By keeping the timestamp in a consistent location, this process is easier.
Warn
I also changed "warning" messages to be printed as "warn". This also has multiple benefits:
Refactor
I refactored the
writeLog
andcountLog
functions to extract out the common logic into a single function. This makes it easier to keep the two functions consistent with each other.Other ideas
I was also tempted to pad out the log-level with spaces to ensure that it always uses the a consistent number of characters, thus perfectly aligning the next field (scope). All log levels are 4 or 5 characters, so this would mean adding an extra space whenever the log level is 4 characters. I didn't implement this because I'm not sure if it deviates from the logfmt standard. Probably not, but I'm playing it safe since it's not a huge benefit either.