Logging in Back-End Services
Understand the importance of adding logs to a server. Learn when and how to add logs.
Logging is an essential aspect of building robust back-end systems. Our logs are critical in helping us debug our applications in real time to understand how they’re performing, triage any issues as soon as they come up, and do so efficiently. To be able to do so, however, we need to be intelligent and diligent with our logging.
What are log levels?
We typically categorize logs into the following four levels of verbosity or criticality:
Debug: These logs are essentially dev environment specific and are often not useful past the initial code-writing stage.
Info: These logs provide us with valuable information that can help keep us apprised of the exact workings of our system, so we can be sure that everything is working okay. It can also be used to log some metrics on occasion. Be wary not to be greedy and add too many logs because logging is an input and output (I/O) operation and uses considerable resources over time.
Warning: These logs are often used to print anomalies that might merit an investigation but aren’t an immediate cause for concern.
Error: These logs indicate something went wrong and warranted further investigation. They typically print the error, trace, and circumstances around the error.
We can set the log level we want to use in different scenarios; only logs of that level and above will be printed. Mostly, we will be able to get different files for varying levels of logs. This helps us go through error logs faster and rotate out the more verbose info logs for archival to save disk space. We certainly want to set a different log level for our production environment than our local dev setup to avoid getting spammed by junk information in production and filling up our disk space too fast.
Standard log levels also include Trace and Fatal, which are uncommonly used. Please take a look at the following diagram for a better decision-making process:
Get hands-on with 1400+ tech skills courses.