What Is a Logging Level
A log level or log severity is a piece of information telling how important a given log message is. It is a simple, yet very powerful way of distinguishing log events from each other. If the log levels are used properly in your application all you need is to look at the severity first. It will tell you if you can continue sleeping during the on-call night or if you need to jump out of bed right away and hit another personal best in running between your bedroom and laptop in the living room.
You can think of the log levels as a way to filter the critical information about your system state and the one that is purely informative. The log levels can help to reduce the information noise and alert fatigue.
Log Level Hierarchy: What Are the Most Common Logging Levels & How to Choose Them
In most logging frameworks you will encounter all or some of the following log levels:
- TRACE
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
The names of some of those give you a hint on what they are about. However, let’s discuss each of them in greater detail.
TRACE — the most fine-grained information only used in rare cases where you need the full visibility of what is happening in your application and inside the third-party libraries that you use. You can expect the TRACE logging level to be very verbose. You can use it for example to annotate each step in the algorithm or each individual query with parameters in your code.
DEBUG — less granular compared to the TRACE level, but it is more than you will need in everyday use. The DEBUG log level should be used for information that may be needed for diagnosing issues and troubleshooting or when running application in the test environment for the purpose of making sure everything is running correctly
INFO — the standard log level indicating that something happened, the application entered a certain state, etc. For example, a controller of your authorization API may include an INFO log level with information on which user requested authorization if the authorization was successful or not. The information logged using the INFO log level should be purely informative and not looking into them on a regular basis shouldn’t result in missing any important information.
WARN — the log level that indicates that something unexpected happened in the application, a problem, or a situation that might disturb one of the processes. But that doesn’t mean that the application failed. The WARN level should be used in situations that are unexpected, but the code can continue the work. For example, a parsing error occurred that resulted in a certain document not being processed.
ERROR — the log level that should be used when the application hits an issue preventing one or more functionalities from properly functioning. The ERROR log level can be used when one of the payment systems is not available, but there is still the option to check out the basket in the e-commerce application or when your social media logging option is not working for some reason.
FATAL — the log level that tells that the application encountered an event or entered a state in which one of the crucial business functionality is no longer working. A FATAL log level may be used when the application is not able to connect to a crucial data store like a database or all the payment systems are not available and users can’t checkout their baskets in your e-commerce.
How Do Log Levels Work
Well, on their own they don’t work at all. They are labels that serve an informational purpose. They are there, in the log message, to provide information. The log levels say how important a given event is. Should you jump out of your cozy bed right away to fight a critical issue, or maybe you can just leave it till morning and the world will not end for a few hours once the patch is ready and deployed?
When combined with a logging framework you can easily limit the information that is written to the log destination of your choice. When setting the log level that you are interested in your logging framework you effectively limit all less important log levels to be ignored. So if you set your logging framework to have the root logging level to WARN you will only get log events with WARN, ERROR, and FATAL levels: