-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_logging.py
More file actions
94 lines (84 loc) · 2.74 KB
/
config_logging.py
File metadata and controls
94 lines (84 loc) · 2.74 KB
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
import logging
import logging.config
import os
from multiprocessing import current_process
log_config = {
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"console_formatter": {
"format": "%(levelname)s - %(message)s"
},
"file_formatter": {
"format": "[%(asctime)s] %(levelname)s - %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
}
},
"handlers": {
"console_handler": {
"level": "INFO",
"formatter": "console_formatter",
"class": "logging.StreamHandler",
"stream": "ext://sys.stdout",
},
"file_handler": {
"level": "DEBUG",
"formatter": "file_formatter",
"class": "logging.FileHandler",
"filename": "",
"mode": "a",
"encoding": "utf-8"
}
},
"loggers": {
"main": {
"handlers": ["console_handler", "file_handler"],
"level": "DEBUG",
"propagate": False
},
"supercell_worker": {
"handlers": ["file_handler"],
"level": "DEBUG",
"propagate": False
},
"collect_worker": {
"handlers": ["file_handler"],
"level": "DEBUG",
"propagate": False
}
}
}
def configure_logging(result_path: str, log_filename: str = "main") -> None:
"""
Configure logging for the main process and the supercell and collect workers.
:param result_path: Logs will be saved in result_path/logs.
:param log_filename: Log filename.
:return: None.
"""
os.makedirs(os.path.join(result_path, "logs"), exist_ok=True)
log_config["handlers"]["file_handler"]["filename"] = os.path.join(result_path, "logs", f"{log_filename}.log")
logging.config.dictConfig(log_config)
def get_main_logger(result_path: str) -> logging.Logger:
"""
Get the main logger.
:param result_path: Logs will be saved in result_path/logs.
:return: Logger.
"""
configure_logging(result_path)
return logging.getLogger("main")
def get_supercell_worker_logger(result_path: str) -> logging.Logger:
"""
Get the supercell worker logger.
:param result_path: Logs will be saved in result_path/logs.
:return: Logger.
"""
configure_logging(result_path, f"supercell_{current_process().name}")
return logging.getLogger("supercell_worker")
def get_collect_worker_logger(result_path: str) -> logging.Logger:
"""
Get the collect worker logger.
:param result_path: Logs will be saved in result_path/logs.
:return: Logger.
"""
configure_logging(result_path, f"collect_{current_process().name}")
return logging.getLogger("collect_worker")