From 16e55e2e69d858b93e742535025d339155e368bb Mon Sep 17 00:00:00 2001 From: Paul RASCLE Date: Sun, 1 May 2016 23:34:06 +0200 Subject: [PATCH] blocFissure : debug logging implementation --- src/Tools/blocFissure/gmu/initLog.py | 87 ++++++++++++++-------------- 1 file changed, 42 insertions(+), 45 deletions(-) diff --git a/src/Tools/blocFissure/gmu/initLog.py b/src/Tools/blocFissure/gmu/initLog.py index bc427de1d..57b887e60 100644 --- a/src/Tools/blocFissure/gmu/initLog.py +++ b/src/Tools/blocFissure/gmu/initLog.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import logging +import os debug = 10 info = 20 @@ -9,76 +10,72 @@ error = 40 critical = 50 loglevel = warning +logging.basicConfig(format='%(funcName)s[%(lineno)d] %(message)s', + level=logging.WARNING) +ch = None +fh = None -def setDebug(logfile=None): - global loglevel - logging.shutdown() +def setLogger(logfile, level, formatter): + global ch, fh + rootLogger = logging.getLogger('') + if fh is not None: + rootLogger.removeHandler(fh) + fh = None + if ch is not None: + rootLogger.removeHandler(ch) + ch = None if logfile: - logging.basicConfig(filename=logfile, - format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s', - level=logging.DEBUG) + if os.path.exists(logfile): + os.remove(logfile) + fh = logging.FileHandler(logfile) + rootLogger.addHandler(fh) + fh.setFormatter(formatter) else: - logging.basicConfig(format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s', - level=logging.DEBUG) + ch = logging.StreamHandler() + rootLogger.addHandler(ch) + ch.setFormatter(formatter) + rootLogger.setLevel(level) + + +def setDebug(logfile=None): + global loglevel loglevel = debug + level = logging.DEBUG + formatter = logging.Formatter('%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s') + setLogger(logfile, level, formatter) logging.info('start Debug %s', loglevel) def setVerbose(logfile=None): global loglevel - logging.shutdown() - if logfile: - logging.basicConfig(filename=logfile, - format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s', - level=logging.INFO) - else: - logging.basicConfig(format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s', - level=logging.INFO) loglevel = info + level = logging.INFO + formatter = logging.Formatter('%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s') + setLogger(logfile, level, formatter) logging.info('start Verbose %s', loglevel) def setRelease(logfile=None): global loglevel - logging.shutdown() - if logfile: - logging.basicConfig(filename=logfile, - format='%(funcName)s[%(lineno)d] %(message)s', - level=logging.WARNING) - else: - logging.basicConfig(format='%(funcName)s[%(lineno)d] %(message)s', - level=logging.WARNING) loglevel = warning + level = logging.WARNING + formatter = logging.Formatter('%(funcName)s[%(lineno)d] %(message)s') + setLogger(logfile, level, formatter) logging.warning('start Release %s', loglevel) def setUnitTests(logfile=None): global loglevel - logging.shutdown() - if logfile: - logging.basicConfig(filename=logfile, - format='%(funcName)s[%(lineno)d] %(message)s', - level=logging.CRITICAL) - else: - logging.basicConfig(format='%(funcName)s[%(lineno)d] %(message)s', - level=logging.CRITICAL) loglevel = critical + level = logging.CRITICAL + formatter = logging.Formatter('%(funcName)s[%(lineno)d] %(message)s') + setLogger(logfile, level, formatter) logging.critical('start UnitTests %s', loglevel) def setPerfTests(logfile=None): global loglevel - logging.shutdown() - if logfile: - logging.basicConfig(filename=logfile, - format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s', - level=logging.CRITICAL) - else: - logging.basicConfig(format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s', - level=logging.CRITICAL) loglevel = critical + level = logging.CRITICAL + formatter = logging.Formatter('%(funcName)s[%(lineno)d] %(message)s') + setLogger(logfile, level, formatter) logging.info('start PerfTests %s', loglevel) def getLogLevel(): return loglevel - - #logging.basicConfig(filename='myapp.log', - # format='%(asctime)s %(message)s', - # datefmt='%m/%d/%Y %I:%M:%S %p', - # level=logging.DEBUG) -- 2.39.2