-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracing.py
55 lines (41 loc) · 1.56 KB
/
Tracing.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
from loguru import logger
LOG_LEVEL_DEBUG = "DEBUG"
LOG_LEVEL_INFO = "INFO"
LOG_LEVEL_WARNING = "WARNING"
LOG_LEVEL_ERROR = "ERROR"
LOG_LEVELS = [
LOG_LEVEL_DEBUG, LOG_LEVEL_ERROR, LOG_LEVEL_INFO, LOG_LEVEL_WARNING
]
class LogTrace:
def __init__(self, logger_name, log_file_path, log_level):
if log_file_path is None:
raise RuntimeError('log_file_path must be defined')
if log_level not in LOG_LEVELS:
raise RuntimeError(f'log_level must be in {LOG_LEVELS}')
logger.add(
log_file_path,
level=log_level,
format=
"{time:YYYY-MM-DD HH:mm:ss} | {level} | {module}.{function}:{line} | {message}",
rotation="10 MB",
filter=lambda record: record["extra"].get("logger_name") == logger_name,
)
self.logger = logger.bind(logger_name=logger_name)
def log(self, level, message, **kwargs):
self.logger.opt(depth=2, exception=None,
lazy=False).log(level, message, **kwargs)
def error(self, message, **kwargs):
self.log(LOG_LEVEL_ERROR, message, **kwargs)
def warning(self, message, **kwargs):
self.log(LOG_LEVEL_WARNING, message, **kwargs)
def info(self, message, **kwargs):
self.log(LOG_LEVEL_INFO, message, **kwargs)
def debug(self, message, **kwargs):
self.log(LOG_LEVEL_DEBUG, message, **kwargs)
def close(self):
self.logger.complete()
self.logger.remove()
#Quick local test for manual checking
if __name__ == "__main__":
tracer = LogTrace('test_tracer', "Local_test_file.txt", LOG_LEVEL_DEBUG)
tracer.error("An error occurred. {error_code}", error_code=123)