Autologging

1.3.2 · active · verified Wed Apr 15

Autologging (version 1.3.2) is a Python library that simplifies logging and tracing for classes and functions by eliminating boilerplate code. It provides decorators (`@logged` and `@traced`) to automatically inject a logger or trace method calls, respectively. It works with Python's standard `logging` module, allowing for independent configuration and control of application logging and program flow tracing. The library supports Python 2.7 and Python 3.3+.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use the `@logged` and `@traced` decorators on a class. It sets up basic logging to console, including the custom `TRACE` level. When `MyClass` methods are called, `__log` is available for manual logging, and `@traced` automatically logs method entry and exit. Ensure `logging.getLogger().setLevel(TRACE)` is called to make `TRACE` level messages visible.

import logging
import sys
from autologging import logged, traced, TRACE

# Configure basic logging to see the output
logging.basicConfig(
    level=logging.DEBUG,
    stream=sys.stdout,
    format="%(levelname)s:%(name)s:%(funcName)s:%(message)s"
)
# Set root logger level to TRACE to see tracing output
logging.getLogger().setLevel(TRACE)

@traced # Apply @traced outermost for consistent behavior
@logged
class MyClass:
    def __init__(self, name):
        self.name = name
        self.__log.info("MyClass instance created with name: %s", self.name)

    def do_work(self, value):
        self.__log.debug("Doing work with value: %s", value)
        result = value * 2
        return result

    def _internal_method(self):
        self.__log.trace("Executing internal method.")
        return "internal_result"

# Example usage
instance = MyClass("Example")
output = instance.do_work(10)
print(f"Result of do_work: {output}")
instance._internal_method()

view raw JSON →