Skip to content

Commit 2283600

Browse files
committed
Added option to load logging conf from yaml files. Not entirely sure about the fine details of its behaviour though.
1 parent 8c32c90 commit 2283600

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

simpledaemonlog/logsetup.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
import time
88
import sys
99
import logging
10+
import logging.config
11+
import yaml
12+
13+
import logging
14+
log = logging.getLogger(__name__)
1015

1116
DEFAULT_FORMAT_STRING = '%(asctime)s|%(levelname)8s|%(module)20s|%(lineno)4s| %(message)s'
1217
COLORED_FORMAT_STRING = '%(log_color)s%(asctime)s%(reset)s|%(module)20s|%(lineno)4s| %(log_color)s%(message)s'
@@ -35,7 +40,18 @@ def filter(self, record):
3540
return True
3641

3742

38-
def setup_console(level=logging.NOTSET, fs=DEFAULT_FORMAT_STRING, color=False):
43+
def _load_settings(path=None):
44+
if path is not None and os.path.exists(path):
45+
with open(path, 'rt') as f:
46+
config = yaml.load(f.read())
47+
logging.config.dictConfig(config)
48+
return path
49+
return None
50+
51+
52+
def setup_console(level=logging.NOTSET, fs=DEFAULT_FORMAT_STRING, settings=None, color=False):
53+
loaded = _load_settings(settings)
54+
3955
console = logging.StreamHandler()
4056
console.addFilter(PrintfFilter())
4157

@@ -63,15 +79,19 @@ def setup_console(level=logging.NOTSET, fs=DEFAULT_FORMAT_STRING, color=False):
6379
rootlogger = logging.getLogger("")
6480
rootlogger.setLevel(min(level, rootlogger.getEffectiveLevel()))
6581
rootlogger.addHandler(console)
82+
if loaded:
83+
log.debug("logging config loaded from {:s}".format(loaded))
6684

6785

68-
def setup_file(application_name, logdir="log", level=logging.NOTSET, fs=DEFAULT_FORMAT_STRING):
86+
def setup_file(application_name, logdir="log", level=logging.NOTSET, fs=DEFAULT_FORMAT_STRING, settings=None):
6987
"""
7088
Directs printf to file with INFO level.
7189
"""
7290
if not os.path.isdir(logdir):
7391
os.makedirs(logdir)
7492

93+
loaded = _load_settings(settings)
94+
7595
utc = time.gmtime()
7696
ts = time.strftime("%Y%m%d_%H%M%S%Z", utc)
7797

@@ -84,7 +104,8 @@ def setup_file(application_name, logdir="log", level=logging.NOTSET, fs=DEFAULT_
84104
if os.path.islink(loglinkpath):
85105
os.unlink(loglinkpath)
86106

87-
os.symlink(logfilename, loglinkpath)
107+
if hasattr(os, "symlink"):
108+
os.symlink(logfilename, loglinkpath)
88109

89110
formatter = logging.Formatter(fs)
90111
logfile.setFormatter(formatter)
@@ -96,7 +117,8 @@ def setup_file(application_name, logdir="log", level=logging.NOTSET, fs=DEFAULT_
96117

97118
sys.stderr = StdLogger(sys.stderr, logging.error)
98119
sys.stdout = StdLogger(sys.stdout, logging.info)
99-
120+
if loaded:
121+
log.debug("logging config loaded from {:s}".format(loaded))
100122

101123
if __name__ == "__main__":
102124
setup_console()

0 commit comments

Comments
 (0)