Logging in Python

Python 8 min min read Updated: Mar 09, 2026 Advanced
Logging in Python
Advanced Topic 3 of 8

Logging in Python

Logging is an essential technique used by developers to track events, debug applications, and monitor system behavior. Instead of printing messages using print(), Python provides a built-in logging module that allows developers to record messages with different severity levels.

Logging helps developers diagnose issues, track application activity, and maintain reliable production systems.

What is Logging?

Logging is the process of recording events that occur while a program is running. These events may include information messages, warnings, errors, and debugging details.

Logs are extremely useful in production environments where developers cannot directly see application output.

Logging Levels in Python

The Python logging module supports different log levels to categorize messages.

Log Level Description
DEBUG Detailed information used for debugging
INFO General application events
WARNING Indicates potential problems
ERROR Indicates a serious issue
CRITICAL Indicates a critical system failure

Basic Logging Example

The simplest way to use logging in Python is with the logging module.

python import logging logging.warning("This is a warning message")

This will display a warning message in the console.

Logging with Different Levels

python import logging logging.debug("Debug message") logging.info("Information message") logging.warning("Warning message") logging.error("Error occurred") logging.critical("Critical error")

Each log level provides different information about the application's state.

Configuring Logging

Developers can configure logging behavior using basicConfig().

python import logging logging.basicConfig(level=logging.DEBUG) logging.debug("Debug message visible")

This ensures that debug-level messages are also displayed.

Logging to a File

Logs can also be stored in files for long-term monitoring.

python import logging logging.basicConfig( filename="app.log", level=logging.INFO ) logging.info("Application started")

This saves logs into a file named app.log.

Log Message Formatting

Logging messages can be formatted to include timestamps and log levels.

python import logging logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" ) logging.info("Application started")

This produces structured logs useful for monitoring systems.

Using Logger Objects

Instead of using the root logger, developers can create custom logger instances.

python import logging logger = logging.getLogger("my_app") logger.setLevel(logging.INFO) logger.info("Custom logger message")

This allows different modules to maintain separate logging configurations.

Exception Logging

The logging module can capture exceptions for debugging purposes.

python import logging try: result = 10 / 0 except Exception as e: logging.error("An error occurred", exc_info=True)

This logs the error along with the stack trace.

Real-World Example

Logging is widely used in web applications such as Django and Flask to track system activity.

python import logging logger = logging.getLogger(__name__) def process_request(): logger.info("Processing user request")

This allows developers to monitor application behavior.

Best Practices for Logging

  • Use appropriate log levels
  • Avoid excessive logging in production
  • Store logs in files or monitoring systems
  • Use structured log formats

Logging Tools in Production

Production systems often integrate logging with monitoring tools such as:

  • ELK Stack (Elasticsearch, Logstash, Kibana)
  • Prometheus
  • Grafana
  • Datadog

Conclusion

Logging is a critical feature for debugging and monitoring Python applications. By using Python’s built-in logging module, developers can track application behavior, identify errors, and maintain stable systems.

Understanding logging helps developers build reliable applications and troubleshoot issues in production environments.

In the next tutorial, we will explore Debugging Techniques in Python and learn how to identify and fix issues efficiently.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators