Skip to content

Loguru

Developer UtilitiesCLI/UtilsPython

What it is

Loguru is a Python library that simplifies logging by providing an easy-to-use, flexible, and powerful logging system. It reduces boilerplate code and adds features like sink management, message formatting, and exception catching.

Loguru allows you to log messages with various severity levels, format logs with colors, redirect logs to files, and catch exceptions automatically. It supports both synchronous and asynchronous applications and can be easily integrated into existing projects.

Installation

pip install loguru

Getting started

The smallest useful thing you can do with it, and what each part means.

Simple logging
from loguru import logger

logger.info('This is an info message')
logger.warning('This is a warning')
Logs messages at different severity levels (info, warning) with minimal setup.
Logging to a file
from loguru import logger

logger.add('file.log')
logger.info('This message is saved to a file')
Redirects log output to a file, creating it automatically if it doesn’t exist.

Advanced usage

Where the library earns its place over a simpler alternative.

Custom formatting
from loguru import logger

logger.add('file.log', format='{time} | {level} | {message}', level='INFO')
logger.info('Custom formatted log')
Uses a custom log format including time, severity level, and message.
Rotating log files
from loguru import logger

logger.add('file_{time}.log', rotation='1 MB')
logger.info('This log will rotate after reaching 1 MB')
Automatically rotates log files when they reach a specified size.
Catching exceptions
from loguru import logger

@logger.catch
def faulty():
    x = 1 / 0

faulty()
Automatically catches exceptions in functions, logs stack traces, and prevents application crash.
Multiple sinks
from loguru import logger

logger.add('file.log')
logger.add(sys.stderr, colorize=True, format='<green>{time}</green> | {level} | {message>')
logger.info('Logged to both file and console')
Logs messages to multiple outputs (file and console) with independent formatting.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

FileNotFoundError
Ensure the specified log file path exists or use a valid path.
PermissionError
Check file permissions and ensure the application can write to the log file.

Best practices

  • Use `logger.add()` to manage multiple log destinations and formats.
  • Use `@logger.catch` to simplify exception handling and logging.
  • Set appropriate log levels for different environments (DEBUG for development, WARNING/ERROR for production).
  • Rotate and compress log files to prevent storage issues.
  • Avoid excessive logging in performance-critical code sections.

Background

Why it exists, and what it was reacting to.

Loguru was created by Delgan in 2017 to offer a simpler, more intuitive logging experience than Python's built-in `logging` module. It aims to streamline logging setup and usage while providing advanced capabilities such as structured logging, colored outputs, and file rotation.