-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging_setup.py
200 lines (165 loc) · 5.05 KB
/
logging_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
""" Logging: A Usage Guide
Assume you have a folder structure like so:
```bash
.
├── LICENSE
├── example
│ └── yo.py
├── logging_setup.py
└── main.py
```
All you'd have to do to setup the logger would be to:
```python
# in main.py
from logging_setup import setup_log
setup_log()
...
# in example/yo.py
from logging import log
log(50, "yo")
```
This will result in a `.logs/warnings.log` file being created, with each line
having an output similar to:
```
[2020-06-02 12:03:24,171] CRITICAL { /path/to/yo.py:3 } - yo
```
The `/path/to/yo.py:3` format is very useful, as you can just ctrl-click it to
open it directly in VSC!
Note: There's no getLogger shenanigans!
Note 2: The rotating file handler ensures no log file will be over 5MB long!
This is the default maximum for VSC, as it'll get quite confused if a text
file is longer than 5MB
## Using the wrapper
Using the wrapper will allow us to quickly inspect all function calls!
It's basically a fancy print
* Usage:
```python
from logging_setup import log_wrapper
@log_wrapper()
def yo(arg, key_arg=0):
return arg + key_arg
yo(0)
yo(0, key_arg=5)
yo('aze')
```
Would produce:
```python
[2020-06-02 ...] WARNING { /path/to:118 }
Function yo called with following params: (0,), {}
[2020-06-02 12:51:04,205] WARNING { /path/to:118 }
Function yo called with following params: (0,), {'key_arg': 5}
[2020-06-02 12:51:04,206] WARNING { /path/to:118 }
Function yo called with following params: ('aze',), {}
[2020-06-02 12:51:04,206] ERROR { /path/to:122 }
can only concatenate str (not "int") to str
Traceback (most recent call last):
File "/path/to", line 120, in wrapper
return func(*args, **kwargs)
File "/path/to", line 6, in yo
return arg + key_arg
TypeError: can only concatenate str (not "int") to str
```
"""
from functools import wraps, partial
import logging
from logging import WARNING
import logging.config
from typing import Dict, Any, Callable, Optional
from pathlib import Path
Logging_Keys = Any
λ = Callable[..., Any]
_default_log_file = "./logs/warnings.log"
_default_file_log_level = "WARNING"
_default_console_log_level = "DEBUG"
# Saying it's a Dict: Any is not very useful,
# Check out the proper documentation over at:
# https://docs.python.org/3/library/logging.config.html?#configuration-dictionary-schema
CONFIG: Dict[str, Logging_Keys] = {
"disable_existing_loggers": False,
"version": 1,
"formatters": {
"short": {
"format": (
"[%(asctime)s] %(levelname)s "
"{ %(pathname)s:%(lineno)d }\n\t%(message)s"
)
},
},
"handlers": {
"console": {
"level": _default_console_log_level,
"formatter": "short",
"class": "logging.StreamHandler",
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"level": _default_file_log_level,
"formatter": "short",
"filename": "./logs/warnings.log",
"maxBytes": 5_242_880, # 5MB
"backupCount": 10,
"encoding": "utf8",
},
},
"loggers": {
"": {
"handlers": ["console", "file"],
"level": "DEBUG",
},
"plugins": {
"handlers": ["console", "file"],
"level": "DEBUG",
"propagate": False,
},
},
}
def setup_log() -> None:
# Will create the log file and directory if it doesn't exist
filename = Path(_default_log_file)
filename.parent.mkdir(parents=True, exist_ok=True)
filename.touch(exist_ok=True)
logging.config.dictConfig(CONFIG)
def _attach_wrapper(obj: Any, func: Optional[λ] = None) -> λ:
"""Helper function that attaches function as attribute of an object"""
if func is None:
return partial(_attach_wrapper, obj)
setattr(obj, func.__name__, func)
return func
def log_wrapper(level: int = WARNING) -> λ:
"""Wrap any function so you can log all function calls! Wow
Usage example:
```python
from logging_setup import log_wrapper
@log_wrapper(50) # That would be critical level
def yo(arg, key_arg=0):
return arg + key_arg
yo(0)
yo(0, key_arg=5)
yo('aze')
```
"""
def decorate(func: λ) -> λ:
setup_log()
logger = logging.getLogger(func.__module__)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
logger.log(
level,
(
f"Function {func.__name__} called with "
f"following params: {args}, {kwargs}"
),
)
try:
return func(*args, **kwargs)
except BaseException as e:
logger.error(e, exc_info=True)
raise e
return wrapper
return decorate