Python Logging: How to Write Logs Like a Pro!
Overview
Effective logging turns a black-box application into a transparent system. While many developers rely on print statements during development, Python's logging package provides a robust framework for tracking events, diagnosing bugs, and monitoring production health. It allows you to categorize messages by severity and direct them to various outputs without changing your core logic.

Prerequisites
To follow this guide, you should have a basic understanding of Python syntax and modular programming. Knowledge of file handling and external API integration is helpful but not required.
Key Libraries & Tools
- logging: The built-in Python module for event tracking.
- PaperTrail: A cloud-based log management service for centralized visualization.
- syslog: A standard for message logging used to send data to external handlers.
Code Walkthrough
Basic Configuration
The simplest way to start is using basicConfig. This sets the global threshold for what messages actually get recorded.
import logging
# Set the threshold to INFO
logging.basicConfig(level=logging.INFO)
logging.debug("This won't show")
logging.info("This will show")
Formatting and File Output
You can customize the look of your logs and redirect them from the console to a file using the format and filename arguments.
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
filename='app.log'
)
Using Handlers for External Services
For production, use logging objects and handlers to send data to services like PaperTrail.
from logging.handlers import SysLogHandler
logger = logging.getLogger("MyApp")
handler = SysLogHandler(address=('your-host.papertrailapp.com', 12345))
logger.addHandler(handler)
logger.warning("External log message")
Syntax Notes
Python uses five standard levels: DEBUG, INFO, WARNING, ERROR, and CRITICAL. The default level is WARNING. Use the %(name)s placeholder in formats to identify which module triggered the log.
Practical Examples
Commercial platforms use logging to detect hacking attempts by monitoring failed login frequency. Developers also use it to trace back the exact state of a system before a crash occurs in a customer environment.
Tips & Gotchas
Never log sensitive data. Twitter and Cam4 suffered massive data leaks because they stored unhashed passwords or PII in logs. Treat your log files as if they are publicly accessible; avoid logging credit card numbers or email addresses entirely.
- logging
- 29%· products
- Cam4
- 14%· companies
- Elasticsearch
- 14%· companies
- PaperTrail
- 14%· companies
- Python
- 14%· products
- 14%· companies

Why Most Python Logging Setups Fail in Production
WatchArjanCodes // 11:02
On this channel, I post videos about programming and software design to help you take your coding skills to the next level. I'm an entrepreneur and a university lecturer in computer science, with more than 20 years of experience in software development and design. If you're a software developer and you want to improve your development skills, and learn more about programming in general, make sure to subscribe for helpful videos. I post a video here every Friday. If you have any suggestion for a topic you'd like me to cover, just leave a comment on any of my videos and I'll take it under consideration. Thanks for watching!