-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt logging setup to work when the API server is run under gunicorn.
Summary: Run with: gunicorn --worker-class inbox.api.wsgi.InboxWSGIWorker --logger-class inbox.api.wsgi.GunicornLogger inbox.api.srv:app ... This also requires some minor changes to our existing logging setup, because gunicorn passes positional arguments in its logging messages (`self.log.info('message %s', some_value)`) but structlog can't currently handle that (hynek/structlog#19). Test Plan: Run `bin/inbox-api` and `gunicorn --worker-class inbox.api.wsgi.InboxWSGIWorker --logger-class inbox.api.wsgi.GunicornLogger --workers=1 --bind 127.0.0.1:5555 inbox.api.srv:app` and make some API requests. Reviewers: spang Reviewed By: spang Differential Revision: https://review.inboxapp.com/D235
- Loading branch information
Showing
4 changed files
with
99 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from gevent.pywsgi import WSGIHandler, WSGIServer | ||
from gunicorn.workers.ggevent import GeventWorker | ||
import gunicorn.glogging | ||
import inbox.log | ||
|
||
|
||
class InboxWSGIHandler(WSGIHandler): | ||
"""Custom WSGI handler class to customize request logging. Based on | ||
gunicorn.workers.ggevent.PyWSGIHandler.""" | ||
def log_request(self): | ||
# gevent.pywsgi tries to call log.write(), but Python logger objects | ||
# implement log.debug(), log.info(), etc., so we need to monkey-patch | ||
# log_request(). See | ||
# http://stackoverflow.com/questions/9444405/gunicorn-and-websockets | ||
log = self.server.log | ||
length = self.response_length | ||
if self.time_finish: | ||
request_time = round(self.time_finish - self.time_start, 6) | ||
if isinstance(self.client_address, tuple): | ||
client_address = self.client_address[0] | ||
else: | ||
client_address = self.client_address | ||
status = getattr(self, 'status', None) | ||
requestline = getattr(self, 'requestline', None) | ||
|
||
log.info('request handled', | ||
length=length, | ||
request_time=request_time, | ||
client_address=client_address, | ||
status=status, | ||
requestline=requestline) | ||
|
||
def get_environ(self): | ||
env = super(InboxWSGIHandler, self).get_environ() | ||
env['gunicorn.sock'] = self.socket | ||
env['RAW_URI'] = self.path | ||
return env | ||
|
||
|
||
class InboxWSGIWorker(GeventWorker): | ||
"""Custom worker class for gunicorn. Based on | ||
gunicorn.workers.ggevent.GeventPyWSGIWorker.""" | ||
server_class = WSGIServer | ||
wsgi_handler = InboxWSGIHandler | ||
|
||
|
||
class GunicornLogger(gunicorn.glogging.Logger): | ||
def __init__(self, cfg): | ||
gunicorn.glogging.Logger.__init__(self, cfg) | ||
inbox.log.configure_logging(is_prod=True) | ||
self.error_log = inbox.log.get_logger() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters