-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
83 lines (61 loc) · 2.63 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import logging
from logging.handlers import RotatingFileHandler
from pathlib import Path
LOG_FILENAME = Path.cwd() / "logs" / "adclicker.log"
LOG_FILENAME.parent.mkdir(exist_ok=True)
# Create a custom logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Create handlers
console_handler = logging.StreamHandler()
file_handler = RotatingFileHandler(
LOG_FILENAME, maxBytes=20971520, encoding="utf-8", backupCount=50
)
console_handler.setLevel(logging.INFO)
file_handler.setLevel(logging.DEBUG)
# Create formatters and add it to handlers
console_log_format = "%(asctime)s [%(levelname)5s] %(lineno)3d: %(message)s"
file_log_format = "%(asctime)s [%(levelname)5s] %(filename)s:%(lineno)3d: %(message)s"
console_formatter = logging.Formatter(console_log_format, datefmt="%d-%m-%Y %H:%M:%S")
console_handler.setFormatter(console_formatter)
file_formatter = logging.Formatter(file_log_format, datefmt="%d-%m-%Y %H:%M:%S")
file_handler.setFormatter(file_formatter)
# Add handlers to the logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
class MultiprocessLogFilter(logging.Filter):
"""Custom log filter for multiple browser runs
:type browser_id: str
:param browser_id: Id for browser instance
"""
def __init__(self, browser_id: str, *args, **kwargs):
super().__init__(*args, **kwargs)
self.browser_id = browser_id
def filter(self, record: logging.LogRecord) -> bool:
"""Register custom keyword for log formatters
:type record: logging.LogRecord
:param record: LogRecord instance
:rtype: bool
:returns: True
"""
record.browser_id = self.browser_id
return True
def update_log_formats(browser_id: str) -> None:
"""Update log formats for multiprocess runs
:type browser_id: str
:param browser_id: Id for browser instance
"""
logger.removeHandler(console_handler)
logger.removeHandler(file_handler)
console_log_format = "%(asctime)s <<%(browser_id)s>> [%(levelname)5s] %(lineno)3d: %(message)s"
file_log_format = (
"%(asctime)s <<%(browser_id)s>> [%(levelname)5s] %(filename)s:%(lineno)3d: %(message)s"
)
console_formatter = logging.Formatter(console_log_format, datefmt="%d-%m-%Y %H:%M:%S")
console_handler.setFormatter(console_formatter)
file_formatter = logging.Formatter(file_log_format, datefmt="%d-%m-%Y %H:%M:%S")
file_handler.setFormatter(file_formatter)
console_handler.addFilter(MultiprocessLogFilter(browser_id))
file_handler.addFilter(MultiprocessLogFilter(browser_id))
logger.addHandler(console_handler)
logger.addHandler(file_handler)