Log Levels Explained: Debug to Critical
Every log message answers two questions: what happened, and how much does it matter? The second question is handled by the log level. Levels let a program label each message with a severity, so that operators can decide which messages to keep, display or act on.
Used well, levels make logs quiet during normal operation and informative during trouble. Used badly, they produce a wall of meaningless noise or hide real problems. This guide explains the common levels, how to pick between them and how to configure them sensibly.
Why Levels Exist
A busy application might produce thousands of messages per minute. Most are routine details that nobody needs unless something breaks. Levels allow you to control the volume without changing code. In production you may record only warnings and above, then temporarily lower the threshold to debug while chasing a specific bug.
They also drive alerting. An error level message might open a ticket, while a critical one might page someone. If levels are chosen carelessly, those automatic reactions become unreliable.
The Common Levels
Different libraries use slightly different names, but a typical ordered set looks like this, from most detailed to most severe.
Trace
The finest detail, often every step inside a function or each value processed. It is extremely verbose and normally disabled except in short, focused debugging sessions. Not every system includes this level.
Debug
Information useful to developers while diagnosing a problem: variable values, branch decisions, the content of requests and the results of internal calculations. Debug logging helps understand behavior, but it is usually off in production because of volume and the risk of exposing sensitive data.
Info
Normal, expected events that describe the application's progress: a service started, a configuration was loaded, a scheduled job finished, a user completed a key action. Info messages tell the story of a healthy system. Keep them meaningful and not too frequent.
Warning
Something unexpected or undesirable happened, but the application can continue. Examples include a retry after a temporary failure, use of a deprecated feature, a disk nearing its limit or a slow response from a dependency. Warnings suggest that attention may be needed soon, even though nothing is broken yet.
Error
An operation failed. The application as a whole is still running, but a specific request, job or task could not be completed. Examples: a database query failed, a payment was declined due to a system fault, or a file could not be written. Errors normally need investigation.
Critical (or Fatal)
A severe failure that threatens the whole application or system. The program may be unable to continue, or a vital component is down. Examples include loss of the main database connection, running out of memory, or corrupted essential data. Critical events usually warrant immediate human attention.
Some systems, such as syslog, add further levels like notice, alert and emergency. The idea is the same: higher means more urgent.
Choosing the Right Level
When deciding, ask what the reader should do. A useful rule of thumb:
- Debug: nobody should need to read this unless investigating a problem.
- Info: no action needed, but it helps to know it happened.
- Warning: no action now, but it may need attention if it continues.
- Error: something failed and someone should look into it.
- Critical: act immediately.
Consider a failed login. One wrong password is routine and fits info or even debug. Hundreds from one address in a minute deserve a warning or higher because they suggest an attack. A failure to reach the authentication database is an error, since nobody can log in.
Common Mistakes
- Logging expected conditions as errors. If a user searches for something that does not exist, the system worked correctly. Logging an error trains people to ignore the level.
- Logging everything as info. When all messages share one level, filtering is useless.
- Using debug in production permanently. It inflates storage, slows the application and can leak secrets.
- Logging the same failure repeatedly. A loop that writes an error thousands of times a second can fill a disk. Log once, count the rest or rate limit.
- Vague messages. "Something went wrong" at error level does not help. Include what was attempted, which resource and why it failed.
- Missing context. Add identifiers such as request ID so you can connect a message to the wider event.
Configuring Levels in Practice
Most logging libraries let you set a minimum level. Messages below it are discarded cheaply. A typical approach is:
import logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger("orders")
log.debug("Cart contents: %s", cart)
log.info("Order %s created", order_id)
log.warning("Payment retry %d for order %s", attempt, order_id)
log.error("Could not save order %s", order_id)
With the level set to info, the debug line is skipped. Different modules can have different thresholds, so you can raise detail for one component without flooding everything.
Make the level configurable through an environment variable or configuration file rather than a code change. This lets operators adjust it quickly during an incident, and then return it to normal afterward.
Protect Sensitive Data at Every Level
Higher detail should never mean exposing secrets. Do not log passwords, tokens, full payment numbers or private personal details at any level, including debug. If you must record something for diagnosis, mask it. Assume logs are copied and stored widely.
Review Regularly
Levels drift over time. Periodically look at which messages fire most often at each level. If a warning appears constantly and nobody cares, lower it or fix its cause. If a real failure only logged at info slipped past unnoticed, raise it. Treat the level choice as part of the code review.
Frequently Asked Questions
What level should I use in production?
Info is a common default, giving a readable record of normal activity plus all problems. Some high-volume services choose warning to reduce cost. Whatever you pick, make sure you can raise the detail temporarily without redeploying, so you can investigate issues when they appear.
What is the difference between error and critical?
An error means a particular operation failed while the application continues serving others. Critical means a broader failure that may stop the application or a vital part of it. If you would wake someone up for it, it is probably critical.
Should warnings trigger alerts?
Usually not individually. A single warning is often harmless. Instead, alert when warnings occur at an unusual rate or when a specific warning indicates an approaching limit. Alerting on every warning tends to create noise and cause people to ignore notifications.