-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
68 lines (57 loc) · 1.87 KB
/
logger.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
"""
This module provides a ThreadLogger class that wraps around the loguru.logger.
It allows to bind the log of a thread to a file path.
"""
import threading
from loguru import logger
class ThreadLogger:
"""
ThreadLogger is a wrapper around loguru.logger.
It allows to bind the log of a thread to a file path.
"""
_instance = None
_logger = None
_lock = threading.Lock()
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._logger = logger
cls._instance._logger.remove()
return cls._instance
def bind(self, thread_id: int, path: str, level: str = "INFO") -> None:
"""
Bind the log of a thread to a file path.
"""
with self._lock:
self._logger.add(
path,
filter=lambda record: record["extra"].get("thread") == thread_id,
level=level,
)
def info(self, message: str, *args, **kwargs) -> None:
"""
Log an info message.
"""
with self._lock:
tid = threading.current_thread().ident
self._logger.bind(thread=tid).info(message, *args, **kwargs)
def debug(self, message: str, *args, **kwargs) -> None:
"""
Log a debug message.
"""
with self._lock:
tid = threading.current_thread().ident
self._logger.bind(thread=tid).debug(message, *args, **kwargs)
def warning(self, message: str, *args, **kwargs) -> None:
"""
Log a warning message.
"""
with self._lock:
tid = threading.current_thread().ident
self._logger.bind(thread=tid).warning(message, *args, **kwargs)
def remove(self) -> None:
"""
Remove the logger.
"""
with self._lock:
self._logger.remove()