Logging
Log Levels
Software Development
Debugging
Programming Best Practices

When to use the different log levels

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Logging is a critical aspect of software development. It provides transparency into the application’s runtime behavior and helps in understanding how an application is functioning or why it might be failing. Choosing the correct log level when writing logs can significantly aid in troubleshooting and monitoring without overwhelming the logging system or the operators trying to make sense of the logs. Here, we'll explore when to use various log levels, primarily focusing on the widely accepted log levels: DEBUG, INFO, WARN, ERROR, and FATAL.

Debug

The DEBUG level is used to output detailed informational events useful in debugging an application. These events are mostly used by developers to understand the application flow or to check the values of variables.

When to use:

  • During development to trace the program flow and internal state.
  • To record detailed state information that might be needed to diagnose issues, especially those which are hard to replicate.

Example:

java
logger.debug("Processing transaction with id: " + transactionId);

Info

The INFO level is used to log messages that are informational in nature and highlight the progress of the application at a coarse-grained level.

When to use:

  • For recording execution milestones or important state changes in the application.
  • To trace what actions have been taken, without the detail needed only for debugging.

Example:

java
logger.info("Server started successfully at port: " + serverPort);

Warn

WARN log level indicates a potential issue or a situation that could possibly lead to an error, but one that does not stop the application from functioning.

When to use:

  • When something unexpected happens, but the application can continue running.
  • To record missing, faulty, or suboptimal data where defaults have been used as a fallback.

Example:

java
logger.warn("Configuration file 'settings.conf' not found, using defaults.");

Error

The ERROR level is appropriate for logging errors which might still allow the application to continue running, but which indicate problems that should be addressed.

When to use:

  • When an operation could not be completed as expected due to an error condition, but the application or a critical thread continues to run.
  • For any issue that requires intervention or further investigation to resolve.

Example:

java
logger.error("Failed to process the payment for transaction id " + transactionId);

Fatal

The FATAL log level signifies very severe error events that lead possibly to the termination of the application execution.

When to use:

  • When a critical error has occurred, and the process must be shut down.
  • To log events where a safe continued operation is no longer possible.

Example:

java
logger.fatal("System integrity is at risk. Shutting down.");

Summarizing the Log Levels

Here is a concise table summarizing when to use each log level:

Log LevelUsage ScenarioExample
DEBUGFor detailed debugging and development.logger.debug("Initiating module X.");
INFOFor important runtime events tracking.logger.info("Application launched successfully.");
WARNFor potentially harmful situations.logger.warn("Deprecated API used by a module.");
ERRORFor error events that might allow the application to continue running.logger.error("Module X failed to load.");
FATALFor very severe error events that will presumably lead the application to abort.logger.fatal("Critical services failed.");

Additional Considerations

  • Performance Issues: Logging excessively at a detailed level like DEBUG can lead to performance degradation and large log files.
  • Security and Privacy: Ensure that log entries do not capture sensitive information. Use secure, masked, or tokenized placeholders where necessary.
  • Environment-specific Logging: Sometimes it is advisable to configure log levels differently for different environments; for example, DEBUG might be enabled in a development environment, but only WARN and above enabled in production.

By understanding and intelligently utilizing these log levels, developers can ensure their application logs are helpful, manageable, and appropriate for the scenarios in which they are operating.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.