I have the following code in a module that I want to call from any other application module.
The code I have also displays the debug message on the console when I invoke it like this:
def main():
logger = custom_logger('test1', 'log', 'test.log', False)
logger.debug('log to file only')
Not sure how to turn off the console.
Any thoughts?
Thanks.
custom_logger(logdir, logfile, logname, logconsole)
logger = logging.getLogger(logname)
formatter = logging.Formatter(fmt='%(asctime)s [%(name)-2s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
rotatingfilehandler = RotatingFileHandler(logdir+'/'+logfile,
maxBytes=MAX_LOGSIZE,
backupCount=MAX_ROTATE_LOGS)
rotatingfilehandler.setFormatter(formatter)
logger.addHandler(rotatingfilehandler)
screen_handler = logging.StreamHandler(stream=sys.stdout)
screen_handler.setFormatter(formatter)
if logconsole:
logger.addHandler(screen_handler)
else:
logger.removeHandler(screen_handler)
return logger