Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Log Levels guidelines #19

Merged
merged 13 commits into from
Apr 12, 2021
Merged

Log Levels guidelines #19

merged 13 commits into from
Apr 12, 2021

Conversation

ktoso
Copy link
Contributor

@ktoso ktoso commented Feb 17, 2021

Work in progress about log level guidelines.

log-levels.md Outdated Show resolved Hide resolved
log-levels.md Show resolved Hide resolved
Copy link
Member

@0xTim 0xTim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comment from an initial read through, feel free to ignore any/all of them!

log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
Copy link
Contributor Author

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks reviews! Will get to it tomorrow I think :)

log-levels.md Outdated Show resolved Hide resolved
Copy link
Contributor

@kmahar kmahar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking good so far 🙂 some minor suggestions

log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
@ktoso ktoso requested review from weissi, tomerd, kmahar and 0xTim March 3, 2021 23:38
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
Co-authored-by: tomer doron <tomerd@apple.com>
log-levels.md Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
Co-authored-by: tomer doron <tomerd@apple.com>
Copy link
Contributor

@weissi weissi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much!

@ktoso ktoso changed the title [WIP] Log Levels guidelines Log Levels guidelines Mar 4, 2021
log-levels.md Outdated Show resolved Hide resolved
Comment on lines +171 to +173
"connection.id": "\(connnection.id)",
"connection.peer": "\(connection.peer)",
"connections.total": "\(connections.count)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to give any guidance on the formatting of metadata keys?

camelCase vs. snake_case vs. snake_case but with dots? prefixes for libraries?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I currently use ahc:request-url (although AHC itself still uses ahc-request-id which was my previous preferred style :P), ie. short library ID then colon, then whatever-the-name-of-this-case-is

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well i could see problems with the colon in some query interfaces. anyway. whatever we decide on, i think we should write this down somewhere, to get some consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what various collector systems survive but a typical "key separator" is . because those can treat things as "well, basically it's json keys". I'll add it as n idea but i don't think we have a strong recommendation here.

log-levels.md Outdated

And a minor yet important hint: avoid inserting newlines and other control characters into log statements (!). Many log aggregation systems assume that a single line in a logged output is specifically "one log statement" which can accidentally break if we log not sanitized, potentially multi-line, strings. This isn't a problem for _all_ log backends, e.g. some will automatically sanitize and form a JSON payload with `{message: "..."}` before emitting it to a backend service collecting the logs, but plain old stream (or file) loggers usually assume that one line equals one log statement - it also makes grepping through logs more reliable.

#### Logging with Correlation IDs / Trace IDs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should go into some detail regarding background/connection loggers for longer running services/clients. I mean the kind of that we inject into

It is worth mentioning that those long running instances in normal circumstances should not be started with a logger that already has a correlation id attached.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is worth mentioning that those long running instances in normal circumstances should not be started with a logger that already has a correlation id attached.

That is specifically the reason swift-distributed-tracing logging context / baggage is designed like it is. Specifically, these are called "top level" logging contexts: https://apple.github.io/swift-distributed-tracing-baggage/docs/current/Baggage/Structs/DefaultLoggingContext.html#/s:7Baggage21DefaultLoggingContextV8topLevel6loggerAC0C06LoggerV_tFZ and they don't contain a baggage, as such, they would not contain correlation IDs.

Really, whenever we talk about any correlation IDs we should be thinking in terms of LoggingContext and tracing APIs. Using "just logger metadata" as carrying these is a workaround rather than the proper way IMHO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can explain this a bit more here, thanks!

Copy link
Member

@0xTim 0xTim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!


- to report errors which the end-user of the library has the means of logging themselfes, e.g. if a database driver fails to fetch all rows of a query, it should not log an error or warning, but instead e.g. error the stream of values (or function, async function, or even the async sequence) that was representing the returned values.
- since the end-user is consuming these values, and has a mean of reporting (or swallowing) this error, the library should not log anything on their behalf.
- never report as warnings which is merely an information, e.g. "weird header detected" may look like a good idea to log as a warning at first sight, however if the "weird header" is simply a misconfigured client (or just a "weird browser") you may be accidentally completely flooding an end-users logs with these "weird header" warnings (!)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to have an additional explanation as to why not to log a warning once, then fall back to debug/trace. (Must always check an atomic)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expanded it a bit

It may be tempting to implement a "log as warning only once" technique for per-request style situations which may be almost important enough to be a warning, but should not be logged repeatedly after all. Authors may think of smart techniques to log a warning only once per "weird header discovered" and later on log the same issue on a different, e.g. trace level... Such techniques result in confusing hard to debug logs, where developers of a system unaware of the stateful nature of the logging would be left confused when trying to reproduce the issue.

  • For example, if a developer spots such warning in a productiuon system, they may attempt to reproduce it thinking that it only happens in the production environment. However if the logging systems' log level choice is sateful they may actually be successfully reproducing the issue but never seeing it manifest. For this, and related performance reasons (as implementing "only once per X" implies growing storage and per-request additional checking requirements), it is not recommended to apply this pattern.

Copy link
Contributor

@kmahar kmahar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made another pass; looking good! mostly some little spelling/grammar/phrasing nits.

log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
log-levels.md Outdated Show resolved Hide resolved
@ktoso
Copy link
Contributor Author

ktoso commented Apr 10, 2021

Awesome, thanks @kmahar, I think I'll go over again and perhaps let's merge it?

@weissi
Copy link
Contributor

weissi commented Apr 10, 2021

Awesome, thanks @kmahar, I think I'll go over again and perhaps let's merge it?

+1

@ktoso
Copy link
Contributor Author

ktoso commented Apr 12, 2021

Okey I think I covered all feedback, happy to keep iterating on this as we go.

Going to merge this as a good starting point though 👍

Thanks for all the feedback!

@ktoso ktoso merged commit 1ff1953 into main Apr 12, 2021
@ktoso ktoso deleted the wip-loglevels branch April 12, 2021 02:25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants