-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogs.py
112 lines (83 loc) · 3.15 KB
/
logs.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
112
import os
import logging.config
import logging
import yaml
import ProxNest
import colorlog
def setup_logging(custom_yaml_path=None, default_level=logging.DEBUG):
"""initialise and configure logging.
Should be called at the beginning of code to initialise and configure the
desired logging level. Logging levels can be ints in [0,50] where 10 is
debug logging and 50 is critical logging.
Args:
custom_yaml_path (string): Complete pathname of desired yaml logging
configuration. If empty will provide default logging config.
default_level (int): Logging level at which to configure.
Raises:
ValueError: Raised if logging.yaml is not in ./logs/ directory.
"""
if custom_yaml_path == None:
path = os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(ProxNest.__file__)))
+ "/logs/logging.yaml"
)
if custom_yaml_path != None:
path = custom_yaml_path
value = os.getenv("LOG_CFG", None)
if value:
path = value
if os.path.exists(path):
with open(path, "rt") as f:
config = yaml.safe_load(f.read())
if custom_yaml_path == None:
config["handlers"]["info_file_handler"]["filename"] = os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(ProxNest.__file__)))
+ "/logs/info.log"
)
config["handlers"]["debug_file_handler"]["filename"] = os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(ProxNest.__file__)))
+ "/logs/debug.log"
)
config["handlers"]["critical_file_handler"]["filename"] = os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(ProxNest.__file__)))
+ "/logs/critical.log"
)
config["handlers"]["info_file_handler"]["filename"] = os.path.join(
os.path.dirname(os.path.dirname(os.path.realpath(ProxNest.__file__)))
+ "/logs/info.log"
)
logging.config.dictConfig(config)
else:
logging.basicConfig(level=default_level)
raise ValueError("Logging config pathway incorrect.")
critical_log("Using custom config from {}".format(path))
def debug_log(message):
"""Log a debug message (e.g. for background logs to assist debugging).
Args:
message: Message to log.
"""
logger = logging.getLogger("ProxNest")
logger.debug(message)
def warning_log(message):
"""Log a warning (e.g. for internal code warnings such as large dynamic
ranges).
Args:
message: Warning to log.
"""
logger = logging.getLogger("ProxNest")
logger.warning(message)
def critical_log(message):
"""Log a critical message (e.g. core code failures etc).
Args:
message: Message to log.
"""
logger = logging.getLogger("ProxNest")
logger.critical(message)
def info_log(message):
"""Log an information message (e.g. evidence value printing, run completion
etc).
Args:
message: Message to log.
"""
logger = logging.getLogger("ProxNest")
logger.info(message)