Logging Tree
The `logging-tree` library provides a simple way to introspect and display the logger hierarchy from Python's built-in `logging` module. It helps developers visualize how loggers are configured, including their levels and handlers. The current version is 1.10, with releases occurring periodically to add minor features or address issues.
Common errors
-
ModuleNotFoundError: No module named 'logging_tree'
cause The `logging-tree` library is not installed in your Python environment.fixRun `pip install logging-tree` to install the library. -
My logging tree output is not appearing in my log file / specific handler output.
cause `logging_tree.printout()` by default sends its output directly to standard output (`sys.stdout`), not through the `logging` system's handlers.fixTo redirect the output, use the `file` argument: `from io import StringIO; output = StringIO(); printout(file=output); tree_string = output.getvalue()`. -
The displayed logging tree only shows 'root' and nothing else, even though my application uses logging.
cause While your application may use logging, it might only be using the root logger or loggers that haven't been explicitly retrieved with `logging.getLogger('name')`, or your `printout()` call happens before loggers are fully initialized.fixEnsure that named loggers are created and active before calling `printout()`. Also, verify that you are indeed defining distinct loggers beyond the root. For example, explicitly call `logging.getLogger('my_app')` at some point in your code.
Warnings
- gotcha `printout()` writes to `sys.stdout` by default.
- gotcha An 'empty' or minimal tree might be displayed if no named loggers are actively used or configured.
- deprecated The `main()` function is deprecated.
Install
-
pip install logging-tree
Imports
- printout
from logging_tree import printout
Quickstart
import logging
from logging_tree import printout
# Configure some loggers to demonstrate the tree
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
root_logger.addHandler(logging.StreamHandler())
app_logger = logging.getLogger('my_app')
app_logger.setLevel(logging.DEBUG)
module_logger = logging.getLogger('my_app.sub_module')
module_logger.setLevel(logging.WARNING)
# Log some messages to ensure loggers are active
root_logger.info('Root logger message')
app_logger.debug('App logger message')
module_logger.warning('Sub-module logger message')
# Display the logging tree
print('\nLogging Tree:')
printout()