Skip to content

Instantly share code, notes, and snippets.

@duongphuhiep
Last active August 14, 2024 11:23
Show Gist options
  • Save duongphuhiep/0546f0eba5e6f74a3f040fe274298dce to your computer and use it in GitHub Desktop.
Save duongphuhiep/0546f0eba5e6f74a3f040fe274298dce to your computer and use it in GitHub Desktop.
Logging best practices

Logging best practices

When generate a log message

  • Major branching points in your code
  • When errors or unexpected values are encountered
  • Any IO operations: calling database / stored proc or make http request
  • Significant domain events
  • Request failures and retries
  • Beginning and end of time-consuming operations

How to choose the log level

Less is more, 5 log levels (Debug, Info, Warning, Error, Fatal) is enough for most applications.

  1. Be generous with your logging but be strict with your logging levels

  2. the "Fatal" level should be reserved for events that you intend to act on (events required support intervention)

  • Support team monitor only the Fatal error logs. We don't care about "Business errors" such as "Product Not Found", "Input validation error" / "Business error returned by external service" etc..
  1. The "Information" level should be reserved for events that would be needed in production (to monitor the state or the correctness of the application).
  • Application start / end, Application config changes
  • Start / End events of any Time-consuming or Important operations. For cheap / unimportant operations such as "GetTransaction", "GetWalletDetails"... which are called 1000 times every sec, we don't need to logs them on production => use “Debug” level in this case.
  1. In almost all other cases the level of your logs level should be "Debug".

  2. If you are indecisive between "Debug" and "Information" => Use "Debug". If you are indecisive between "Debug".

  • if we are indecisive it means that the event might important / interesting (but not important or interesting enough)
  • It is often easy to temporarily activate a lower logs level (Trace, Debug) on Production.
  1. Justify log levels for each release: After deploying to Production, we recognize that some logs is more important, others is less important than we think => adapt them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment