From 6c2273387d0317a35f56167c37c291f5ef1c4124 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Wed, 3 May 2017 21:30:33 +0300 Subject: [PATCH] util: extend logging.basicConfig() --- libagent/util.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/libagent/util.py b/libagent/util.py index 6c397aa..c10b0b6 100644 --- a/libagent/util.py +++ b/libagent/util.py @@ -179,13 +179,22 @@ class Reader(object): self._captured = None -def setup_logging(verbosity, **kwargs): +def setup_logging(verbosity, filename=None): """Configure logging for this tool.""" - fmt = ('%(asctime)s %(levelname)-12s %(message)-100s ' - '[%(filename)s:%(lineno)d]') levels = [logging.WARNING, logging.INFO, logging.DEBUG] level = levels[min(verbosity, len(levels) - 1)] - logging.basicConfig(format=fmt, level=level, **kwargs) + logging.root.setLevel(level) + + fmt = logging.Formatter('%(asctime)s %(levelname)-12s %(message)-100s ' + '[%(filename)s:%(lineno)d]') + hdlr = logging.StreamHandler() # stderr + hdlr.setFormatter(fmt) + logging.root.addHandler(hdlr) + + if filename: + hdlr = logging.FileHandler(filename, 'a') + hdlr.setFormatter(fmt) + logging.root.addHandler(hdlr) def memoize(func):