Skip to content

Instantly share code, notes, and snippets.

@r3code
Last active March 24, 2021 12:15
Show Gist options
  • Save r3code/3be86f6ef23bd3ecadf96c87b3b0d3b5 to your computer and use it in GitHub Desktop.
Save r3code/3be86f6ef23bd3ecadf96c87b3b0d3b5 to your computer and use it in GitHub Desktop.
Structured logging best practices

Structured logging best practices (from NLog fo java)

  • The message logged should be the same every time. It should be a constant string, not a string formatted to contain data values such as ids or quantities. Then it is easy to search for.
  • The message logged should be distinct i.e. not the same as the message produced by an unrelated log statement. Then searching for it does not match unrelated things as well.
  • The message should be a reasonable length i.e. longer than a word, but shorter than an essay.
  • The data should be simple values. Use field values of types e.g. string, int, decimal, DateTimeOffset, or enum types. StructuredLogging.Json does not log hierarchical data, just a flat list of key-value pairs. The values are serialised to string with some simple rules: Nulls are serialised as empty strings. DateTime values (and DateTime?, DateTimeOffset and DateTimeOffset?) are serialised to string in ISO8601 date and time format. Everything else is just serialised with .ToString(). This won't do anything useful for your own types unless you override .ToString(). See the "Code Pitfalls" below.
  • The data fields should have consistent names and values. Much of the utility of Kibana is from collecting logs from multiple systems and searching across them. e.g. if one system logs request failures with a data field StatusCode: 404 and another system with HttpStatusCode: NotFound then it will be much harder to search and aggregate logging data across these systems.

Source https://github.com/justeat/NLog.StructuredLogging.Json#best-practices

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment