logging doesn't work using wsgi, no file is created

Viewed 1799

I'm running this flask application to test logging. It's running fine on my local system, but if I run it on the apache server using mod_wsgi, it doesn't work. There is no errors, but, no foo.log is created.

I'm using python 3.5

The code looks like this:

from flask import Flask
import logging
from logging.handlers import RotatingFileHandler

app = Flask(__name__)

@app.route('/')
def foo():
    app.logger.warning('A warning occurred (%d apples)', 42)
    app.logger.error('An error occurred')
    app.logger.info('Info')
    return "foo"

if __name__ == '__main__':
    handler = RotatingFileHandler('foo.log', maxBytes=10000, backupCount=1)
    handler.setLevel(logging.INFO)
    app.logger.addHandler(handler)
    app.run(host='0.0.0.0')

the wsgi file looks like this:

#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")

from FlaskApp import app as application
application.secret_key = 'Add your secret key'
3 Answers

I have met the same problem. I use Apache's mod_wsgi to run a Django application.

At first, the log file is '/tmp/debug.log', there is no error in wsgi's logfile but file "/tmp/debug.log" is not created.

Then, I changes the file to '/var/log/httpd/debug.log', I found errors like this:

ValueError: Unable to configure handler 'file': [Errno 13] Permission denied: '/var/log/httpd/debug.log'

Last , I create a new directory, and change the owner to apache, like this:

make /var/log/test
chown apache:apache /var/log/test

Now, logfile '/var/log/test/debug.log' is created.

Related