11import os
22import logging .config
33import logging
4+ from pathlib import Path
45import yaml
56import harmonic
6- import colorlog
7+
78
89def setup_logging (custom_yaml_path = None , default_level = logging .DEBUG ):
910 """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
1314 debug logging and 50 is critical logging.
1415
1516 Args:
@@ -24,35 +25,21 @@ def setup_logging(custom_yaml_path=None, default_level=logging.DEBUG):
2425 ValueError: Raised if logging.yaml is not in ./logs/ directory.
2526
2627 """
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"
5232 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 )
5643
5744
5845def debug_log (message ):
@@ -63,21 +50,23 @@ def debug_log(message):
6350 message: Message to log.
6451
6552 """
66- logger = logging .getLogger (' Harmonic' )
53+ logger = logging .getLogger (" Harmonic" )
6754 logger .debug (message )
6855
56+
6957def 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
7159 ranges).
7260
7361 Args:
7462
7563 message: Warning to log.
7664
7765 """
78- logger = logging .getLogger (' Harmonic' )
66+ logger = logging .getLogger (" Harmonic" )
7967 logger .warning (message )
8068
69+
8170def critical_log (message ):
8271 """Log a critical message (e.g. core code failures etc).
8372
@@ -86,18 +75,18 @@ def critical_log(message):
8675 message: Message to log.
8776
8877 """
89- logger = logging .getLogger (' Harmonic' )
78+ logger = logging .getLogger (" Harmonic" )
9079 logger .critical (message )
9180
81+
9282def 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
9484 etc).
9585
9686 Args:
9787
9888 message: Message to log.
9989
10090 """
101- logger = logging .getLogger (' Harmonic' )
91+ logger = logging .getLogger (" Harmonic" )
10292 logger .info (message )
103-
0 commit comments