I'm looking to do something like this: http://blog.sendhub.com/post/48832423565/hacking-flask-a-success-story which is using a subclass of werkzeug's BaseRequestHandler. You can specify a request_handler in flask's web server directly like so:
app.run(host='0.0.0.0', port=port, request_handler=MyFancyRequestHandler)
But is there a way to specify a request_handler
if I'm running my Flask app through gunicorn
?
Thanks!
Nope. There is no way to do that. Gunicorn is the request handler :-D. You can write your own worker class. What are you trying to accomplish?
I should have been more specific about what I'm trying to do in my question.
I have a user_id stored on Flask's g
object, and for all authenticated requests I'd like to add g.user_id to the request log (which is specified by gunicorn's config)
You can subclass the gunicorn.glogging.Logger
class and override the access
method. Then set your new custom logging class to logger_class
: http://docs.gunicorn.org/en/19.1.1/settings.html#logger-class
@berkerpeksag Thanks, can you give an example of how to pass in a custom logger class? I'm trying but it seems like the path isn't accessible
If you have the following structure:
- app.py # your flask app
- custom_logger.py # your glogging subclass YourLoggerClass
you can pass it to Gunicorn like
$ gunicorn app:app --logger-class custom_logger.YourLoggerClass
You can also take a look my implementation at https://github.com/berkerpeksag/django/blob/21978-gunicorn/django/core/servers/gserver.py
Hmmm still can't get it to work
Error: class uri 'custom_logger.CustomLogger' invalid or not found:
[Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 139, in load_class
mod = import_module('.'.join(components))
File "/usr/lib/python2.7/importlib/init.py", line 37, in import_module
import(name)
ImportError: No module named custom_logger
]
Oh, it may be a path issue. Try with PYTHONPATH
:
$ PYTHONPATH=. gunicorn app:app -b 127.0.0.1:7777 --access-logfile - --error-logfile - --logger-class custom_logger.MyLogger
[2014-11-15 08:03:57 +0000] [23929] [INFO] Starting gunicorn 19.1.1
[2014-11-15 08:03:57 +0000] [23929] [INFO] Listening at: http://127.0.0.1:7777 (23929)
[2014-11-15 08:03:57 +0000] [23929] [INFO] Using worker: sync
[2014-11-15 08:03:57 +0000] [23934] [INFO] Booting worker with pid: 23934
127.0.0.1 - - [15/Nov/2014:08:04:08 +0200] "GET / HTTP/1.1" 200 111 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36"
127.0.0.1 - - [15/Nov/2014:08:04:08 +0200] "GET /static/style.css HTTP/1.1" 304 - "http://127.0.0.1:7777/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36"
grrr this is so frustrating. Now I get this error:
PYTHONPATH=. gunicorn app:app --logger-class custom_logger.CustomLogger -c /code/gunicorn.py
Traceback (most recent call last):
File "/usr/local/bin/gunicorn", line 7, in <module>
from gunicorn.app.wsgiapp import run
ImportError: No module named app.wsgiapp
@gregeinfrank that looks unrelated to this issue. It looks like you've got gunicorn installed globally. Are you also using a virtual environment, or no? I'm just wondering if it could be a gunicorn version mismatch.
Since you've installed Gunicorn globally, you probably need to append the current directory to PYTHONPATH
. Please try something like PYTHONPATH=.:$PYTHONPATH
.
That didn't work either because I don't actually have PYTHONPATH set it looks like. I'm running inside of a docker container. Here's all the relevant info:
root@d1e2de0046d0:/code# which gunicorn
/usr/local/bin/gunicorn
root@d1e2de0046d0:/code# which python
/usr/bin/python
root@d1e2de0046d0:/code# echo $PYTHONPATH
root@d1e2de0046d0:/code#
@gregeinfrank not sur eit's possible but can't you install your logger as a python application using pip? Doing it would put it automatically in your PYTHONPATH.
If not you can set an environment variable in your Dockerfile imo.
I think we can close this now, it's not directly related to Gunicorn.
Since you've installed Gunicorn globally, you probably need to append the current directory to
PYTHONPATH
. Please try something likePYTHONPATH=.:$PYTHONPATH
.
This worked for me!