1
1
import os
2
2
import logging .config
3
3
import logging
4
+ from pathlib import Path
4
5
import yaml
5
6
import harmonic
6
- import colorlog
7
+
7
8
8
9
def setup_logging (custom_yaml_path = None , default_level = logging .DEBUG ):
9
10
"""initialise and configure logging.
10
-
11
- Should be called at the beginning of code to initialise and configure the
12
- desired logging level. Logging levels can be ints in [0,50] where 10 is
11
+
12
+ Should be called at the beginning of code to initialise and configure the
13
+ desired logging level. Logging levels can be ints in [0,50] where 10 is
13
14
debug logging and 50 is critical logging.
14
15
15
16
Args:
@@ -24,35 +25,21 @@ def setup_logging(custom_yaml_path=None, default_level=logging.DEBUG):
24
25
ValueError: Raised if logging.yaml is not in ./logs/ directory.
25
26
26
27
"""
27
- if custom_yaml_path == None :
28
- path = os .path .join (os .path .dirname (os .path .dirname (
29
- os .path .realpath (harmonic .__file__ ))) + '/logs/logging.yaml' )
30
- if custom_yaml_path != None :
31
- path = custom_yaml_path
32
- value = os .getenv ('LOG_CFG' , None )
33
- if value :
34
- path = value
35
- if os .path .exists (path ):
36
- with open (path , 'rt' ) as f :
37
- config = yaml .safe_load (f .read ())
38
- if custom_yaml_path == None :
39
- config ['handlers' ]['info_file_handler' ]['filename' ] = os .path .join (
40
- os .path .dirname (os .path .dirname (
41
- os .path .realpath (harmonic .__file__ ))) + '/logs/info.log' )
42
- config ['handlers' ]['debug_file_handler' ]['filename' ] = os .path .join (
43
- os .path .dirname (os .path .dirname (
44
- os .path .realpath (harmonic .__file__ ))) + '/logs/debug.log' )
45
- config ['handlers' ]['critical_file_handler' ]['filename' ] = os .path .join (
46
- os .path .dirname (os .path .dirname (
47
- os .path .realpath (harmonic .__file__ ))) + '/logs/critical.log' )
48
- config ['handlers' ]['info_file_handler' ]['filename' ] = os .path .join (
49
- os .path .dirname (os .path .dirname (
50
- os .path .realpath (harmonic .__file__ ))) + '/logs/info.log' )
51
- logging .config .dictConfig (config )
28
+ if "LOG_CFG" in os .environ :
29
+ path = Path (os .environ ["LOG_CFG" ])
30
+ elif custom_yaml_path is None :
31
+ path = Path (harmonic .__file__ ).parent / "default-logging-config.yaml"
52
32
else :
53
- logging .basicConfig (level = default_level )
54
- raise ValueError ("Logging config pathway incorrect." )
55
- critical_log ('Using custom config from {}' .format (path ))
33
+ path = Path (custom_yaml_path )
34
+ if not path .exists ():
35
+ raise ValueError (f"Logging config path { path } does not exist." )
36
+ with open (path , "rt" ) as f :
37
+ config = yaml .safe_load (f .read ())
38
+ if custom_yaml_path is None :
39
+ config ["handlers" ]["info_file_handler" ]["filename" ] = "info.log"
40
+ config ["handlers" ]["debug_file_handler" ]["filename" ] = "debug.log"
41
+ config ["handlers" ]["critical_file_handler" ]["filename" ] = "critical.log"
42
+ logging .config .dictConfig (config )
56
43
57
44
58
45
def debug_log (message ):
@@ -63,21 +50,23 @@ def debug_log(message):
63
50
message: Message to log.
64
51
65
52
"""
66
- logger = logging .getLogger (' Harmonic' )
53
+ logger = logging .getLogger (" Harmonic" )
67
54
logger .debug (message )
68
55
56
+
69
57
def warning_log (message ):
70
- """Log a warning (e.g. for internal code warnings such as large dynamic
58
+ """Log a warning (e.g. for internal code warnings such as large dynamic
71
59
ranges).
72
60
73
61
Args:
74
62
75
63
message: Warning to log.
76
64
77
65
"""
78
- logger = logging .getLogger (' Harmonic' )
66
+ logger = logging .getLogger (" Harmonic" )
79
67
logger .warning (message )
80
68
69
+
81
70
def critical_log (message ):
82
71
"""Log a critical message (e.g. core code failures etc).
83
72
@@ -86,18 +75,18 @@ def critical_log(message):
86
75
message: Message to log.
87
76
88
77
"""
89
- logger = logging .getLogger (' Harmonic' )
78
+ logger = logging .getLogger (" Harmonic" )
90
79
logger .critical (message )
91
80
81
+
92
82
def info_log (message ):
93
- """Log an information message (e.g. evidence value printing, run completion
83
+ """Log an information message (e.g. evidence value printing, run completion
94
84
etc).
95
85
96
86
Args:
97
87
98
88
message: Message to log.
99
89
100
90
"""
101
- logger = logging .getLogger (' Harmonic' )
91
+ logger = logging .getLogger (" Harmonic" )
102
92
logger .info (message )
103
-
0 commit comments