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 ommitting logging statements, you will quickly find yourself overcrowded with logging statements.
Fortunately, you could avoid this by using the python decorator. This library provide easy logging for your code without stealing readability and maintainability. Furthermore, it also provides stack trace to get full local variable on the function.
How to use:
To use this library, you just need to add the decorator to your function. Depending on when you want to log, you can use the decorator as below:
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 test_1(a, b):
return a + b
@log_on_error
def test_2(a, b):
return a / b
if __name__ == '__main__':
test_1(1, b=2)
# INFO:__main__:Start func test_1 with args (1,), kwargs {'b': 2}
# INFO:__main__:Successfully run func test_1 with args (1,), kwargs {'b': 2}
test_2("abc", "def")
# ERROR:__main__:Error in func test_2 with args ('abc', 'def'), kwargs {}
# unsupported operand type(s) for /: 'str' and 'str'.
What devlog can do for you
Decorators
devlog provides three different decorators:
- LogOnStart: Log when the function is called.
- LogOnEnd: Log when the function is finished.
- LogOnError: Log when the function is finished with error.
Use variables in messages
The message given to decorators are treated as a format string which takes the function arguments as the format arguments.
The following example shows how to use variables in messages:
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 are:
Arguments | Description |
---|---|
logger | The logger object. If no logger is given, the devlog will create a logger object with the name of the module where the function is defined. Default is logging.getLogger(callable.__name__) |
handler | A custom log handler object. Only available if no logger object is given, |
args_kwargs | Set true if the message format using args, kwargs format or false to use function parameter name format. Default True |
callable_format_variable | The format variable to use for callable. Default is callable |
trace_stack | Set to True if you want to get the full stack trace. Default is False or capture_local |
capture_locals | Set to True if you want to get the local variable of the function. Default is False (or trace_stack on log_on_error decorator) |
include_decorator | Set to True if you want to include the devlog libray on the stack. Default is False |
log_on_start
Arguments | Description |
---|---|
level | The level of the log message. Default is logging.INFO |
message | The message to log. Could using {args} {kwargs} or function parameter name but not both. Default is Start func {callable.__name__} with args {args}, kwargs {kwargs} |
log_on_end
Arguments | Description |
---|---|
level | The level of the log message. Default is logging.INFO |
message | The message to log. Could using {args} {kwargs} or function parameter name but not both. Default is Successfully run func {callable.__name__} with args {args}, kwargs {kwargs} |
result_format_variable | The format variable to use for reference the return from callable. Default is result |
log_on_error
Arguments | Description |
---|---|
level | The level of the log message. Default is logging.ERROR |
message | The message to log. Could using {args} {kwargs} or function parameter name but not both. Default is Successfully run func {callable.__name__} with args {args}, kwargs {kwargs} |
on_exceptions | A tuple containing exception classes or a single exception, which should get caught and trigger the logging. Default is tuple() (All exception will get caught) |
reraise | Control whether the exception should be reraised after logging. Default is True |
exception_format_variable | The format variable to use for reference the exception raised in callable. Default is error |
Extras
Stack trace
The stack trace configuration.
method | Description |
---|---|
set_stack_start_frames | The number of frames to skip at the start (positive value). Default is 0 |
set_stack_removal_frames | The number of frames to remove from the back of the stack trace (positive value). Default is 6 (the last 6 frame usually from the devlog module) |
Custom exception hook
Override the default exception hook with a custom function.
import devlog
devlog.system_excepthook_overwrite() # Overwrite the default exception hook
This will replace the sys.excepthook with the devlog.excepthook.
Arguments | Description |
---|---|
out_file | The path to the file to write the 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
File details
Details for the file python-devlog-1.2.tar.gz
.
File metadata
- Download URL: python-devlog-1.2.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 281393495a11ab8365fa6364d4e1b56338f8577e14f7eeb134bf2d41d88a1c48 |
|
MD5 | 104bc8d08ba6d54579766d2b22dc2cfc |
|
BLAKE2b-256 | 514721594d005ba50ce84f506981ee6eebbd41df490500488bff7763279f779a |
File details
Details for the file python_devlog-1.2-py3-none-any.whl
.
File metadata
- Download URL: python_devlog-1.2-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0341740edc2bc96e6d9b49dfb3313d4cd768404f2e1180a4ae4feeb20fbfc5a8 |
|
MD5 | 9b91cdbd8ffc7db8f52e67f7b9cf12cf |
|
BLAKE2b-256 | fcb77eafc86609a310caa53112ba2a47eca9f41a94dcefe23f5a9b8fb5718177 |