-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogconfig.py
111 lines (102 loc) · 3.57 KB
/
logconfig.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding:utf-8 -*-
import json
import yaml
import logging
import logging.config
import os
default_config = {
"version": 1,
"incremental": False,
"disable_existing_loggers": True,
"formatters": {
"default": {
"format": "%(name)s %(asctime)s [%(filename)s %(funcName)s()] <%(levelname)s>: %(message)s",
},
"brief": {
"format": "%(name)s %(asctime)s [%(funcName)s() %(lineno)d] <%(levelname)s>: %(message)s",
}
},
"filters": {},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "brief",
"level": "DEBUG",
"stream": "ext://sys.stdout",
},
"file_detail": {
"class": "logging.handlers.RotatingFileHandler",
"formatter": "default",
"level": "DEBUG",
"filename": "./log/detail_logger.log",
"encoding": "utf8",
"maxBytes": 10485760,
"backupCount": 10,
},
"file_info": {
"class": "logging.handlers.RotatingFileHandler",
"formatter": "default",
"level": "INFO",
"filename": "./log/info_logger.log",
"encoding": "utf8",
"maxBytes": 10485760,
"backupCount": 10,
}
},
"loggers": {
"cov2": {
"level": "DEBUG",
"handlers": ["console", "file_detail", "file_info"],
"propagate": False,
}
},
"root": {
"level": "DEBUG",
"handlers": ["console", "file_detail"],
},
}
def setup_logging(
default_log_config=None,
is_yaml=True,
default_level=logging.INFO,
env_key='LOG_CFG',
):
"""Setup logging configuration
Call this only once from the application main() function or __main__ module!
This will configure the python logging module based on a logging configuration
in the following order of priority:
1. Log configuration file found in the environment variable specified in the `env_key` argument.
2. Log configuration file found in the `default_log_config` argument.
3. Default log configuration found in the `logconfig.default_config` dict.
4. If all of the above fails: basicConfig is called with the `default_level` argument.
Args:
default_log_config (Optional[str]): Path to log configuration file.
env_key (Optional[str]): Environment variable that can optionally contain
a path to a configuration file.
default_level (int): logging level to set as default. Ignored if a log
configuration is found elsewhere.
is_yaml (bool): weather config file is a yaml file
Returns: None
"""
dict_config = None
logconfig_filename = default_log_config
env_var_value = os.getenv(env_key, None)
if env_var_value is not None:
logconfig_filename = env_var_value
if default_config is not None:
dict_config = default_config
if logconfig_filename is not None and os.path.exists(logconfig_filename):
with open(logconfig_filename, 'rt', encoding="utf8") as f:
if is_yaml:
file_config = yaml.load(f, Loader=yaml.FullLoader)
else:
file_config = json.load(f)
if file_config is not None:
dict_config = file_config
if dict_config is not None:
logging.config.dictConfig(dict_config)
else:
logging.basicConfig(level=default_level)
if __name__ == '__main__':
with open("./logconfig.yaml", "w") as f:
yaml.dump(default_config, f)