def setupLogger(console=True, File=False, Variable=False, Filebackupcount=0):
'''
Setup a logger for the application
:return: Nothing
'''
global logger
global log_capture_string
# create logger
logging.raiseExceptions = False
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(message)s')
# Check if log exists and should therefore be rolled
needRoll = os.path.isfile(LOG_FILENAME)
if File:
# create file handler which logs even debug messages and hold a backup of old logs
fh = logging.handlers.RotatingFileHandler( LOG_FILENAME, backupCount=int(Filebackupcount)) # create a backup of the log
fh.setLevel(logging.DEBUG) if args.debug else fh.setLevel(logging.INFO) #TODO:Change this to ERROR after dev
fh.setFormatter(formatter)
logger.addHandler(fh)
if Variable:
# create variable handler for on the fly read
vh = logging.StreamHandler(log_capture_string)
vh.setLevel(logging.DEBUG) if args.debug else vh.setLevel(logging.INFO)
vh.setFormatter(formatter)
logger.addHandler(vh)
if console:
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG) if args.debug else ch.setLevel(logging.ERROR)
ch.setFormatter(formatter)
logger.addHandler(ch)
# This is a stale log, so roll it
if File and needRoll:
# Add timestamp
logger.debug(_('\n---------\nLog closed on {0}.\n---------\n').format(time.asctime()))
# Roll over on application start
logger.handlers[0].doRollover()
# Add timestamp
logger.debug(_('\n---------\nLog started on {0}.\n---------\n').format(time.asctime()))
评论列表
文章目录