Twiggy
Twiggy is a Pythonic logging library that emphasizes structured logging and a fluid, method-chaining API. It aims to be more "Pythonic" than the standard library's `logging` module. The current version is 0.5.1, released on May 12, 2021. The library has a slow release cadence and is marked by its maintainers as "not rock solid (yet)", suggesting it is in a maintenance status rather than active, rapid development.
Common errors
-
NameError: name 'log' is not defined
cause The global 'log' object or 'quick_setup' function was used without being imported, or the `quick_setup()` function was not called to initialize the logging system.fixAdd `from twiggy import quick_setup, log` at the top of your file, and ensure `quick_setup()` is called once early in your application's lifecycle. -
TypeError: 'Message' object is not callable
cause Attempting to call the `log` object directly like a function (`log("message")`) instead of calling one of its level-specific methods (e.g., `log.info()`, `log.warning()`).fixUse a logging level method on the `log` object, e.g., `log.info("Your message here")`, `log.debug("Another message")`, etc. -
SyntaxError: invalid syntax (related to string formatting in log messages)
cause Mixing up string formatting styles (e.g., using old-style `%` formatting with `str.format()` placeholders, or vice-versa).fixTwiggy primarily uses `str.format()` style. Ensure your format string (`"hello {who}"`) matches the arguments (`who='world'`) and avoid mixing with C-style `%` formatting.
Warnings
- gotcha The official documentation states, 'Twiggy works great, but is not rock solid (yet); do not use for nuclear power plants, spaceships or mortgage derivatives trading.' This suggests caution for mission-critical applications.
- breaking Version 0.4.1's changelog mentions "deprecate features, pending a rewrite in 0.5". Users upgrading from versions prior to 0.5.0 that relied on these deprecated features may experience breakage.
- gotcha Twiggy's `log` object needs to be imported (`from twiggy import log`) and often `quick_setup()` needs to be called to configure basic output before logging messages. Forgetting these steps leads to errors or no output.
Install
-
pip install Twiggy
Imports
- quick_setup
import twiggy.quick_setup
from twiggy import quick_setup
- log
import twiggy.log
from twiggy import log
- FileOutput
from twiggy import FileOutput
from twiggy.outputs import FileOutput
Quickstart
from twiggy import quick_setup, log
# Configure a basic console output
quick_setup()
# Log a message with structured fields
log.name('my_app').fields(user_id=123).info("User {user_id} logged in from {ip}", ip="192.168.1.100")
# Log a warning message
log.warning("Something unusual happened, but it's not critical.")
# Using the NOTICE level (added in 0.5.0)
log.notice("Heads up: An important event occurred.")