How to write to a file, using the logging Python module?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The Python logging module can write directly to a file through a FileHandler or through basicConfig with a filename. The key difference from plain file writing is that logging adds levels, formatting, timestamps, and handler management automatically.
If all you want is "append text to a file," open(..., "a") works. If you want structured application logs, logging is the right tool.
The Simplest File Logging Setup
This creates or appends to app.log and writes formatted log lines there.
The important options are:
- '
filenamefor the output file' - '
levelfor the minimum severity' - '
formatfor the log line structure'
A More Explicit FileHandler Setup
For anything beyond the simplest script, building the logger explicitly is clearer:
This gives you direct control over handlers and makes it easier to combine file logging with console logging later.
It also scales better when the application grows and different modules need different logger names or handler combinations.
That explicit setup is also easier to test, because you can inspect which handlers and formatters were attached instead of relying on hidden global configuration.
It also makes later refactoring easier when the project grows from one script into a larger application with several logging destinations.
Log to File and Console at the Same Time
This is a common production pattern because developers want logs on screen during local runs and persisted to disk in longer-running environments.
It also helps during debugging because the console gives immediate feedback while the file provides a historical record you can inspect later.
Avoid Duplicate Log Lines
One frequent problem is accidentally adding handlers multiple times, especially in notebooks, REPL sessions, or code that reinitializes logging.
Before attaching handlers, check whether they already exist:
Otherwise each log message may appear two or three times in the file.
Rotating Log Files
If the file can grow indefinitely, use a rotating handler:
This prevents one log file from growing forever.
For long-running services, that matters a lot. Logging to a file is useful only if the file remains manageable and does not quietly consume all available disk space.
Common Pitfalls
- Using
print()for application logging when you actually need timestamps and levels. - Calling
basicConfigafter logging has already been configured elsewhere and expecting it to reconfigure everything. - Adding the same handler multiple times and getting duplicate log lines.
- Forgetting encoding when the log file may contain non-ASCII text.
- Letting one log file grow forever when rotation would be safer.
Summary
- Use
logging.basicConfig(filename=...)for the quickest file logging setup. - Use
FileHandlerwhen you want explicit control over handlers and formatters. - Combine file and console handlers when you need both persistent and visible logs.
- Guard against duplicate handlers in reusable code.
- Use rotating handlers when log growth needs to be controlled.
Related reading
- How to write to Kafka from Python logging module?
- How to write to TensorBoard in TensorFlow 2
- How to yank to host clipboard from inside a Docker container?
- How upgrade a docker image without creating new image?
- How to write to an Excel spreadsheet using Python?
- How to write very long string that conforms with PEP8 and prevent E501
- How using try catch for exception handling is best practice
- .htaccess ErrorDocument 404 not showing up

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.