Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from sprockets/json-logging
Add JSON logging functionality for Tornado
- Loading branch information
Showing
8 changed files
with
287 additions
and
5 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
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 |
---|---|---|
@@ -1,8 +1,21 @@ | ||
Version History | ||
=============== | ||
`1.1.0`_ Jun 18, 2015 | ||
--------------------- | ||
- Added :class:`sprockets.logging.JSONRequestFormatter` | ||
- Added :method:`sprockets.logging.tornado_log_function` | ||
- Added convenience constants and methods as a pass through to Python's logging package: | ||
- :data:`sprockets.logging.DEBUG` to :data:`logging.DEBUG` | ||
- :data:`sprockets.logging.ERROR` to :data:`logging.ERROR` | ||
- :data:`sprockets.logging.INFO` to :data:`logging.INFO` | ||
- :data:`sprockets.logging.WARN` to :data:`logging.WARN` | ||
- :data:`sprockets.logging.WARNING` to :data:`logging.WARNING` | ||
- :method:`sprockets.logging.dictConfig` to :method:`logging.config.dictConfig` | ||
- :method:`sprockets.logging.getLogger` to :method:`logging.getLogger` | ||
|
||
`1.0.0`_ Jun 09, 2015 | ||
--------------------- | ||
- Added :class:`sprockets.logging.ContextFilter` | ||
|
||
.. _1.1.0: https://github.com/sprockets/sprockets.logging/compare/1.0.0...1.1.0 | ||
.. _1.0.0: https://github.com/sprockets/sprockets.logging/compare/0.0.0...1.0.0 |
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,76 @@ | ||
import logging.config | ||
import signal | ||
import uuid | ||
|
||
from tornado import ioloop, web | ||
import sprockets.logging | ||
|
||
|
||
LOG_CONFIG = { | ||
'version': 1, | ||
'handlers': { | ||
'console': { | ||
'class': 'logging.StreamHandler', | ||
'stream': 'ext://sys.stdout', | ||
'formatter': 'simple', | ||
'filters': ['context'] | ||
} | ||
}, | ||
'formatters': { | ||
'simple': { | ||
'()': sprockets.logging.JSONRequestFormatter | ||
} | ||
}, | ||
'filters': { | ||
'context': { | ||
'()': 'sprockets.logging.ContextFilter', | ||
'properties': ['context'] | ||
} | ||
}, | ||
'loggers': { | ||
'tornado': { | ||
'level': 'DEBUG' | ||
} | ||
}, | ||
'root': { | ||
'handlers': ['console'], | ||
'level': 'DEBUG' | ||
}, | ||
'incremental': False | ||
} | ||
|
||
|
||
class RequestHandler(web.RequestHandler): | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.parent_log = kwargs.pop('parent_log') | ||
super(RequestHandler, self).__init__(*args, **kwargs) | ||
|
||
def prepare(self): | ||
uniq_id = self.request.headers.get('X-UniqID', uuid.uuid4().hex) | ||
self.logger = logging.LoggerAdapter( | ||
self.parent_log.getChild('RequestHandler'), | ||
extra={'context': uniq_id}) | ||
|
||
def get(self, object_id): | ||
self.logger.debug('fetchin %s', object_id) | ||
self.set_status(200) | ||
return self.finish() | ||
|
||
def sig_handler(signo, frame): | ||
logging.info('caught signal %d, stopping IO loop', signo) | ||
iol = ioloop.IOLoop.instance() | ||
iol.add_callback_from_signal(iol.stop) | ||
|
||
if __name__ == '__main__': | ||
logging.config.dictConfig(LOG_CONFIG) | ||
logger = logging.getLogger('app') | ||
app = web.Application([ | ||
web.url('/(?P<object_id>\w+)', RequestHandler, | ||
kwargs={'parent_log': logger}), | ||
], log_function=sprockets.logging.tornado_log_function) | ||
app.listen(8000) | ||
signal.signal(signal.SIGINT, sig_handler) | ||
signal.signal(signal.SIGTERM, sig_handler) | ||
ioloop.IOLoop.instance().start() | ||
logger.info('IO loop stopped, exiting') |
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