loggingdecorators
Simple, easy-to-use decorators for logging object initialisation and function calls using the
core logging module.
This package provides 2 decorators which allow you to separate logging functionality from business logic.
Note that these decorators perform no logging setup, this is left to the user.
on_init
on_init(
logger: typing.Union[str, logging.Logger, callable]="logger",
level=logging.DEBUG,
logargs=True,
depth=0
)
When applied to a class or an __init__ method, decorate it with a wrapper which logs the __init__ call using the
given logger at the specified level.
If logger is a string, look up an attribute of this name in the initialised object and use it to log the message. If logger is a function, call it to obtain a reference to a logger instance.
Otherwise, assume logger is an instance of a logger from the logging library and use it to log the message.
If logargs is True, the message contains the arguments passed to __init__.
If the decorated class or __init__ method is to be nested inside other decorators, increase the depth argument by 1
for each additional level of nesting in order for the messages emitted to contain the correct source file name &
line number.
Examples
- Applied directly to a user-defined class:
from loggingdecorators import on_init
from logging import getLogger
# as a class decorator...
@on_init(logger=getLogger())
class Widget:
...
class OtherWidget:
# ... or as an __init__ decorator
@on_init(logger=getLogger())
def __init__(self):
...
- Decorating a built-in class using a subclass:
from loggingdecorators import on_init
from collections import defaultdict
from logging import getLogger
@on_init(logger=getLogger())
class defaultdict_log(defaultdict):
pass
Note: it is not recommended to directly decorate a built-in class unless you want
all initialisations of that class to be logged, as this decorator replaces the class'
__init__ method.
- Decorating a class in a subclass with a mixin:
from loggingdecorators import on_init
from logging import getLogger
class Widget:
...
class LoggingMixin:
@on_init(logger=getLogger())
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class LoggingWidget(LoggingMixin, Widget):
pass
on_call
on_call(
logger: typing.Union[logging.Logger, callable],
level=logging.DEBUG,
logargs=True,
depth=0
)
When applied to a function, decorate it with a wrapper which logs the call using the given logger at the specified level.
The logger argument must be an instance of a logger from the logging library, or a function which returns an instance of a logger.
If logargs is True, log the function arguments, one per line.
If the decorated function is to be nested inside other decorators, increase the depth argument by 1 for each
additional level of nesting in order for the messages emitted to contain the correct source file name & line number.
Examples
- Directly decorating a function:
from loggingdecorators import on_call
from logging import getLogger
@on_call(logger=getLogger())
def interesting_function(*args, **kwargs):
...
interesting_function()
- Creating a logging version of another function:
from loggingdecorators import on_call
from logging import getLogger
def interesting_function(*args, **kwargs):
...
decorator = on_call(logger=getLogger())
interesting_function_log = decorator(interesting_function)
interesting_function_log()
Metadata
Release files for loggingdecorators 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| loggingdecorators-0.1.3.tar.gz | 5.2 kB | Without provenance | Details for loggingdecorators-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| loggingdecorators-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for loggingdecorators-0.1.3-py3-none-any.whl |
Total release size: 11.5 kB
Release files / loggingdecorators-0.1.3.tar.gz
| Download URL | loggingdecorators-0.1.3.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ff3640f772a49709d2812058135343726abc88c783202dcab18c22dc78ec7253
|
|
BLAKE2b-256 checksum How to use checksums |
6e81883a6121632ae660b22c9ab3d5fd9fe2bc74fe33c361ce353cd0e7b87931
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10
|
Release files / loggingdecorators-0.1.3-py3-none-any.whl
| Download URL | loggingdecorators-0.1.3-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
40922cd7ba4d7f24d3e331bf235f2c52c3edf1d891c7b089cd921cb552437add
|
|
BLAKE2b-256 checksum How to use checksums |
7efb991c8b9e9e568d5e96b69ba1e15b1226c2023ef5bfb315502b17246f3e48
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10
|
Security for loggingdecorators 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.