From Logging Hell to Loguru Heaven: How I Finally Made Peace with Python Logging



This content originally appeared on Level Up Coding – Medium and was authored by Harish Siva Subramanian

Photo by Marcos Paulo Prado on Unsplash

The Dark Ages: My Relationship with Python’s logging Module

Picture this: It’s 2 AM, your production server is on fire, and you desperately need to figure out what went wrong. You frantically open your log files, only to be greeted by… nothing. Or worse, a cryptic timestamp with no context. Sound familiar?

For the longest time, my relationship with Python’s built-in logging module was like trying to assemble IKEA furniture blindfolded while riding a unicycle. Technically possible, but why would you want to put yourself through that torture?

import logging

# The dreaded configuration ritual
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('app.log'),
logging.StreamHandler()
]
)

logger = logging.getLogger(__name__)
logger.info("If you can read this, I've already spent 30 minutes on Stack Overflow")

Every time I needed to set up logging, I felt like I was performing some ancient ritual. “Please, oh great logging gods, accept my offering of formatters, handlers, and mysterious percentage symbols!”

The Moment Everything Changed

Then I discovered Loguru. And friends, let me tell you — it was like switching from a Nokia 3310 to an iPhone. Suddenly, logging wasn’t just bearable; it was actually enjoyable.

Here’s the same functionality, but with Loguru:

from loguru import logger

logger.info("Holy moly, this actually works without sacrificing a goat!")

That’s it. Two lines. TWO LINES!

I literally stared at my screen for five minutes thinking, “This can’t be real. Where’s the catch? Where are the 47 configuration files I need to create?”

Why Loguru is Like Having a Logging Superhero

1. Zero Configuration Required

Loguru works beautifully right out of the box. No more spending your lunch break trying to figure out why your logs aren’t appearing in the console. It’s like having a car that doesn’t require you to rebuild the engine every time you want to drive to the grocery store.

from loguru import logger

logger.debug("I'm debugging!")
logger.info("Here's some info")
logger.warning("Uh oh, something's fishy")
logger.error("Houston, we have a problem")
logger.critical("The server is literally on fire")

2. Automatic File Rotation (Because Nobody Likes 50GB Log Files)

Remember the time your 6-month-old log file crashed your text editor? Loguru’s got your back:

from loguru import logger

# Automatically rotate when file reaches 10 MB
logger.add("my_app.log", rotation="10 MB")

# Or rotate daily
logger.add("daily_logs.log", rotation="1 day")

# Keep only the last 10 files
logger.add("app.log", rotation="100 MB", retention="10 files")

It’s like having a responsible adult manage your logs while you focus on breaking… I mean, writing code.

3. Beautiful, Colorful Output

The built-in logging module’s output looks like it was designed by someone who thought “visual appeal” was a type of fruit. Loguru’s output, on the other hand, is so pretty you’ll actually want to look at your logs:

  • 🟢 INFO messages in calming green
  • 🟡 WARNING messages in attention-grabbing yellow
  • 🔴 ERROR messages in panic-inducing red

4. Structured Logging Made Simple

Want to add context to your logs? With the old logging module, you’d need to perform dark magic:

# The old way (ugh)
logger.info("User %s performed action %s with result %s",
user_id, action, result)

With Loguru, it’s as natural as breathing:

# The Loguru way (ahh)
logger.info("User {user} performed {action} with result {result}",
user=user_id, action=action, result=result)

Real-World Magic: Exception Handling

Here’s where Loguru really shines. Remember trying to get meaningful stack traces in your logs? It was like trying to get your cat to fetch — theoretically possible, but good luck with that.

from loguru import logger

@logger.catch
def divide_by_zero():
return 1 / 0

divide_by_zero() # Loguru automatically logs the full traceback!

The @logger.catch decorator is like having a safety net for your functions. When something goes wrong (and let's be honest, it will), you get beautiful, detailed error logs without writing a single try/except block.

Advanced Features That’ll Make You Smile

Custom Formatting (Without the Headache)

from loguru import logger
import sys

# Remove the default handler
logger.remove()

# Add a custom format
logger.add(
sys.stderr,
format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
)

logger.info("Look ma, custom formatting without crying!")

Filtering Like a Pro

from loguru import logger

# Only log errors from specific modules
logger.add("errors_only.log", level="ERROR")

# Custom filter function
def my_filter(record):
return "important" in record["message"]

logger.add("important_stuff.log", filter=my_filter)

logger.info("This is important stuff") # Goes to important_stuff.log
logger.info("This is boring") # Doesn't go anywhere

Serialization for the Modern Age

from loguru import logger

logger.add("structured_logs.json", serialize=True)

logger.bind(user_id=123, session="abc").info("User logged in")
# Creates beautiful JSON logs perfect for modern log analysis tools

The Bottom Line: Life’s Too Short for Bad Logging

Look, I spent years wrestling with Python’s logging module like it owed me money. I’ve written more logging boilerplate than actual business logic. I’ve debugging sessions that lasted longer than some Hollywood movies.

But with Loguru? Logging became the least of my worries. It just works. It’s intuitive. It’s powerful. And most importantly, it doesn’t make me question my life choices.

Photo by Donald Giannatti on Unsplash

Getting Started (It’s Embarrassingly Easy)

pip install loguru
from loguru import logger

logger.info("Welcome to the future of Python logging!")

That’s literally it. You’re now logging like a pro.

Final Thoughts

If you’re still using the built-in logging module for new projects, I have one question: Why do you hate yourself?

Loguru has turned logging from a necessary evil into something I actually look forward to implementing. My logs are cleaner, my debugging sessions are shorter, and I sleep better at night knowing my production logs will actually help me when things go sideways.

Give Loguru a try. Your future self (and your 2 AM debugging sessions) will thank you.

If you like the article and would like to support me, make sure to:


From Logging Hell to Loguru Heaven: How I Finally Made Peace with Python Logging was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding – Medium and was authored by Harish Siva Subramanian