Forums

Flask Logging

Hi everybody. I'm trying to get logging module in webapp in Flask to work but to no result. What am I doing wrong? The file just don't get created. When I try this in Python console it works. See code attached.

@app.before_first_request
def logging_init():
logging.basicConfig(
        datefmt = '%Y-%m-%d %H:%M:%S',
        format = '%(asctime)s%%%(message)s',
        filename = '/home/vmax/mysite/routing.log',
        level=logging.CRITICAL)

@app.route('/')
def hello_world():
    logging.critical('a request!')
    return 'Hello from Flask!'

Try a couple of debug prints? One in the logging init function, and one in the view?

print('debug info', file=sys.stderr)

Try a couple of debug prints? One in the logging init function, and one in the view?

I tried some: they, as expected, end up in /var/log/…error.log (in some time after request)

But how do I write the log to other file? Or should I use 'open() and write()' with custom file and don't use the logging subsystem?

Thanks!

ugh. the logging module in python is a bit of a nightmare, all overcomplicated and enterprise-ey. But I thought flask had its own logging setup?

http://flask.pocoo.org/docs/0.10/errorhandling/

Give it a go, but if you're still finding it's being difficult, you might just write your own little 3-line function to do logging, using with open...