# -*- coding: utf-8 -*-
import logging
+import os
debug = 10
info = 20
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)