feat(log): Don't write time if logging to journald#4151
Open
Bravo555 wants to merge 1 commit intothin-edge:mainfrom
Open
feat(log): Don't write time if logging to journald#4151Bravo555 wants to merge 1 commit intothin-edge:mainfrom
Bravo555 wants to merge 1 commit intothin-edge:mainfrom
Conversation
Journald already records the timestamp of each log and allows the user to easily customise the format using journalctl's `--output` flag, and to filter the logs using `--since/--until`. So only format time if not logging to journald. Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Proposed changes
Journald already records the timestamp of each log and allows the user to easily customise the format using journalctl's
--outputflag, and to filter the logs using--since/--until. So only format time if not logging to journald.Before:
After:
In this PR, while we use
tracing-journaldto detect if we're logging to journald and change our formatting based on that, we aren't yet fully using its functionality to send structured logs containing log levels and fields, because the default formatting of the journald'sMESSAGEfield (i.e. the log message shown byjournalctlafter the timestamp and identifier) does not format attached fields and the target to be present in the message.Given an event like this:
our current logging to
stderrgives us this:Logging with journald gives us this:
Though field are accessible when selecting a more verbose output level:
Given that this is a change from the current behaviour where in some cases we assume that the field should appear next to the message where it's relevant, which could be surprising to the users, we will still log to stderr and only switch once we can put embed relevant fields into the message.
There are some proposed solutions (tokio-rs/tracing#3474), but they haven't been merged yet.
Still, one nice thing about logging to journald is that information relevant to debugging, like
CODE_FILEandCODE_LINEare attached to the logs by default and can be shown with--output json-pretty/verboseand at the same time they don't have to shown to users by default. With these and other fields it can make our debugging easier while not making logs too noisy for the users.Types of changes
Paste Link to the issue
Checklist
just prepare-devonce)just formatas mentioned in CODING_GUIDELINESjust checkas mentioned in CODING_GUIDELINESFurther comments