Migrate from logging¶
The standard logging module is very configurable, but it asks for a lot of setup: loggers, handlers, formatters, levels on each of them. This page shows how the usual logging code maps to Logust, so you can move an existing project step by step.
Why move¶
Compared to the standard logging module, Logust gives you:
- Zero configuration: one import, and you get readable, colored output with time, level and caller.
- Better performance out of the box: formatting and file writes run in Rust. See the Benchmarks.
- A simpler API: one
loggerobject, and handlers you add with one call. - Built-in rotation, retention, compression, colors and JSON, without extra handler classes.
A first migration¶
Here's a typical logging setup that writes to the console and to a file, next to the same thing with Logust:
With Logust, the console handler is already there, so you only add the file. Both lines, the console one and the one in app.log, use the default format with the caller (__main__:<module>:4).
There's no getLogger(__name__): you import the same logger everywhere, and the module name is filled in for you as {name}.
Messages, context and exceptions¶
The everyday calls change a bit:
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
user = "alice"
logger.info("User %s logged in from %s", user, "10.0.0.7")
logger.info("Payment received", extra={"order_id": 1234})
try:
result = 1 / 0
except ZeroDivisionError:
logger.exception("Calculation failed")
from logust import logger
user = "alice"
logger.info("User {} logged in from {}", user, "10.0.0.7")
logger.bind(order_id=1234).info("Payment received")
try:
result = 1 / 0
except ZeroDivisionError:
logger.exception("Calculation failed")
$ python main.py
2026-10-06 12:05:51.036 | INFO | __main__:<module>:4 - User alice logged in from 10.0.0.7
2026-10-06 12:05:51.036 | INFO | __main__:<module>:5 - Payment received
2026-10-06 12:05:51.036 | ERROR | __main__:<module>:10 - Calculation failed
Traceback (most recent call last):
File "/home/user/code/main.py", line 8, in <module>
1 / 0
~~^~~
ZeroDivisionError: division by zero
- Arguments use
{}placeholders (str.format()style) instead of%s. See Message Arguments. - Context goes in
bind()instead ofextra={...}. See Adding Context. logger.exception()works the same: call it in anexceptblock to log the traceback. See Logging Exceptions.
%s is not replaced
Logust doesn't know about %-style placeholders. They are left as they are:
Search your code for %s, %d, %r and %( in log calls when you migrate.
Rotating files¶
RotatingFileHandler and TimedRotatingFileHandler become options of logger.add():
from logust import logger
logger.add(
"app.log",
level="INFO",
rotation="10 MB",
retention=5,
)
logger.add("daily.log", rotation="daily", retention="7 days")
logger.info("Rotation without RotatingFileHandler")
rotation="10 MB"replacesmaxBytes, andretention=5replacesbackupCount.rotation="daily"replacesTimedRotatingFileHandler(when="midnight"), andretention="7 days"keeps a week of files.
You can also compress the old files with compression. Read all about it in Rotation, Retention and Compression.
Cheat sheet¶
logging | Logust |
|---|---|
logger = logging.getLogger(__name__) | from logust import logger |
logger.info("x=%s", x) | logger.info("x={}", x) |
logger.info("msg", extra={"k": v}) | logger.bind(k=v).info("msg") |
logging.LoggerAdapter(logger, {"k": v}) | logger.bind(k=v) |
logger.exception("msg") | logger.exception("msg") |
logger.error("msg", exc_info=True) | logger.opt(exception=True).error("msg") |
logging.basicConfig(level=logging.INFO) | logger.set_level("INFO") |
handler.setLevel(logging.WARNING) | logger.add(sink, level="WARNING") |
logging.Formatter("%(asctime)s %(message)s") | logger.add(sink, format="{time} {message}") |
logging.FileHandler("app.log") | logger.add("app.log") |
RotatingFileHandler(maxBytes=..., backupCount=...) | logger.add("app.log", rotation="10 MB", retention=5) |
TimedRotatingFileHandler(when="midnight") | logger.add("app.log", rotation="daily") |
logging.StreamHandler(sys.stderr) | logger.add(sys.stderr) |
logging.Filter subclass | logger.add(sink, filter=lambda record: ...) |
logging.addLevelName(25, "NOTICE") | logger.level("NOTICE", no=25) |
logging.config.dictConfig({...}) | logger.configure(handlers=[...], levels=[...]) |
logging.getLogger("lib").setLevel(logging.CRITICAL) | logger.disable("lib") |
| A JSON formatter library | logger.add(sink, serialize=True) |
Logust's built-in levels are the same numbers as logging (DEBUG 10, INFO 20, WARNING 30, ERROR 40, CRITICAL 50), plus TRACE (5), SUCCESS (25) and FAIL (45). See Log Levels.
Third-party libraries¶
Your dependencies keep using logging, and that's fine. Route their records through Logust with one call, as explained in Intercept Standard Logging:
This also lets you migrate gradually: modules you haven't moved yet keep calling logging, and their messages still end up in your Logust handlers.
Recap¶
- Import
loggerfromlogustinstead of creating loggers withgetLogger(). - Replace
%splaceholders with{}, andextra=withbind(). - Handlers, formatters, levels and rotation become arguments of
logger.add(). dictConfig()becomeslogger.configure().- Use
intercept_logging()for third-party libraries and for code you haven't migrated yet.