python-devlog 1.1__tar.gz → 2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_devlog-2.0/.github/workflows/python-publish.yml +39 -0
- python_devlog-2.0/.github/workflows/python-test.yml +27 -0
- python_devlog-2.0/.gitignore +6 -0
- python_devlog-2.0/CHANGELOG.txt +21 -0
- python_devlog-2.0/PKG-INFO +227 -0
- python_devlog-2.0/README.md +206 -0
- {python-devlog-1.1 → python_devlog-2.0}/devlog/__init__.py +3 -4
- python_devlog-2.0/devlog/base.py +184 -0
- python_devlog-2.0/devlog/custom_excepthook.py +47 -0
- python_devlog-2.0/devlog/decorators.py +212 -0
- python_devlog-2.0/devlog/sanitize.py +139 -0
- python_devlog-2.0/pyproject.toml +39 -0
- python_devlog-2.0/python_devlog.egg-info/PKG-INFO +227 -0
- python_devlog-2.0/python_devlog.egg-info/SOURCES.txt +19 -0
- python_devlog-2.0/requirements_dev.txt +3 -0
- python_devlog-2.0/tests/test_generic.py +529 -0
- python_devlog-2.0/tox.ini +21 -0
- python-devlog-1.1/PKG-INFO +0 -177
- python-devlog-1.1/README.md +0 -161
- python-devlog-1.1/devlog/custom_excepthook.py +0 -33
- python-devlog-1.1/devlog/decorator.py +0 -272
- python-devlog-1.1/devlog/stack_trace.py +0 -31
- python-devlog-1.1/pyproject.toml +0 -9
- python-devlog-1.1/python_devlog.egg-info/PKG-INFO +0 -177
- python-devlog-1.1/python_devlog.egg-info/SOURCES.txt +0 -12
- python-devlog-1.1/setup.py +0 -24
- {python-devlog-1.1 → python_devlog-2.0}/LICENSE.txt +0 -0
- {python-devlog-1.1 → python_devlog-2.0}/python_devlog.egg-info/dependency_links.txt +0 -0
- {python-devlog-1.1 → python_devlog-2.0}/python_devlog.egg-info/top_level.txt +0 -0
- {python-devlog-1.1 → python_devlog-2.0}/setup.cfg +0 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
name: Publish Python distributions to PyPI and TestPyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
deploy:
|
|
12
|
+
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
with:
|
|
18
|
+
fetch-depth: 0
|
|
19
|
+
- name: Set up Python
|
|
20
|
+
uses: actions/setup-python@v5
|
|
21
|
+
with:
|
|
22
|
+
python-version: '3.x'
|
|
23
|
+
- name: Install dependencies
|
|
24
|
+
run: |
|
|
25
|
+
python -m pip install --upgrade pip
|
|
26
|
+
pip install build setuptools-scm>=8
|
|
27
|
+
- name: Build package
|
|
28
|
+
run: python -m build
|
|
29
|
+
- name: Publish distribution to Test PyPI
|
|
30
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
31
|
+
with:
|
|
32
|
+
password: ${{ secrets.TEST_PYPI_API_TOKEN }}
|
|
33
|
+
repository-url: https://test.pypi.org/legacy/
|
|
34
|
+
skip-existing: true
|
|
35
|
+
- name: Publish distribution to PyPI
|
|
36
|
+
if: startsWith(github.ref, 'refs/tags')
|
|
37
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
38
|
+
with:
|
|
39
|
+
password: ${{ secrets.PYPI_API_TOKEN }}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
- push
|
|
5
|
+
- pull_request
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
test:
|
|
9
|
+
runs-on: ${{ matrix.os }}
|
|
10
|
+
strategy:
|
|
11
|
+
matrix:
|
|
12
|
+
os: [ubuntu-latest, windows-latest]
|
|
13
|
+
python-version: ['3.9', '3.10', '3.11', '3.12', '3.13']
|
|
14
|
+
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
18
|
+
uses: actions/setup-python@v5
|
|
19
|
+
with:
|
|
20
|
+
python-version: ${{ matrix.python-version }}
|
|
21
|
+
- name: Install dependencies
|
|
22
|
+
run: |
|
|
23
|
+
python -m pip install --upgrade pip
|
|
24
|
+
pip install pytest pytest-cov pytest-asyncio
|
|
25
|
+
pip install -e .
|
|
26
|
+
- name: Test with pytest
|
|
27
|
+
run: pytest
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
Change Log
|
|
2
|
+
==========
|
|
3
|
+
1.2 (13/06/2022)
|
|
4
|
+
----------------
|
|
5
|
+
- Add anti log the same exception twice
|
|
6
|
+
- Add custom traceback
|
|
7
|
+
- Now the module can be excluded from the traceback
|
|
8
|
+
|
|
9
|
+
1.1 (09/06/2022)
|
|
10
|
+
----------------
|
|
11
|
+
- Add unit testing and tested on python 3.6 to 3.10
|
|
12
|
+
- Stable release of the package
|
|
13
|
+
|
|
14
|
+
1.0b2 (21/05/2022)
|
|
15
|
+
------------------
|
|
16
|
+
- Change the decorator from camelCase to snake_case
|
|
17
|
+
- Treat the no parenthesis decorator as an empty parenthesis decorator
|
|
18
|
+
|
|
19
|
+
1.0b1 (21/05/2022)
|
|
20
|
+
------------------
|
|
21
|
+
- First Release
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: python-devlog
|
|
3
|
+
Version: 2.0
|
|
4
|
+
Summary: No more logging in your code business logic with decorators
|
|
5
|
+
Author-email: めがねこ <neko@meganeko.dev>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/MeGaNeKoS/devlog
|
|
8
|
+
Keywords: clean code,decorators,logging
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE.txt
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
[](https://github.com/MeGaNeKoS/devlog/releases/latest)
|
|
23
|
+
[](https://github.com/MeGaNeKoS/devlog/actions/workflows/python-test.yml)
|
|
24
|
+
[](https://github.com/MeGaNeKoS/devlog/actions/workflows/python-publish.yml)
|
|
25
|
+

|
|
26
|
+

|
|
27
|
+
|
|
28
|
+
devlog
|
|
29
|
+
=====
|
|
30
|
+
|
|
31
|
+
No more logging in your code business logic with python decorators.
|
|
32
|
+
|
|
33
|
+
Logging is a very powerful tool for debugging and monitoring your code. But if you are often adding logging
|
|
34
|
+
statements, you will quickly find your code overcrowded with them.
|
|
35
|
+
|
|
36
|
+
Fortunately, you can avoid this by using python decorators. This library provides easy logging for your code without
|
|
37
|
+
stealing readability and maintainability. It also provides stack traces with full local variables, value sanitization,
|
|
38
|
+
and async support.
|
|
39
|
+
|
|
40
|
+
**Requires Python 3.9+**
|
|
41
|
+
|
|
42
|
+
Installation
|
|
43
|
+
------------
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pip install python-devlog
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
How to use
|
|
50
|
+
----------
|
|
51
|
+
|
|
52
|
+
Add the decorator to your function. Depending on when you want to log, you can use:
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
import logging
|
|
56
|
+
from devlog import log_on_start, log_on_end, log_on_error
|
|
57
|
+
|
|
58
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@log_on_start
|
|
62
|
+
@log_on_end
|
|
63
|
+
def add(a, b):
|
|
64
|
+
return a + b
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@log_on_error
|
|
68
|
+
def divide(a, b):
|
|
69
|
+
return a / b
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
if __name__ == '__main__':
|
|
73
|
+
add(1, b=2)
|
|
74
|
+
# INFO:__main__:Start func add with args (1,), kwargs {'b': 2}
|
|
75
|
+
# INFO:__main__:Successfully run func add with args (1,), kwargs {'b': 2}
|
|
76
|
+
|
|
77
|
+
divide("abc", "def")
|
|
78
|
+
# ERROR:__main__:Error in func divide with args ('abc', 'def'), kwargs {}
|
|
79
|
+
# unsupported operand type(s) for /: 'str' and 'str'.
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Async support
|
|
83
|
+
|
|
84
|
+
All decorators work with async functions automatically:
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
@log_on_start
|
|
88
|
+
@log_on_end
|
|
89
|
+
@log_on_error
|
|
90
|
+
async def fetch_data(url):
|
|
91
|
+
...
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Value sanitization
|
|
95
|
+
|
|
96
|
+
Prevent sensitive values from appearing in logs using `Sensitive` or `sanitize_params`:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from devlog import log_on_start, Sensitive
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
# Option 1: Wrap the value — function receives the real value, logs show "***"
|
|
103
|
+
@log_on_start
|
|
104
|
+
def login(username, password):
|
|
105
|
+
...
|
|
106
|
+
|
|
107
|
+
login("admin", Sensitive("hunter2"))
|
|
108
|
+
# INFO:__main__:Start func login with args ('admin', '***'), kwargs {}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
# Option 2: Auto-redact by parameter name
|
|
112
|
+
@log_on_start(sanitize_params={"password", "token", "secret"})
|
|
113
|
+
def connect(host, token):
|
|
114
|
+
...
|
|
115
|
+
|
|
116
|
+
connect("example.com", "sk-abc123")
|
|
117
|
+
# INFO:__main__:Start func connect with args ('example.com', '***'), kwargs {}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
`Sensitive` is a transparent proxy — the wrapped function receives the real value. Only devlog log output is redacted.
|
|
121
|
+
|
|
122
|
+
What devlog can do for you
|
|
123
|
+
---------------------------
|
|
124
|
+
|
|
125
|
+
### Decorators
|
|
126
|
+
|
|
127
|
+
devlog provides three decorators:
|
|
128
|
+
|
|
129
|
+
- **log_on_start**: Log when the function is called.
|
|
130
|
+
- **log_on_end**: Log when the function finishes successfully.
|
|
131
|
+
- **log_on_error**: Log when the function raises an exception.
|
|
132
|
+
|
|
133
|
+
Use variables in messages
|
|
134
|
+
=========================
|
|
135
|
+
|
|
136
|
+
The message given to decorators is treated as a format string which takes the function arguments as format
|
|
137
|
+
arguments.
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
import logging
|
|
141
|
+
from devlog import log_on_start
|
|
142
|
+
|
|
143
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@log_on_start(logging.INFO, 'Start func {callable.__name__} with args {args}, kwargs {kwargs}')
|
|
147
|
+
def hello(name):
|
|
148
|
+
print("Hello, {}".format(name))
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
if __name__ == "__main__":
|
|
152
|
+
hello("World")
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Which will print:
|
|
156
|
+
```INFO:__main__:Start func hello with args ('World',), kwargs {}```
|
|
157
|
+
|
|
158
|
+
### Documentation
|
|
159
|
+
|
|
160
|
+
#### Format variables
|
|
161
|
+
|
|
162
|
+
The following variables are available in the format string:
|
|
163
|
+
|
|
164
|
+
| Default variable name | Description | LogOnStart | LogOnEnd | LogOnError |
|
|
165
|
+
|-----------------------|---------------------------------------------------------|------------|----------|------------|
|
|
166
|
+
| callable | The function object | Yes | Yes | Yes |
|
|
167
|
+
| *args/kwargs* | The arguments, key arguments passed to the function | Yes | Yes | Yes |
|
|
168
|
+
| result | The return value of the function | No | Yes | No |
|
|
169
|
+
| error | The error object if the function is finished with error | No | No | Yes |
|
|
170
|
+
|
|
171
|
+
#### Base arguments
|
|
172
|
+
|
|
173
|
+
Available arguments in all decorators:
|
|
174
|
+
|
|
175
|
+
| Argument | Description |
|
|
176
|
+
|--------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
177
|
+
| 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__)` |
|
|
178
|
+
| handler | A custom log handler object. Only available if no logger object is given. |
|
|
179
|
+
| args_kwargs | Set `True` to use `{args}`, `{kwargs}` format, or `False` to use function parameter names. Default `True` |
|
|
180
|
+
| callable_format_variable | The format variable name for the callable. Default is `callable` |
|
|
181
|
+
| trace_stack | Set to `True` to get the full stack trace. Default is `False` |
|
|
182
|
+
| capture_locals | Set to `True` to capture local variables in stack frames. Default is `False` (or `trace_stack` on log_on_error) |
|
|
183
|
+
| include_decorator | Set to `True` to include devlog frames in the stack trace. Default is `False` |
|
|
184
|
+
| sanitize_params | A set of parameter names to auto-redact in log messages. Default is `None` |
|
|
185
|
+
|
|
186
|
+
#### log_on_start
|
|
187
|
+
|
|
188
|
+
| Argument | Description |
|
|
189
|
+
|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
190
|
+
| level | The level of the log message. Default is `logging.INFO` |
|
|
191
|
+
| 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}` |
|
|
192
|
+
|
|
193
|
+
#### log_on_end
|
|
194
|
+
|
|
195
|
+
| Argument | Description |
|
|
196
|
+
|------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
197
|
+
| level | The level of the log message. Default is `logging.INFO` |
|
|
198
|
+
| 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}` |
|
|
199
|
+
| result_format_variable | The format variable name for the return value. Default is `result` |
|
|
200
|
+
|
|
201
|
+
#### log_on_error
|
|
202
|
+
|
|
203
|
+
| Argument | Description |
|
|
204
|
+
|---------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
205
|
+
| level | The level of the log message. Default is `logging.ERROR` |
|
|
206
|
+
| 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}` |
|
|
207
|
+
| on_exceptions | Exception classes to catch and log. Default catches all exceptions. |
|
|
208
|
+
| reraise | Whether to reraise the exception after logging. Default is `True` |
|
|
209
|
+
| exception_format_variable | The format variable name for the exception. Default is `error` |
|
|
210
|
+
|
|
211
|
+
### Extras
|
|
212
|
+
|
|
213
|
+
#### Custom exception hook
|
|
214
|
+
|
|
215
|
+
Override the default exception hook to write crash logs with local variable capture:
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
import devlog
|
|
219
|
+
|
|
220
|
+
devlog.system_excepthook_overwrite() # Overwrite the default exception hook
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
This replaces `sys.excepthook` with devlog's handler, which writes detailed crash information to a file.
|
|
224
|
+
|
|
225
|
+
| Argument | Description |
|
|
226
|
+
|----------|---------------------------------------------------------------|
|
|
227
|
+
| out_file | The path to the file to write the crash log. Default is `crash.log` |
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
[](https://github.com/MeGaNeKoS/devlog/releases/latest)
|
|
2
|
+
[](https://github.com/MeGaNeKoS/devlog/actions/workflows/python-test.yml)
|
|
3
|
+
[](https://github.com/MeGaNeKoS/devlog/actions/workflows/python-publish.yml)
|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
devlog
|
|
8
|
+
=====
|
|
9
|
+
|
|
10
|
+
No more logging in your code business logic with python decorators.
|
|
11
|
+
|
|
12
|
+
Logging is a very powerful tool for debugging and monitoring your code. But if you are often adding logging
|
|
13
|
+
statements, you will quickly find your code overcrowded with them.
|
|
14
|
+
|
|
15
|
+
Fortunately, you can avoid this by using python decorators. This library provides easy logging for your code without
|
|
16
|
+
stealing readability and maintainability. It also provides stack traces with full local variables, value sanitization,
|
|
17
|
+
and async support.
|
|
18
|
+
|
|
19
|
+
**Requires Python 3.9+**
|
|
20
|
+
|
|
21
|
+
Installation
|
|
22
|
+
------------
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pip install python-devlog
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
How to use
|
|
29
|
+
----------
|
|
30
|
+
|
|
31
|
+
Add the decorator to your function. Depending on when you want to log, you can use:
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import logging
|
|
35
|
+
from devlog import log_on_start, log_on_end, log_on_error
|
|
36
|
+
|
|
37
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@log_on_start
|
|
41
|
+
@log_on_end
|
|
42
|
+
def add(a, b):
|
|
43
|
+
return a + b
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@log_on_error
|
|
47
|
+
def divide(a, b):
|
|
48
|
+
return a / b
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
if __name__ == '__main__':
|
|
52
|
+
add(1, b=2)
|
|
53
|
+
# INFO:__main__:Start func add with args (1,), kwargs {'b': 2}
|
|
54
|
+
# INFO:__main__:Successfully run func add with args (1,), kwargs {'b': 2}
|
|
55
|
+
|
|
56
|
+
divide("abc", "def")
|
|
57
|
+
# ERROR:__main__:Error in func divide with args ('abc', 'def'), kwargs {}
|
|
58
|
+
# unsupported operand type(s) for /: 'str' and 'str'.
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Async support
|
|
62
|
+
|
|
63
|
+
All decorators work with async functions automatically:
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
@log_on_start
|
|
67
|
+
@log_on_end
|
|
68
|
+
@log_on_error
|
|
69
|
+
async def fetch_data(url):
|
|
70
|
+
...
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Value sanitization
|
|
74
|
+
|
|
75
|
+
Prevent sensitive values from appearing in logs using `Sensitive` or `sanitize_params`:
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
from devlog import log_on_start, Sensitive
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
# Option 1: Wrap the value — function receives the real value, logs show "***"
|
|
82
|
+
@log_on_start
|
|
83
|
+
def login(username, password):
|
|
84
|
+
...
|
|
85
|
+
|
|
86
|
+
login("admin", Sensitive("hunter2"))
|
|
87
|
+
# INFO:__main__:Start func login with args ('admin', '***'), kwargs {}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
# Option 2: Auto-redact by parameter name
|
|
91
|
+
@log_on_start(sanitize_params={"password", "token", "secret"})
|
|
92
|
+
def connect(host, token):
|
|
93
|
+
...
|
|
94
|
+
|
|
95
|
+
connect("example.com", "sk-abc123")
|
|
96
|
+
# INFO:__main__:Start func connect with args ('example.com', '***'), kwargs {}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`Sensitive` is a transparent proxy — the wrapped function receives the real value. Only devlog log output is redacted.
|
|
100
|
+
|
|
101
|
+
What devlog can do for you
|
|
102
|
+
---------------------------
|
|
103
|
+
|
|
104
|
+
### Decorators
|
|
105
|
+
|
|
106
|
+
devlog provides three decorators:
|
|
107
|
+
|
|
108
|
+
- **log_on_start**: Log when the function is called.
|
|
109
|
+
- **log_on_end**: Log when the function finishes successfully.
|
|
110
|
+
- **log_on_error**: Log when the function raises an exception.
|
|
111
|
+
|
|
112
|
+
Use variables in messages
|
|
113
|
+
=========================
|
|
114
|
+
|
|
115
|
+
The message given to decorators is treated as a format string which takes the function arguments as format
|
|
116
|
+
arguments.
|
|
117
|
+
|
|
118
|
+
```python
|
|
119
|
+
import logging
|
|
120
|
+
from devlog import log_on_start
|
|
121
|
+
|
|
122
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
@log_on_start(logging.INFO, 'Start func {callable.__name__} with args {args}, kwargs {kwargs}')
|
|
126
|
+
def hello(name):
|
|
127
|
+
print("Hello, {}".format(name))
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
if __name__ == "__main__":
|
|
131
|
+
hello("World")
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Which will print:
|
|
135
|
+
```INFO:__main__:Start func hello with args ('World',), kwargs {}```
|
|
136
|
+
|
|
137
|
+
### Documentation
|
|
138
|
+
|
|
139
|
+
#### Format variables
|
|
140
|
+
|
|
141
|
+
The following variables are available in the format string:
|
|
142
|
+
|
|
143
|
+
| Default variable name | Description | LogOnStart | LogOnEnd | LogOnError |
|
|
144
|
+
|-----------------------|---------------------------------------------------------|------------|----------|------------|
|
|
145
|
+
| callable | The function object | Yes | Yes | Yes |
|
|
146
|
+
| *args/kwargs* | The arguments, key arguments passed to the function | Yes | Yes | Yes |
|
|
147
|
+
| result | The return value of the function | No | Yes | No |
|
|
148
|
+
| error | The error object if the function is finished with error | No | No | Yes |
|
|
149
|
+
|
|
150
|
+
#### Base arguments
|
|
151
|
+
|
|
152
|
+
Available arguments in all decorators:
|
|
153
|
+
|
|
154
|
+
| Argument | Description |
|
|
155
|
+
|--------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
156
|
+
| 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__)` |
|
|
157
|
+
| handler | A custom log handler object. Only available if no logger object is given. |
|
|
158
|
+
| args_kwargs | Set `True` to use `{args}`, `{kwargs}` format, or `False` to use function parameter names. Default `True` |
|
|
159
|
+
| callable_format_variable | The format variable name for the callable. Default is `callable` |
|
|
160
|
+
| trace_stack | Set to `True` to get the full stack trace. Default is `False` |
|
|
161
|
+
| capture_locals | Set to `True` to capture local variables in stack frames. Default is `False` (or `trace_stack` on log_on_error) |
|
|
162
|
+
| include_decorator | Set to `True` to include devlog frames in the stack trace. Default is `False` |
|
|
163
|
+
| sanitize_params | A set of parameter names to auto-redact in log messages. Default is `None` |
|
|
164
|
+
|
|
165
|
+
#### log_on_start
|
|
166
|
+
|
|
167
|
+
| Argument | Description |
|
|
168
|
+
|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
169
|
+
| level | The level of the log message. Default is `logging.INFO` |
|
|
170
|
+
| 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}` |
|
|
171
|
+
|
|
172
|
+
#### log_on_end
|
|
173
|
+
|
|
174
|
+
| Argument | Description |
|
|
175
|
+
|------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
176
|
+
| level | The level of the log message. Default is `logging.INFO` |
|
|
177
|
+
| 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}` |
|
|
178
|
+
| result_format_variable | The format variable name for the return value. Default is `result` |
|
|
179
|
+
|
|
180
|
+
#### log_on_error
|
|
181
|
+
|
|
182
|
+
| Argument | Description |
|
|
183
|
+
|---------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
184
|
+
| level | The level of the log message. Default is `logging.ERROR` |
|
|
185
|
+
| 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}` |
|
|
186
|
+
| on_exceptions | Exception classes to catch and log. Default catches all exceptions. |
|
|
187
|
+
| reraise | Whether to reraise the exception after logging. Default is `True` |
|
|
188
|
+
| exception_format_variable | The format variable name for the exception. Default is `error` |
|
|
189
|
+
|
|
190
|
+
### Extras
|
|
191
|
+
|
|
192
|
+
#### Custom exception hook
|
|
193
|
+
|
|
194
|
+
Override the default exception hook to write crash logs with local variable capture:
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
import devlog
|
|
198
|
+
|
|
199
|
+
devlog.system_excepthook_overwrite() # Overwrite the default exception hook
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
This replaces `sys.excepthook` with devlog's handler, which writes detailed crash information to a file.
|
|
203
|
+
|
|
204
|
+
| Argument | Description |
|
|
205
|
+
|----------|---------------------------------------------------------------|
|
|
206
|
+
| out_file | The path to the file to write the crash log. Default is `crash.log` |
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
from .custom_excepthook import system_excepthook_overwrite
|
|
2
|
-
from .
|
|
3
|
-
from .
|
|
2
|
+
from .decorators import LogOnStart, LogOnError, LogOnEnd
|
|
3
|
+
from .sanitize import Sensitive
|
|
4
4
|
|
|
5
|
-
__all__ = ["log_on_start", "log_on_end", "log_on_error", "system_excepthook_overwrite", "
|
|
6
|
-
"set_stack_start_frames"]
|
|
5
|
+
__all__ = ["log_on_start", "log_on_end", "log_on_error", "system_excepthook_overwrite", "Sensitive"]
|
|
7
6
|
|
|
8
7
|
|
|
9
8
|
def log_on_start(*args, **kwargs):
|