python - Flask logging - Cannot get it to write to a file -


ok, here's code setup everything:

if __name__ == '__main__':     app.debug = false      applogger = app.logger      file_handler = filehandler("error.log")     file_handler.setlevel(logging.debug)      applogger.setlevel(logging.debug)     applogger.addhandler(file_handler)      app.run(host='0.0.0.0') 

what happens is

  1. error.log gets created
  2. nothing ever written it
  3. despite not adding streamhandler , setting debug false still stdout (this might correct, still seems weird)

am totally off here somewhere or happening?

why not this:

if __name__ == '__main__':     init_db()  # or whatever need      import logging     logging.basicconfig(filename='error.log',level=logging.debug)      app.run(host="0.0.0.0") 

if start application, you'll see error.log contains:

info:werkzeug: * running on http://0.0.0.0:5000/ 

for more info, visit http://docs.python.org/2/howto/logging.html

okay, insist cannot have 2 handler method showed you, i'll add example makes quite clear. first, add logging code main:

import logging, logging.config, yaml logging.config.dictconfig(yaml.load(open('logging.conf'))) 

now add debug code, see our setup works:

logfile    = logging.getlogger('file') logconsole = logging.getlogger('console') logfile.debug("debug file") logconsole.debug("debug console") 

all left "logging.conf" program. let's use that:

version: 1 formatters:   hiformat:     format: 'hi %(asctime)s - %(name)s - %(levelname)s - %(message)s'   simple:     format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s' handlers:   console:     class: logging.streamhandler     level: debug     formatter: hiformat     stream: ext://sys.stdout   file:     class: logging.filehandler     level: debug     formatter: simple     filename: errors.log loggers:   console:     level: debug     handlers: [console]     propagate: no   file:     level: debug     handlers: [file]     propagate: no root:   level: debug   handlers: [console,file] 

this config more complicated needed, shows features of logging module.

now, when run our application, see output (werkzeug- , console-logger):

hi 2013-07-22 16:36:13,475 - console - debug - debug console hi 2013-07-22 16:36:13,477 - werkzeug - info -  * running on http://0.0.0.0:5000/ 

also note custom formatter "hi" used.

now @ "errors.log" file. contains:

2013-07-22 16:36:13,475 - file - debug - debug file 2013-07-22 16:36:13,477 - werkzeug - info -  * running on http://0.0.0.0:5000/ 

Comments