Better logging management, solves #26

This commit is contained in:
Fabio Manganiello 2018-04-14 00:59:56 +02:00
parent e83b21ad33
commit 82200bb150
2 changed files with 19 additions and 5 deletions

View file

@ -52,7 +52,7 @@ class Daemon(object):
self.processed_requests = 0 self.processed_requests = 0
Config.init(self.config_file) Config.init(self.config_file)
logging.basicConfig(level=Config.get('logging'), stream=sys.stdout) logging.basicConfig(**Config.get('logging'))
@classmethod @classmethod
def build_from_cmdline(cls, args): def build_from_cmdline(cls, args):

View file

@ -2,6 +2,7 @@ import datetime
import logging import logging
import os import os
import socket import socket
import sys
import time import time
import yaml import yaml
@ -63,10 +64,23 @@ class Config(object):
self._config['workdir'] = self._workdir_location self._config['workdir'] = self._workdir_location
os.makedirs(self._config['workdir'], exist_ok=True) os.makedirs(self._config['workdir'], exist_ok=True)
if 'logging' not in self._config: logging_config = {
self._config['logging'] = logging.INFO 'level': logging.INFO,
else: 'stream': sys.stdout,
self._config['logging'] = getattr(logging, self._config['logging'].upper()) 'format': '%(asctime)-15s|%(levelname)5s|%(name)s|%(message)s',
}
if 'logging' in self._config:
for (k,v) in self._config['logging'].items():
logging_config[k] = v
if k == 'filename':
logfile = os.path.expanduser(v)
logdir = os.path.dirname(logfile)
os.makedirs(logdir, exist_ok=True)
logging_config[k] = logfile
del logging_config['stream']
self._config['logging'] = logging_config
if 'device_id' not in self._config: if 'device_id' not in self._config:
self._config['device_id'] = socket.gethostname() self._config['device_id'] = socket.gethostname()