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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jcardozo
    New Member
    • Aug 2019
    • 1

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

    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('l og to file only')

    Not sure how to turn off the console.
    Any thoughts?
    Thanks.

    custom_logger(l ogdir, logfile, logname, logconsole)
    logger = logging.getLogg er(logname)

    formatter = logging.Formatt er(fmt='%(ascti me)s [%(name)-2s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')

    rotatingfilehan dler = RotatingFileHan dler(logdir+'/'+logfile,
    maxBytes=MAX_LO GSIZE,
    backupCount=MAX _ROTATE_LOGS)
    rotatingfilehan dler.setFormatt er(formatter)
    logger.addHandl er(rotatingfile handler)

    screen_handler = logging.StreamH andler(stream=s ys.stdout)
    screen_handler. setFormatter(fo rmatter)

    if logconsole:
    logger.addHandl er(screen_handl er)
    else:
    logger.removeHa ndler(screen_ha ndler)

    return logger
Working...