Why Loguru Beats Python’s Built‑in Logging: A Hands‑On Guide

This article introduces Loguru, a third‑party Python logging library, explains why it simplifies logging compared to the standard logging module, outlines its key features such as out‑of‑the‑box setup, async safety, lazy evaluation, and file rotation, and provides practical code examples for quick integration into scripts, libraries, and frameworks.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Why Loguru Beats Python’s Built‑in Logging: A Hands‑On Guide

Introduction

Loguru: a more elegant logging solution!
loguru

is a simple yet powerful third‑party logging library for Python that adds useful features to the standard logger, reducing the pain of Python logging.

1. Why use Loguru

Simple and convenient way to output the logs you need.

When writing Python scripts you often need logs to debug issues and record important information.

The built‑in logging module requires manual handler/formatter configuration, which can be cumbersome.

import logging
logger = logging.getLogger('xxx')
handler = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.debug('This is a %s', 'test')

Loguru works out of the box without such initialization.

# pip install loguru

2. Features

Key advantages of Loguru.

Ready‑to‑use, no setup required.

No explicit initialization; just import and use.

Easy file rotation, retention, compression.

Elegant string formatting.

Exception catching in threads or main thread.

Customizable log level styles.

Async‑compatible and thread‑/process‑safe.

Lazy evaluation.

Suitable for scripts and libraries.

Full compatibility with the standard logging module.

Improved datetime handling.

3. Quick start

Common Loguru operations.
from loguru import logger
logger.debug("That's it, beautiful and simple logging!")

Adding a handler:

logger.add(sys.stderr,
    format="{time} {level} {message}",
    filter="my_module",
    level="INFO")

File rotation and retention:

logger.add("file_{time}.log")
logger.add("file_{time}.log", rotation="500 MB")
logger.add("file_{time}.log", rotation="12:00")
logger.add("file_{time}.log", rotation="1 week")
logger.add("file_X.log", retention="10 days")
logger.add("file_Y.log", compression="zip")

Lazy evaluation:

logger.opt(lazy=True).debug("If sink level <= DEBUG: {x}", x=lambda: expensive_function(2**64))

Custom log level:

new_level = logger.level("SNAKY", no=38, color="<yellow>", icon="🐍")
logger.log("SNAKY", "Here we go!")

4. Important details

add() parameters and removal.
sink

can be a file path, file object, stream, another logging handler, or a custom callable.

Calling remove(id) disables the previously added sink.

trace = logger.add('runtime.log')
logger.debug('this is a debug message')
logger.remove(trace)
logger.debug('this is another debug message')

Integration with Flask (example omitted for brevity).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DebuggingperformanceserializationloggingAsyncLoguru
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.