forked from SitaoLuan/ACM-GNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseLogger.py
52 lines (38 loc) · 1.39 KB
/
BaseLogger.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
from abc import ABC, abstractmethod
from datetime import datetime
import logging
from pathlib import Path
from sys import stdout
class BaseLogger(ABC):
def __init__(self, name, log_dir="./logs/", log_level="INFO"):
Path(log_dir).mkdir(parents=True, exist_ok=True)
log_name = log_dir + datetime.now().strftime(f"{name}file_%Y_%m_%d_%H_%M.log")
self.logger = logging.getLogger(log_name)
self.stream_handler = logging.StreamHandler(stdout)
self.file_handler = logging.FileHandler(log_name)
self._set_logger(log_level)
def _set_logger(self, log_level):
log_format = logging.Formatter(
"%(asctime)s: %(message)s", "%Y-%m-%d %H:%M:%S"
)
log_level = getattr(logging, log_level)
self.logger.setLevel(log_level)
self.stream_handler.setFormatter(log_format)
self.logger.addHandler(self.stream_handler)
self.file_handler.setFormatter(log_format)
self.logger.addHandler(self.file_handler)
def log_init(self, msg):
msg = f"Init - {msg}"
self.logger.info(msg)
@abstractmethod
def log_best_result(self, **kwargs):
pass
@abstractmethod
def log_run(self, **kwargs):
pass
@abstractmethod
def log_param_tune(self, **kwargs):
pass
def log_time(self, msg):
msg = f"Total time elapsed - {msg}"
self.logger.info(msg)