diff options
author | Aaron LI <aaronly.me@outlook.com> | 2016-09-29 10:51:58 +0800 |
---|---|---|
committer | Aaron LI <aaronly.me@outlook.com> | 2016-09-29 10:51:58 +0800 |
commit | aa5842cf42a58ad28aa371384400325fc1bafe53 (patch) | |
tree | 3297286f610dec19858b8162629b2759440351c6 /fg21sim/configs | |
parent | 0aeeaf289ec31da902a9a5e614c1686baf880a29 (diff) | |
download | fg21sim-aa5842cf42a58ad28aa371384400325fc1bafe53.tar.bz2 |
configs/manager.py: Add property "logging"
This "logging" property get and perpare the configurations
for `logging.basicConfig()` use.
Needs testing.
Diffstat (limited to 'fg21sim/configs')
-rw-r--r-- | fg21sim/configs/manager.py | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/fg21sim/configs/manager.py b/fg21sim/configs/manager.py index 933a63d..2fee986 100644 --- a/fg21sim/configs/manager.py +++ b/fg21sim/configs/manager.py @@ -10,8 +10,10 @@ Configuration manager. """ import os +import sys from glob import glob from errors import ConfigError +import logging from configobj import ConfigObj, ConfigObjError, flatten_errors from validate import Validator @@ -30,7 +32,6 @@ class ConfigManager: spec = "\n".join([open(f).read() for f in configs_spec]).split("\n") self._configspec = ConfigObj(spec, interpolation=False, list_values=False, _inspec=True) - self._validator = Validator() configs_default = ConfigObj(configspec=self._configspec) self._config = self.validate(configs_default) if configs: @@ -43,8 +44,9 @@ class ConfigManager: self._config.merge(newconfig) def validate(self, config): + validator = Validator() try: - results = config.validate(self._validator, preserve_errors=True) + results = config.validate(validator, preserve_errors=True) except ConfigObjError as e: raise ConfigError(e.message) if not results: @@ -72,3 +74,27 @@ class ConfigManager: def set(self, key, value): self._config[key] = value + + @property + def logging(self): + """Get and prepare the logging configurations for + `logging.basicConfig()` + """ + from logging import FileHandler, StreamHandler + conf = self.get("logging") + # logging handlers + handlers = [] + stream = conf["stream"] + if stream: + handlers.append(StreamHandler(getattr(sys, stream))) + logfile = conf["filename"] + if logfile: + handlers.append(FileHandler(logfile, mode=conf["filemode"])) + # + logconf = { + "level": getattr(logging, conf["level"]), + "format": conf["format"], + "datefmt": conf["datefmt"], + "handlers": handlers, + } + return logconf |