LogDecorator

raw JSON →
2.5 verified Fri May 01 auth: no python

Move logging code out of your business logic with decorators. Lightweight library (v2.5) that provides @log_decorator and related decorators for automatic logging of function calls, arguments, results, and exceptions. Requires Python >=3.10. Stable release cadence (sporadic).

pip install logdecorator
error ImportError: cannot import name 'log_decorator' from 'logdecorator'
cause Using an outdated import path (e.g., from logdecorator.LogDecorator) or an old version.
fix
Upgrade to v2.x and use from logdecorator import log_decorator.
error AttributeError: module 'logdecorator' has no attribute 'log_level'
cause Importing only log_decorator without log_level when log_level is referenced.
fix
Use from logdecorator import log_decorator, log_level.
breaking In version 2.x, the import path changed. `from logdecorator import log_decorator` is correct; `from logdecorator.LogDecorator import log_decorator` no longer works.
fix Use `from logdecorator import log_decorator`.
deprecated The `log_exception` decorator has been replaced by using `@log_decorator(log_level.ERROR, exception_only=True)`.
fix Use `@log_decorator(log_level.ERROR, exception_only=True)` instead of `@log_exception`.
gotcha The `log_level` argument must be the constant from `logdecorator.log_level`, not a string like 'INFO'.
fix Use `log_level.INFO` instead of `'INFO'`.

Basic usage: decorate a function to log its calls with INFO level.

from logdecorator import log_decorator, log_level
import logging

logging.basicConfig(level=logging.INFO)

@log_decorator(log_level.INFO)
def my_function(a, b):
    return a + b

my_function(1, 2)