Python logging is an in-built module by which programmers can log messages regarding what their program is doing. Storing these messages (or logs) makes it easier to observe how the program works and any issues it has.
How you format these logs is important because it affects how the messages are organized and presented. This makes them easy to read, search, and analyze. Well-formatted logs give meaning to raw messages, particularly for debugging and monitoring.
In this article, we’ll look at the Python logging format. You’ll understand some logging formats in Python, how to configure them, some best practices to follow, and how you can use Middleware to enhance these logs for observability.
If you want to understand the basics, this article on what Python logging is explains how it works in simple terms.
What is Logging Format in Python?
Python logging format defines how each log message looks and what details it shows. This includes the time, log level, and the actual message. It helps make your logs clear and easy to read. Python has a built-in logging module that provides a deafult format. It uses the default configuration without customizing anything.
Example:
import logging
logging.warning("This is a warning")
Output:
WARNING:root:This is a warning
- WARNING is the log level.
- root is the default logger name.
- “This is a warning” is the actual log message.
You can customize the Python log format using placeholders in the format string. Here are some of the placeholders for python logging format string:
Placeholder | Description | Example |
%(asctime)s | Timestamp | 2025-04-23 18:30:45, 123 |
%(levelname)s | Log level (DEBUG, INFO, ERROR, WARNING, etc.) | INFO |
%(message)s | The actual log message | User logged in successfully |
%(name)s | Name of the logger | auth |
%(filename)s | Source file name | app.py |
%(lineno)d | Line number in the file | 20 |
%(funcName)s | The function where logging was called. | user_auth |
Python Logging Format Example: A log format in Python might look like this:
"%(asctime)s - %(levelname)s - %(name)s - %(message)s"
This shows the time of the log, the log level, the name of the logger, and the message itself.
Output:
2025-04-23 10:35:42,789 - INFO - auth - User login successful
Why Custom Log Formats Matter
Customizing the log format helps make logs clearer, more consistent, and easier to analyze. Here are more reasons why you should customize log formats:
- Debugging: Good logs can save you so much time when something breaks. Having timestamps and line numbers makes it easy to find and fix issues quickly.
- Monitoring: Once your app is live, you will need to keep an eye on how it is running. Having a structured log format like JSON makes tools like Middleware or Datadog capable of reading and understanding your logs in real time.
- Auditing: For auditing, logs help you keep track of who did what and when. Custom formats let you include important details, which makes it easier to review what happened later, especially for security checks.
How to Configure Log Format in Python
Python gives you a few ways to set up how your logs look, depending on what you need. You can start with something simple using basicConfig()
, or go further by creating custom formats or using libraries like python-json-logger
to make your logs more organized.
Basic Configuration
The simplest method of applying log formatting in Python is through the basicConfig()
function. It enables you to define the level, how your python logging messages format will look, and where they will be displayed, e.g., in the terminal or on a file.
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.info("User logged in successfully.")
Output:
2025-04-24 10:31:30,265 - INFO - User logged in successfully.
basicConfig()
is great for quick setups or small projects where you want clean logs with minimal effort.
Custom Formatting with logging.Formatter
If the default log format isn’t enough, you can set up your own using logging.Formatter
. This way, you can decide exactly how your logs should look, making them clearer and easier to work with.
Example:
import logging
# Create a logger
logger = logging.getLogger("my_app")
logger.setLevel(logging.DEBUG)
# Clear existing handlers (optional, avoids duplicates)
logger.handlers.clear()
# Create a console handler
console_handler = logging.StreamHandler()
# Define and set formatter with clean timestamp
formatter = logging.Formatter(
fmt="%(asctime)s - %(levelname)s - %(name)s - %(lineno)d - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S"
)
console_handler.setFormatter(formatter)
# Add handler to logger
logger.addHandler(console_handler)
# Sample log messages
logger.debug("Debug message")
logger.info("Info message")
logger.error("An error occurred")
This basic custom format is preferable when you’re building scalable, production-ready apps where you want full control. Comments are added to explain each line of code.
Output:
2025-04-24 11:20:39 - DEBUG - my_app - 24 - Debug message
2025-04-24 11:20:39 - INFO - my_app - 25 - Info message
2025-04-24 11:20:39 - ERROR - my_app - 26 - An error occurred
Using logging.Formatter
with handlers offers more control and flexibility compared to basicConfig()
.
Structured Logging
Another way to format logs in Python is to use python-json-logger
. This package lets you output logs as JSON objects instead of plain text. It extends the logging.Formatter
to format your logs in a structured JSON format.
For more insights into structured logging and why it’s beneficial, check out this article on structured logging.
First, install the package:
pip install python-json-logger
Then write the code:
import logging
from pythonjsonlogger import jsonlogger
logger = logging.getLogger("my_json_app")
logger.setLevel(logging.DEBUG)
logger.handlers.clear()
# Create JSON formatter
formatter = jsonlogger.JsonFormatter(
fmt="%(asctime)s %(levelname)s %(name)s %(message)s",
datefmt="%Y-%m-%dT%H:%M:%SZ"
)
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("User logged in", extra={"user_id": 123, "ip": "192.168.1.1"})
Output:
import logging
from pythonjsonlogger import jsonlogger
logger = logging.getLogger("my_json_app")
logger.setLevel(logging.DEBUG)
logger.handlers.clear()
# Create JSON formatter
formatter = jsonlogger.JsonFormatter(
fmt="%(asctime)s %(levelname)s %(name)s %(message)s",
datefmt="%Y-%m-%dT%H:%M:%SZ"
)
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info("User logged in", extra={"user_id": 123, "ip": "192.168.1.1"})
This package helps to keep logs organized by turning them into JSON instead of plain text. That way, tools can easily read and understand them.
Once your logs are structured well, you’ll also want to think about how to store, search, and analyze them. This log management guide covers those aspects in detail.
Best Practices for Python Logging Format
Following best practices helps ensure your applications are easier to debug, monitor app performance, and maintain. You should check out the general Python logging best practices.
To make your logs more effective, follow these Python logging format best practices:
1. Use JSON for Machine-Readable Logs
Using JSON to structure your Python logs helps turn messy text into clean, organized data. It makes things easier for logging tools like Middleware to read and display those logs clearly, so you can spot issues faster and keep everything running smoothly.
2. Use Consistent Timestamps
Having consistent time stamps (ISO 8601 is recommended) makes log entries easy to sort and trace. It is also required for debugging, as it allows you to see when each event happened and in what order. This also simplifies filtering and analysis.
3. Include Contextual Information
This gives you a full overview of what each log message means. Instead of just seeing “Error occurred”, it’ll be more helpful if you have something like:
2025-04-16 14:30:12 - ERROR - payment_service - UserID: 12345 - Error occurred during payment processing
This will immediately tell you where the error happened and who it affected, enabling you to troubleshoot faster.
4. Avoid Sensitive Data in Logs
Avoiding sensitive data in logs will preserve user privacy, prevent security risks, and maintain your compliance with security regulations like GDPR or HIPAA. Logs can accidentally reveal private data, so avoid that.
5. Use Different Formats for Different Environments
Another thing you should take note of is the environment you’re working in (development or production). It is good to handle log formatting differently for each environment. In development, you want logs that are simple and readable. In production, logs should be machine-friendly and well-structured because they’re often processed by observability tools.
Once you’re comfortable with formatting and structuring logs, the next step is analyzing them for performance insights. Check out this article on Python Performance Monitoring to understand how logging fits into a broader observability strategy.
Using Middleware to Enhance Logs for Observability
Middleware provides you with a platform where you can take Python logs and make them useful. You can automatically collect, structure, and visualize logs in real-time by integrating your Python app with Middleware.

Here’s how Middleware enhances these logs for observability:
- Centralized Logging: All your application logs from any environment are listed on one dashboard for fast access and checking.
- Searching and Filtering: Search your logs in seconds on log levels or keywords so you can see and fix issues more quickly.
- Alerting: Set up notifications to get alerted when things go wrong so you can catch and fix them before they become bigger problems.
- Structured Log Support: If your logs are in JSON, Middleware can automatically organize them in a clean, readable way.
Conclusion
In conclusion, it is necessary that you format your logs for better debugging and monitoring. Choosing the right log format makes your logs easier to read and understand. Custom formats allow you to add more information to your logs.
Tools like Middleware can enhance your logs by making them easier to analyze and troubleshoot. Consider integrating Middleware into your stack for better observability.
FAQs
What is a logging format in Python?
A logging format python is just the way your log messages are arranged. This can include things like the time, the type of message, and what the message says.
Why is it important to customize Python logging formats?
Customizing log formats renders logs more relevant to your application, simpler to analyze, and more suitable for various environments such as development, testing, or production.
What are the key components of a typical log message?
Most log messages include a timestamp, log level (e.g., INFO, ERROR), the actual message, and sometimes the source module or function name.
When should I use structured logging instead of plain text?
Structured logging (like JSON) is ideal for production systems or microservices where logs must be parsed by machines or aggregated by observability platforms like Middleware.
How does Middleware improve Python logging?
Middleware enhances Python logs by collecting and visualizing them in a central platform, helping teams identify issues faster and monitor application behavior more effectively.