-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_setup.py
57 lines (45 loc) · 1.99 KB
/
logger_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
import logging
import sys
from logging.handlers import TimedRotatingFileHandler
from pathlib import Path
def get_console_handler() -> logging.handlers:
"""
The console sends only messages by default no need for formatter.
"""
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
return console_handler
def get_file_handler(log_dir: Path, log_format: logging.Formatter) -> logging.handlers:
log_file = log_dir / "runtime.log"
file_handler = TimedRotatingFileHandler(log_file, when='midnight', interval=1, backupCount=7, encoding='utf-8')
file_handler.namer = log_namer
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(log_format)
return file_handler
def setup_logging() -> None:
"""
Use the following to add logger to other modules.
import logging
logger = logging.getLogger(__name__)
The following suppress log messages. It will not log messages of given module unless they are at least warnings.
logging.getLogger("module_name").setLevel(logging.WARNING)
"""
# Create folder for file logs.
log_dir = Path("logs")
log_dir.mkdir(exist_ok=True)
# Create a custom logger.
logger = logging.getLogger()
logger.setLevel(logging.DEBUG) # Better to have too much log than not enough.
# Create formatters and add it to handlers.
log_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# Add handlers to the logger.
logger.addHandler(get_console_handler())
logger.addHandler(get_file_handler(log_dir, log_format))
def log_namer(default_name: str) -> str:
"""
This will be called when doing the log rotation
default_name is the default filename that would be assigned, e.g. Rotate_Test.txt.YYYY-MM-DD
Do any manipulations to that name here, for example this function changes the name to Rotate_Test.YYYY-MM-DD.txt
"""
base_filename, ext, date = default_name.split(".")
return f"{base_filename}.{date}.{ext}"