Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Logging module prints to console even when propagate=False

Tags:

python

logging

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?

like image 900
buddy123 Avatar asked Aug 17 '13 07:08

buddy123


1 Answers

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.

like image 113
Vinay Sajip Avatar answered Oct 05 '22 19:10

Vinay Sajip