Python - Python Logging Module for Application Monitoring
The Python Logging module is a built-in library that allows developers to record messages about the execution of a program. These messages help monitor application behavior, identify problems, debug errors, and keep a history of important events. Unlike the print() function, which simply displays information on the console, the Logging module provides a structured and configurable way to record messages in different locations such as the console, log files, or remote servers.
Logging is an essential part of software development because applications often run continuously or are used by many people. When an issue occurs, logs provide valuable information about what happened, when it happened, and where the problem occurred. This helps developers quickly diagnose and fix issues without manually reproducing them.
Why Use Logging Instead of print()
Many beginners use the print() function to display information while testing their programs. Although this works for small projects, it becomes difficult to manage as applications grow larger.
Some limitations of print() include:
-
Messages cannot be categorized based on importance.
-
Output disappears when the application closes unless manually saved.
-
Difficult to filter important information.
-
No timestamps or detailed records.
-
Cannot easily save messages to log files.
The Logging module solves these problems by providing organized, configurable, and permanent records.
Example using print():
print("Application started")
print("User logged in")
print("Database connected")
Using logging:
import logging
logging.basicConfig(level=logging.INFO)
logging.info("Application started")
logging.info("User logged in")
logging.info("Database connected")
The logging version automatically includes additional information such as timestamps and message severity.
Components of the Logging Module
The Logging module consists of several important components.
Logger
A logger is the object responsible for generating log messages.
Example:
import logging
logger = logging.getLogger(__name__)
logger.info("Program started")
A project may contain multiple loggers for different modules.
Handler
Handlers determine where log messages should be sent.
Common handlers include:
-
Console Handler
-
File Handler
-
Rotating File Handler
-
Email Handler
-
HTTP Handler
Example:
import logging
logger = logging.getLogger()
file_handler = logging.FileHandler("application.log")
logger.addHandler(file_handler)
Now all log messages will also be written to the file.
Formatter
A formatter controls the appearance of log messages.
Example:
import logging
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.warning("Disk space is low")
Output:
2026-08-01 10:15:20 - WARNING - Disk space is low
Formatters make logs easier to read and analyze.
Log Levels
Every log message belongs to a severity level.
DEBUG
Used while developing software.
Example:
logging.debug("Variable x = 25")
Useful for tracking variable values and program flow.
INFO
Indicates that the application is working normally.
Example:
logging.info("User successfully logged in")
WARNING
Indicates something unexpected happened, but the application can continue.
Example:
logging.warning("Password will expire in 5 days")
ERROR
Indicates an operation failed.
Example:
logging.error("Unable to connect to database")
CRITICAL
Represents very serious errors.
Example:
logging.critical("Application crashed")
Configuring Logging
The simplest configuration uses basicConfig().
Example:
import logging
logging.basicConfig(
level=logging.INFO,
filename="app.log",
filemode="w",
format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.info("Program started")
Explanation:
-
levelspecifies the minimum level to record. -
filenamestores logs in a file. -
filemode="w"overwrites the file each time. -
formatdefines the message layout.
Logging to the Console
If no filename is specified, logs appear on the console.
Example:
import logging
logging.basicConfig(level=logging.INFO)
logging.info("Server started")
logging.warning("Low memory")
Output:
INFO:root:Server started
WARNING:root:Low memory
Logging to a File
Saving logs in a file helps maintain records even after the application exits.
Example:
import logging
logging.basicConfig(
filename="system.log",
level=logging.INFO
)
logging.info("Application launched")
The file system.log will contain:
INFO:root:Application launched
Logging Exceptions
Errors should be logged whenever an exception occurs.
Example:
import logging
logging.basicConfig(level=logging.ERROR)
try:
number = 10 / 0
except Exception:
logging.exception("An error occurred")
Output:
ERROR:root:An error occurred
Traceback (most recent call last):
...
ZeroDivisionError
The complete stack trace helps identify the exact source of the error.
Creating Multiple Loggers
Large applications often have separate loggers for different modules.
Example:
import logging
database_logger = logging.getLogger("Database")
user_logger = logging.getLogger("Users")
database_logger.warning("Database response is slow")
user_logger.info("New user registered")
This keeps logs organized.
Rotating Log Files
If logs grow continuously, they consume disk space.
The RotatingFileHandler automatically creates new log files after reaching a size limit.
Example:
import logging
from logging.handlers import RotatingFileHandler
handler = RotatingFileHandler(
"app.log",
maxBytes=5000,
backupCount=3
)
logger = logging.getLogger()
logger.addHandler(handler)
logger.warning("Testing rotating logs")
Here:
-
Each log file is limited to 5000 bytes.
-
Three backup files are maintained.
-
Older logs are replaced automatically.
Timed Log Rotation
Sometimes log files should rotate daily or weekly.
Example:
from logging.handlers import TimedRotatingFileHandler
handler = TimedRotatingFileHandler(
"app.log",
when="midnight",
interval=1,
backupCount=7
)
This creates a new log every midnight while retaining logs for seven days.
Custom Log Format
Developers can include useful information in each log message.
Example:
import logging
logging.basicConfig(
format="%(asctime)s %(filename)s %(levelname)s %(message)s"
)
logging.info("Application started")
Possible output:
2026-08-01 10:30:15 main.py INFO Application started
This includes:
-
Date
-
Time
-
File name
-
Log level
-
Message
Logging User Activity
Applications often record user actions.
Example:
import logging
logging.basicConfig(level=logging.INFO)
username = "Rahul"
logging.info(f"{username} logged in")
logging.info(f"{username} updated profile")
logging.info(f"{username} logged out")
This creates a history of user operations, which can be useful for auditing and troubleshooting.
Logging Database Operations
Logs help monitor database interactions.
Example:
logging.info("Connecting to database")
logging.info("Executing SELECT query")
logging.info("Connection closed")
If an error occurs, developers can identify the exact stage where it happened.
Logging API Requests
Web applications commonly log incoming API requests.
Example:
logging.info("GET /users")
logging.info("POST /login")
logging.warning("Invalid login attempt")
These logs help analyze application usage and investigate issues.
Best Practices for Logging
-
Use appropriate log levels instead of marking everything as an error.
-
Avoid logging sensitive information such as passwords, PINs, or credit card details.
-
Use meaningful and descriptive log messages.
-
Include timestamps in all logs.
-
Store logs in files for long-running applications.
-
Rotate log files to prevent excessive disk usage.
-
Log exceptions with stack traces using
logging.exception(). -
Use separate loggers for different application modules.
-
Regularly review logs to identify recurring issues and improve application reliability.
Advantages of the Logging Module
-
Built into Python, so no additional installation is required.
-
Helps detect and troubleshoot errors efficiently.
-
Maintains a permanent history of application events.
-
Supports different severity levels for better organization.
-
Can write logs to multiple destinations such as the console and files.
-
Supports automatic log rotation for efficient storage management.
-
Provides customizable formatting for clear and informative log entries.
-
Improves application monitoring, maintenance, and debugging in both development and production environments.
Conclusion
The Python Logging module is a powerful tool for monitoring and maintaining applications. It offers a structured way to record events, warnings, errors, and critical issues, making it much more effective than using print() statements. By utilizing loggers, handlers, formatters, and appropriate log levels, developers can create applications that are easier to debug, monitor, and maintain. Proper logging practices improve software reliability, simplify troubleshooting, and provide valuable insights into application behavior over time, making logging an essential component of professional Python development.