Say Goodbye to the Standard Logging Module, I’m Choosing Loguru
If you’ve ever spent an entire morning just configuring Python’s default logging module, you know the frustration. Creating a logger, defining a handler, setting up a formatter, and then attaching the handler… too much boilerplate code just to print a single error message.
Loguru was born to solve that mess. Its philosophy is extremely practical: “Logging made simple”. No need to relearn everything or deal with complex configurations—just import and use. After many backend projects, I’ve realized that focusing on business logic is much more important than tweaking every comma in a log format.
Reality from my projects: A script that started at 200 lines scaled to 2000 lines with multiple parallel threads, and the default logging began to show its weaknesses. Switching to Loguru saved me 70% of debugging time during system incidents.
Installation in 3 Seconds
Open your terminal and install the library via pip:
pip install loguru
This library is lightweight and doesn’t pull in messy dependencies.
The “One for All” Mindset
In the standard logging module, you usually have to create a separate instance for each module. With Loguru, you only work with a single logger object. Loguru’s system revolves around the concept of Sinks.
A Sink is where you want to send log data: terminal, file, email, or even Slack. Instead of creating long-winded handlers, you just use the .add() function.
Hands-on: From Colorful Logs to Professional File Management
1. Colorful Logs (Easy to read, easy to manage)
The biggest plus when starting with Loguru is the colors. The library automatically color-codes levels (INFO in green, ERROR in red). You’ll no longer have to strain your eyes searching for errors in a sea of black-and-white text while the server is spamming logs.
from loguru import logger
logger.debug("Debug info - development only")
logger.info("System started successfully")
logger.warning("Warning: RAM usage above 80%")
logger.error("Database connection error (Retry #3)")
logger.critical("System stopped abruptly!")
By default, every log line includes: Time (ISO 8601), Level, Filename, and the exact line of code. You get all these benefits without writing a single line of configuration.
2. Logging to Files and Auto-compression (Rotation)
Never let log files swell to several gigabytes as they can crash your server. Loguru handles this intelligently through parameters in the add() function.
# Create a new file when the old one reaches 500 MB
logger.add("app.log", rotation="500 MB")
# Create a new log file every day at 12:00 PM
logger.add("daily.log", rotation="12:00")
# Keep logs for only 10 days to save space
logger.add("cleanup.log", retention="10 days")
# Compress old logs into .zip files for storage
logger.add("archive.log", compression="zip")
Compared to using the cumbersome RotatingFileHandler in the standard module, this approach is much more intuitive.
3. Instant Error Catching with @logger.catch
This is the “killer feature”. Usually, when code crashes, Python prints a hard-to-read traceback. The @logger.catch decorator wraps your function and provides a detailed analysis of variables at the moment of failure.
@logger.catch
def process_data(a, b):
return a / b
# If b = 0, Loguru will display the values of a and b directly in the traceback
process_data(10, 0)
In my experience, this feature reduces bug-hunting time from 30 minutes to 3 minutes, as you can see the incorrect values immediately without needing to insert print statements for debugging.
4. Exporting Logs as JSON for ELK/Grafana
In large projects, logs need to be structured so that tools like Elasticsearch or Loki can parse them.
# Simply add serialize=True
logger.add("server.json", serialize=True)
Now every log line is a complete JSON object, containing everything from timestamps to thread and process information.
Tips for Real-world Implementation
As your project grows from a small script into a microservices system, keep in mind:
- Use Enqueue: Add
enqueue=Truein theadd()function for asynchronous logging. This prevents the app from slowing down while waiting for logs to be written to disk. - Environment Separation: In Dev, set logs to
DEBUGlevel. For Production, upgrade toINFOorSUCCESSto avoid filling up disk space unnecessarily. - Custom Formatting: You can streamline your log lines by redefining the format:
logger.add("short.log", format="{time:HH:mm:ss} | {level} | {message}")
Conclusion
Good logging isn’t just for show. It’s the difference between a good night’s sleep and staying up all night hunting a “ghost” bug in production. Loguru makes this process much more effortless and professional. If you’re starting a new Python project, don’t hesitate to install Loguru and skip the default logging module.

