From 82200bb150158ff00afb168aaefbc648a709629d Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 14 Apr 2018 00:59:56 +0200 Subject: [PATCH] Better logging management, solves #26 --- platypush/__init__.py | 2 +- platypush/config/__init__.py | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/platypush/__init__.py b/platypush/__init__.py index 8d686a8fc..f67f3c96a 100644 --- a/platypush/__init__.py +++ b/platypush/__init__.py @@ -52,7 +52,7 @@ class Daemon(object): self.processed_requests = 0 Config.init(self.config_file) - logging.basicConfig(level=Config.get('logging'), stream=sys.stdout) + logging.basicConfig(**Config.get('logging')) @classmethod def build_from_cmdline(cls, args): diff --git a/platypush/config/__init__.py b/platypush/config/__init__.py index fa761bed1..7caec1f9b 100644 --- a/platypush/config/__init__.py +++ b/platypush/config/__init__.py @@ -2,6 +2,7 @@ import datetime import logging import os import socket +import sys import time import yaml @@ -63,10 +64,23 @@ class Config(object): self._config['workdir'] = self._workdir_location os.makedirs(self._config['workdir'], exist_ok=True) - if 'logging' not in self._config: - self._config['logging'] = logging.INFO - else: - self._config['logging'] = getattr(logging, self._config['logging'].upper()) + logging_config = { + 'level': logging.INFO, + 'stream': sys.stdout, + '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: self._config['device_id'] = socket.gethostname()