Im trying to use the logging module to print to file. IT works fine, my file has my logs but It also shows those logs to my console window, which i dont want to happen. I tried the following:
logger = logging.getLogger('log')
hdlr = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=200000, backupCount=3)
formatter = logging.Formatter('%(asctime)s -- %(levelname)s -- \n%(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
logger.propagate = False
even tough, it still prints to console. Any ideas why?
Most likely you have a call to logging.basicConfig()
somewhere, or a call to logging.info()
or similar (calling a module-level function, rather than a logger method).
The module-level functions are meant for simple usage, and create and add a console handler if no handlers are assigned to the root logger. So ensure that you have no logging.debug(...)
style calls and that you're not calling basicConfig()
or other logging configuration functions which might be changing your logging configuration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With