Ho creato un TimedRotatingHandler per il server Flask. Ma tutti i log generati dal werkzeug vengono comunque lanciati sulla console.Come reindirizzare i log werkzeug di python in un file di registro quando si utilizza TimedRotatingHandler?
Come reindirizzare questi registri al file di registro (test.log) con la funzionalità di rotazione del registro.
Codice frammento:
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
# add a file handler
fh = logging.handlers.TimedRotatingFileHandler("test.log",when='M',interval=1,backupCount=0)
fh.setLevel(logging.DEBUG)
# create a formatter and set the formatter for the handler.
frmt = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh.setFormatter(frmt)
# add the Handler to the logger
log.addHandler(fh)
app.run(host='0.0.0.0', debug=True)
I registri che seguono sono ancora gettati sulla console.
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with 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 -
Aggiungendo il gestore di file di log al logger werkzeug non è troppo risolvere il problema
logging.getLogger('werkzeug').addHandler(fh);