logging - How to redirect python flask werkzeug logs to a log file when using TimedRotatingHandler? -
i've created timedrotatinghandler flask server. logs generated werkzeug still thrown on console.
how can redirect these logs log file(test.log) log rotating functionality.
code snippet:
log = logging.getlogger(__name__) log.setlevel(logging.debug) # add file handler fh = logging.handlers.timedrotatingfilehandler("test.log",when='m',interval=1,backupcount=0) fh.setlevel(logging.debug) # create formatter , set formatter handler. frmt = logging.formatter('%(asctime)s - %(levelname)s - %(message)s') fh.setformatter(frmt) # add handler logger log.addhandler(fh) app.run(host='0.0.0.0', debug=true)
the below logs still thrown on console.
* running on http://0.0.0.0:5000/ (press ctrl+c quit) * restarting stat 192.168.1.6 - - [25/jun/2015 07:11:13] "get / http/1.1" 200 - 192.168.1.6 - - [25/jun/2015 07:11:13] "get /static/js/jquery-1.11.2/jquery-1.11.2.min.js http/1.1" 304 - 192.168.1.6 - - [25/jun/2015 07:11:13] "get /static/js/main/main.js http/1.1" 304 - 192.168.1.6 - - [25/jun/2015 07:11:13] "get /favicon.ico http/1.1" 404 -
adding log file handler werkzeug logger not solving problem
logging.getlogger('werkzeug').addhandler(fh);
it looks werkzeug sends messages root logger rather named logger.
if try should start logging file logging.getlogger().addhandler(fh)
if want stop logging console you'll have remove stream handler root logger. can see handlers root logger using
>>> print logging.getlogger().handlers [<logging.streamhandler object @ 0x.....>, logging.handlers.timedrotatingfilehandler object @ 0x.....>]
Comments
Post a Comment