By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,656 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

How to disable console display while enabling logging to a file.

P: 1
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
2 Weeks Ago #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.