-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomLog.py
41 lines (29 loc) · 1.24 KB
/
customLog.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
import logging
import os
import yaml
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)),"config.yaml"), "r") as yaml_file:
config = yaml.load(yaml_file, Loader=yaml.FullLoader)
os.makedirs(config['output'],exist_ok=True)
class ContextLoggerAdapter(logging.LoggerAdapter):
def process(self, msg, kwargs):
return "[%s] %s" % (self.extra["database"], msg), kwargs
def set_context(logger, database):
logger = ContextLoggerAdapter(logger, {"database": database})
return logger
FORMAT = "%(levelname)s - %(message)s - %(asctime)s"
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(format=FORMAT, datefmt=DATE_FORMAT)
# Create the logger
logger = logging.getLogger("orenza")
logger.setLevel(logging.INFO)
def get_logger():
return logger
# Create file which log our messages
fh_detailed = logging.FileHandler(os.path.join(config['output'],"orenza_detailed.log"))
fh_detailed.setLevel(logging.INFO)
fh_detailed.setFormatter(logging.Formatter(fmt=FORMAT, datefmt=DATE_FORMAT))
fh_error = logging.FileHandler(os.path.join(config['output'],"orenza_error.log"))
fh_error.setLevel(logging.ERROR)
fh_error.setFormatter(logging.Formatter(fmt=FORMAT, datefmt=DATE_FORMAT))
logger.addHandler(fh_detailed)
logger.addHandler(fh_error)