No more logging in your code business logic with decorators
Project description
devlog
No more logging in your code business logic with python decorators.
Logging is a very powerful tool for debugging and monitoring your code. But if you are often adding logging statements, you will quickly find your code overcrowded with them.
Fortunately, you can avoid this by using python decorators. This library provides easy logging for your code without stealing readability and maintainability. It also provides stack traces with full local variables, value sanitization, and async support.
Requires Python 3.9+
Installation
pip install python-devlog
How to use
Add the decorator to your function. Depending on when you want to log, you can use:
import logging
from devlog import log_on_start, log_on_end, log_on_error
logging.basicConfig(level=logging.DEBUG)
@log_on_start
@log_on_end
def add(a, b):
return a + b
@log_on_error
def divide(a, b):
return a / b
if __name__ == '__main__':
add(1, b=2)
# INFO:__main__:Start func add with args (1,), kwargs {'b': 2}
# INFO:__main__:Successfully run func add with args (1,), kwargs {'b': 2}
divide("abc", "def")
# ERROR:__main__:Error in func divide with args ('abc', 'def'), kwargs {}
# unsupported operand type(s) for /: 'str' and 'str'.
Async support
All decorators work with async functions automatically:
@log_on_start
@log_on_end
@log_on_error
async def fetch_data(url):
...
Value sanitization
Prevent sensitive values from appearing in logs using Sensitive or sanitize_params:
from devlog import log_on_start, Sensitive
# Option 1: Wrap the value — function receives the real value, logs show "***"
@log_on_start
def login(username, password):
...
login("admin", Sensitive("hunter2"))
# INFO:__main__:Start func login with args ('admin', '***'), kwargs {}
# Option 2: Auto-redact by parameter name
@log_on_start(sanitize_params={"password", "token", "secret"})
def connect(host, token):
...
connect("example.com", "sk-abc123")
# INFO:__main__:Start func connect with args ('example.com', '***'), kwargs {}
Sensitive is a transparent proxy — the wrapped function receives the real value. Only devlog log output is redacted.
What devlog can do for you
Decorators
devlog provides three decorators:
- log_on_start: Log when the function is called.
- log_on_end: Log when the function finishes successfully.
- log_on_error: Log when the function raises an exception.
Use variables in messages
The message given to decorators is treated as a format string which takes the function arguments as format arguments.
import logging
from devlog import log_on_start
logging.basicConfig(level=logging.DEBUG)
@log_on_start(logging.INFO, 'Start func {callable.__name__} with args {args}, kwargs {kwargs}')
def hello(name):
print("Hello, {}".format(name))
if __name__ == "__main__":
hello("World")
Which will print:
INFO:__main__:Start func hello with args ('World',), kwargs {}
Documentation
Format variables
The following variables are available in the format string:
| Default variable name | Description | LogOnStart | LogOnEnd | LogOnError |
|---|---|---|---|---|
| callable | The function object | Yes | Yes | Yes |
| args/kwargs | The arguments, key arguments passed to the function | Yes | Yes | Yes |
| result | The return value of the function | No | Yes | No |
| error | The error object if the function is finished with error | No | No | Yes |
Base arguments
Available arguments in all decorators:
| Argument | Description |
|---|---|
| logger | The logger object. If no logger is given, devlog will create one with the module name where the function is defined. Default is logging.getLogger(callable.__module__) |
| handler | A custom log handler object. Only available if no logger object is given. |
| args_kwargs | Set True to use {args}, {kwargs} format, or False to use function parameter names. Default True |
| callable_format_variable | The format variable name for the callable. Default is callable |
| trace_stack | Set to True to get the full stack trace. Default is False |
| capture_locals | Set to True to capture local variables in stack frames. Default is False (or trace_stack on log_on_error) |
| include_decorator | Set to True to include devlog frames in the stack trace. Default is False |
| sanitize_params | A set of parameter names to auto-redact in log messages. Default is None |
log_on_start
| Argument | Description |
|---|---|
| level | The level of the log message. Default is logging.INFO |
| message | The message to log. Can use {args} {kwargs} or function parameter names, but not both. Default is Start func {callable.__name__} with args {args}, kwargs {kwargs} |
log_on_end
| Argument | Description |
|---|---|
| level | The level of the log message. Default is logging.INFO |
| message | The message to log. Can use {args} {kwargs} or function parameter names, but not both. Default is Successfully run func {callable.__name__} with args {args}, kwargs {kwargs} |
| result_format_variable | The format variable name for the return value. Default is result |
log_on_error
| Argument | Description |
|---|---|
| level | The level of the log message. Default is logging.ERROR |
| message | The message to log. Can use {args} {kwargs} or function parameter names, but not both. Default is Error in func {callable.__name__} with args {args}, kwargs {kwargs}\n{error} |
| on_exceptions | Exception classes to catch and log. Default catches all exceptions. |
| reraise | Whether to reraise the exception after logging. Default is True |
| exception_format_variable | The format variable name for the exception. Default is error |
Extras
Custom exception hook
Override the default exception hook to write crash logs with local variable capture:
import devlog
devlog.system_excepthook_overwrite() # Overwrite the default exception hook
This replaces sys.excepthook with devlog's handler, which writes detailed crash information to a file.
| Argument | Description |
|---|---|
| out_file | The path to the file to write the crash log. Default is crash.log |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_devlog-2.0.tar.gz.
File metadata
- Download URL: python_devlog-2.0.tar.gz
- Upload date:
- Size: 16.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7b080c783d3eafbca9822c444de92836d14bd3aa96a017b5b0c5b748aa5bb31
|
|
| MD5 |
38a6f61a14ffb85376135648815eb4ae
|
|
| BLAKE2b-256 |
cb0dd40a860866e4ad477ce9ffd47a6019d542530d62a78f6b6a406859c91e1e
|
File details
Details for the file python_devlog-2.0-py3-none-any.whl.
File metadata
- Download URL: python_devlog-2.0-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69d24dd3aa5bac815ca250224fe2c09e48455d42025f95e4af6fefb72a83d5f1
|
|
| MD5 |
b464ff574d10f6de621460fda3972608
|
|
| BLAKE2b-256 |
4cacb461d33b641809e421e8227536a2045c36004719ad928387b1448f484076
|