Logging Levels
A logging level is a standard that attaches a grade of "how important is this" to each log line, so that only logs at or above the needed grade are kept depending on the situation.
At a Glance
- While running, a program records "what it did and what went wrong" as logs. But if it records everything, the important lines are hard to find and storage fills up quickly.
- So each log gets a grade, and the volume is controlled by not printing logs below the configured grade.
- Analogy: it is like patient triage in a hospital emergency room.
- DEBUG: detailed notes in the medical record (only the doctor reads them)
- INFO: routine records like "patient checked in" and "treatment completed"
- WARN: "Blood pressure is a little high. Let's keep an eye on it."
- ERROR: "The test equipment is broken. We need to act now."
- FATAL: "Power outage. The emergency room is shutting down."
- During development you see everything down to the detailed notes (DEBUG), and in the real service (production) you see only routine records and above (INFO). The amount of logging is adjusted by changing only the setting, with the same code.
Levels Work Like a Threshold
Levels have an order, and the configured level acts as a threshold: only that level and above are printed.
Low (detailed) ──────────────────────────────────────▶ High (severe)
TRACE DEBUG INFO WARN ERROR FATAL
If setting = INFO:
TRACE DEBUG │ INFO WARN ERROR FATAL
(not printed) │ (printed)- Lowering the threshold (DEBUG) produces more, and more detailed, logs.
- Raising the threshold (ERROR) keeps only serious events.
What Each Level Means, with Examples
TRACE
A level even more detailed than DEBUG, recording every single step in and out of functions. It exists in Java libraries such as Log4j 2 and Logback, but not in Python's built-in logging module. It is turned on briefly only to track down very tricky bugs.
DEBUG
Detailed information developers need when hunting for problems. It records variable values, the raw content of received requests, executed SQL and so on. It is usually turned off in production.
- Example:
Raw cart data: {'items': 3, 'coupon': None}
INFO
Records important events that happened normally. Things like server startup and shutdown, or the start and end of important business processing, are recorded well enough that you can later follow the flow of "what happened."
- Examples:
Server started on port 8000,Starting payment for order #1001
WARN (WARNING)
A situation that is not a problem right now but needs attention. The program keeps running. There are two broad cases.
- There is a clear problem, but the program holds up using a fallback: an external server responded slowly so a cached value was used instead, or it succeeded after retries
- It is fine now but could become a problem later: a production server started in development mode, the admin password is still the default, disk usage passed 85%, or a feature that will soon lose support is being used
The original text said "termination occurs in two cases even at WARN," but WARN is a level that does not stop the program. The intended meaning is closer to "WARN situations fall into two kinds (clear problems and potential problems)." If the program has to stop, it is ERROR or FATAL.
ERROR
A situation where a feature failed and action is needed. That request or task failed, but the program as a whole keeps running and can handle other requests. Usually, ERROR logs are set up to send an alert to the person in charge.
- Examples: failed to connect to the payment server, a DB query failed, an unhandled exception
FATAL (CRITICAL)
A situation where the program as a whole can no longer operate. The program often shuts down shortly after recording this log. Python calls it CRITICAL.
- Examples: cannot connect to the DB at all on startup, a required configuration file is missing, out of memory
Summary Table by Level
| Level | When | Example | In production | Who should look |
|---|---|---|---|---|
| TRACE | Every step of execution | Entering and leaving functions | Off | Developers (special debugging) |
| DEBUG | Detailed info for finding problems | Variable values, raw requests, SQL | Usually off | Developers |
| INFO | Important normal events | Server start, order received | On | Operators (following the flow) |
| WARN | Fine for now, but needs attention | Fell back to cache, disk at 85% | On | Operators (check periodically) |
| ERROR | Feature failed, action needed | Payment failed, exception thrown | On + alert | Person in charge (check quickly) |
| FATAL | Whole program stops | Shutdown because the DB is unreachable | On + urgent alert | Person in charge (respond immediately) |
Names Differ Slightly by Library
| Library | Levels (low → high) | Notes |
|---|---|---|
Python logging | DEBUG(10), INFO(20), WARNING(30), ERROR(40), CRITICAL(50) | No TRACE, default is WARNING |
| Java SLF4J + Logback | TRACE, DEBUG, INFO, WARN, ERROR | No FATAL, the default combination in Spring Boot |
| Java Log4j 2 | TRACE, DEBUG, INFO, WARN, ERROR, FATAL | |
| Linux syslog | debug, info, notice, warning, err, crit, alert, emerg | 8 levels |
Browser, Node.js console | console.debug, console.info, console.warn, console.error | Can be filtered by level in developer tools |
The original text said "the log4j library is usually used," but the old Log4j 1.x reached end of support in 2015, and in late 2021 a severe vulnerability that allowed remote code execution (Log4Shell) was found in Log4j 2. Java today uses SLF4J + Logback (the Spring Boot default) or the latest Log4j 2 with the vulnerability fixed. The concept of levels is the same in every library.
Using Different Levels per Environment
| Environment | Typical setting | Reason |
|---|---|---|
| My computer (development) | DEBUG | You need to see in detail what is happening |
| Test server | DEBUG or INFO | Reproducing problems and checking the flow |
| Production server | INFO or WARN | Reduces log volume and cost, keeps only important events |
Instead of hard-coding the level in the code, make it changeable through environment variables or configuration files. Then, when a problem occurs in production, you can temporarily lower it to DEBUG to find the cause without changing the code.
Tips for Writing Good Logs
- Do not log sensitive information such as passwords, tokens, card numbers or resident registration numbers. Log files are seen by more people than you might think, and they are kept for a long time.
- Include context. "Payment for order #1001 failed (amount 5000 won, reason: timeout)" is far more useful than "Payment failed."
- Log exceptions together with the stack trace. It tells you which line failed. (Python's
log.exception()) - Do not spam INFO inside loops. When processing 10,000 items, rather than writing 10,000 lines, summarize it as "10,000 items processed, 3 failed."
- Choose levels honestly. If trivial things are logged as ERROR, alerts will flood in and you will miss the real ERRORs.
- Make sure the time, the level, and where the log came from appear in the same format. That makes them easy to search and aggregate later.
How to accumulate logs in files and clean up old ones is covered in Log Management.
Getting a Feel Through Code: Changing Levels with Python logging
Output Changes with the Level
Let's run the same code while changing only the threshold level.
# log_levels.py
import logging
import sys
level_name = sys.argv[1] if len(sys.argv) > 1 else "INFO"
logging.basicConfig(
level=getattr(logging, level_name),
format="%(levelname)-8s %(name)s: %(message)s",
)
log = logging.getLogger("order")
log.debug("Raw cart data: %s", {"items": 3, "coupon": None})
log.info("Starting payment for order #1001")
log.warning("Inventory server is slow, using cached stock values")
log.error("Could not connect to the payment server")
log.critical("Cannot connect to the DB, stopping the order service")$ python3 log_levels.py DEBUG
DEBUG order: Raw cart data: {'items': 3, 'coupon': None}
INFO order: Starting payment for order #1001
WARNING order: Inventory server is slow, using cached stock values
ERROR order: Could not connect to the payment server
CRITICAL order: Cannot connect to the DB, stopping the order service
$ python3 log_levels.py INFO
INFO order: Starting payment for order #1001
WARNING order: Inventory server is slow, using cached stock values
ERROR order: Could not connect to the payment server
CRITICAL order: Cannot connect to the DB, stopping the order service
$ python3 log_levels.py WARNING
WARNING order: Inventory server is slow, using cached stock values
ERROR order: Could not connect to the payment server
CRITICAL order: Cannot connect to the DB, stopping the order service
$ python3 log_levels.py ERROR
ERROR order: Could not connect to the payment server
CRITICAL order: Cannot connect to the DB, stopping the order serviceNot a single line of code changed, yet as the threshold goes up, the lower (less important) logs disappear one by one.
- If you pass values separately, as in
log.debug("... %s", value), instead of inserting them into the string right away, the work of building the string is skipped entirely when that level is turned off, which is a bit more efficient. - Giving the logger a name, as in
getLogger("order"), shows at a glance which feature wrote the log.
Level Numbers and the Default Setting
import logging
for name in ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]:
print(name, getattr(logging, name))
logging.warning("If you use it right away without basicConfig")
logging.info("this line will not appear")DEBUG 10
INFO 20
WARNING 30
ERROR 40
CRITICAL 50
WARNING:root:If you use it right away without basicConfigLevels are actually numbers, and the larger the number, the more severe. If you use logging without any configuration, the default threshold is WARNING (30), so INFO logs are not printed. This is a common cause of "I logged it but I can't see it."
Logging Exceptions with the Stack Trace
# log_exc.py
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
log = logging.getLogger("payment")
def pay(amount):
log.info("Payment request: %d won", amount)
try:
return 10000 / amount
except ZeroDivisionError:
log.exception("Failed to calculate payment amount (amount=%d)", amount)
return None
pay(5000)
pay(0)2026-10-04 05:59:46 [INFO] payment - Payment request: 5000 won
2026-10-04 05:59:46 [INFO] payment - Payment request: 0 won
2026-10-04 05:59:46 [ERROR] payment - Failed to calculate payment amount (amount=0)
Traceback (most recent call last):
File "/private/tmp/demo/log_exc.py", line 13, in pay
return 10000 / amount
~~~~~~^~~~~~~~
ZeroDivisionError: division by zerolog.exception() logs at the ERROR level and also records which file, which line, and what exception occurred. Because the time and level appear in the same format, it is easy to pick out only the failures later with grep ERROR.
Interview Questions
Q. What log levels are there, and when is each used? Typically DEBUG, INFO, WARN and ERROR are used (with TRACE and FATAL added depending on the library). DEBUG is detailed information for development, INFO is important normal events, WARN is a situation that is not a problem right now but needs attention, ERROR is a situation where a feature failed and action is needed, and FATAL is a situation where the whole program stops.
Q. What level do you set in production? Usually INFO or WARN. DEBUG produces a large volume of logs, which adds storage cost and performance overhead, and there is also a risk that sensitive values get mixed in. When a problem occurs, you lower it temporarily through configuration to find the cause.
Q. How do you tell WARN and ERROR apart? If the request or task eventually succeeded or was handled through a fallback, it is WARN; if it failed and someone needs to take action, it is ERROR. For example, if an external API call failed once but succeeded on retry, it is WARN; if it ultimately failed, it is ERROR.
Q. What should you be careful about when logging? Do not log sensitive information like passwords and tokens, include identifiers and context so the cause can be found later, and record exceptions together with the stack trace. Levels must not be exaggerated, so that alerts stay meaningful.
Summary
| Concept | One-line summary |
|---|---|
| Logging level | A mechanism that grades logs by importance and keeps only those at or above the threshold |
| Order | TRACE < DEBUG < INFO < WARN < ERROR < FATAL |
| DEBUG | Detailed info for developers, usually off in production |
| INFO | Important normal events, the production default |
| WARN | Holding up but needs attention, the program keeps running |
| ERROR / FATAL | Feature failed, action needed / whole program stops |
| Operating tips | Make the level configurable, never log sensitive info, log exceptions with the stack trace |
