Server-side logging principles

Tips on better logging in your server-side code.

From Java Code Geeks.

  • Try to avoid logging within loops. Loops are usually assumed to be limited, but if there is an infinite loop, with logging (in addition to the computation expense from the rest of the loop logic) the server or an entire cluster can go down since logging will clog up a disk.
  • Especially in multi-threaded environments, the thread name is important to log (and is often automatically prepended). However, typically a thread does multiple operations. Adding some identifier like the class or method caller helps.
  • Similarly, use distributed identifiers in your logging to better troubleshoot and isolate logged behavior. A user ID may be helpful but also get obfuscated with other operations done by the same user. A UUID of a transaction, and/or a specific thread, etc. can further help.
  • When catching external calls, potential for error goes up a lot. Having the request and/or response parameters in the logs (where possible, barring privacy or security concerns) is helpful.