mirror of
https://github.com/sprockets/sprockets.logging.git
synced 2024-11-25 03:00:23 +00:00
Merge pull request #5 from sprockets/improved-logging-traceback
Improved logging functionality
This commit is contained in:
commit
02b0b4694e
3 changed files with 50 additions and 25 deletions
|
@ -1,5 +1,10 @@
|
||||||
Version History
|
Version History
|
||||||
===============
|
===============
|
||||||
|
`1.2.0`_ Jun 23, 2015
|
||||||
|
---------------------
|
||||||
|
- Monkeypatch logging.currentframe
|
||||||
|
- Include a logging message if it's there
|
||||||
|
|
||||||
`1.1.0`_ Jun 18, 2015
|
`1.1.0`_ Jun 18, 2015
|
||||||
---------------------
|
---------------------
|
||||||
- Added :class:`sprockets.logging.JSONRequestFormatter`
|
- Added :class:`sprockets.logging.JSONRequestFormatter`
|
||||||
|
|
|
@ -13,13 +13,14 @@ from __future__ import absolute_import
|
||||||
from logging import config
|
from logging import config
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from tornado import log
|
from tornado import log
|
||||||
except ImportError:
|
except ImportError:
|
||||||
log = None
|
log = None
|
||||||
|
|
||||||
version_info = (1, 1, 0)
|
version_info = (1, 2, 0)
|
||||||
__version__ = '.'.join(str(v) for v in version_info)
|
__version__ = '.'.join(str(v) for v in version_info)
|
||||||
|
|
||||||
# Shortcut methods and constants to avoid needing to import logging directly
|
# Shortcut methods and constants to avoid needing to import logging directly
|
||||||
|
@ -71,15 +72,22 @@ class JSONRequestFormatter(logging.Formatter):
|
||||||
:rtype: str
|
:rtype: str
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return json.dumps({'name': record.name,
|
output = {'name': record.name,
|
||||||
'module': record.module,
|
'module': record.module,
|
||||||
'level': logging.getLevelName(record.levelno),
|
'message': record.msg % record.args,
|
||||||
'line_number': record.lineno,
|
'level': logging.getLevelName(record.levelno),
|
||||||
'process': record.processName,
|
'line_number': record.lineno,
|
||||||
'timestamp': self.formatTime(record),
|
'process': record.processName,
|
||||||
'thread': record.threadName,
|
'timestamp': self.formatTime(record),
|
||||||
'file': record.filename,
|
'thread': record.threadName,
|
||||||
'request': record.args}, sort_keys=True)
|
'file': record.filename,
|
||||||
|
'request': record.args}
|
||||||
|
for key, value in list(output.items()):
|
||||||
|
if not value:
|
||||||
|
del output[key]
|
||||||
|
if 'message' in output:
|
||||||
|
del output['request']
|
||||||
|
return json.dumps(output)
|
||||||
|
|
||||||
|
|
||||||
def tornado_log_function(handler):
|
def tornado_log_function(handler):
|
||||||
|
@ -112,3 +120,24 @@ def tornado_log_function(handler):
|
||||||
'query_args': handler.request.query_arguments,
|
'query_args': handler.request.query_arguments,
|
||||||
'remote_ip': handler.request.remote_ip,
|
'remote_ip': handler.request.remote_ip,
|
||||||
'status_code': status_code})
|
'status_code': status_code})
|
||||||
|
|
||||||
|
|
||||||
|
def currentframe():
|
||||||
|
"""Return the frame object for the caller's stack frame."""
|
||||||
|
try:
|
||||||
|
raise Exception
|
||||||
|
except:
|
||||||
|
traceback = sys.exc_info()[2]
|
||||||
|
frame = traceback.tb_frame
|
||||||
|
while True:
|
||||||
|
if hasattr(frame, 'f_code'):
|
||||||
|
filename = frame.f_code.co_filename
|
||||||
|
if filename.endswith('logging.py') or \
|
||||||
|
filename.endswith('logging/__init__.py'):
|
||||||
|
frame = frame.f_back
|
||||||
|
continue
|
||||||
|
return frame
|
||||||
|
return traceback.tb_frame.f_back
|
||||||
|
|
||||||
|
# Monkey-patch currentframe
|
||||||
|
logging.currentframe = currentframe
|
||||||
|
|
21
tests.py
21
tests.py
|
@ -1,12 +1,11 @@
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
from os import path
|
|
||||||
import random
|
import random
|
||||||
import threading
|
|
||||||
import unittest
|
import unittest
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
|
|
||||||
import sprockets.logging
|
import sprockets.logging
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__name__)
|
LOGGER = logging.getLogger(__name__)
|
||||||
|
@ -130,17 +129,9 @@ class JSONRequestHandlerTestCase(unittest.TestCase):
|
||||||
'status_code': handler.status_code}
|
'status_code': handler.status_code}
|
||||||
|
|
||||||
LOGGER.info('', args)
|
LOGGER.info('', args)
|
||||||
record = logging_handler.records.pop(0)
|
|
||||||
result = logging_handler.results.pop(0)
|
result = logging_handler.results.pop(0)
|
||||||
expectation = \
|
keys = ['line_number', 'file', 'level', 'module', 'name',
|
||||||
{'line_number': 132,
|
'process', 'thread', 'timestamp', 'request']
|
||||||
'file': path.basename(globals()['__file__']),
|
value = json.loads(result)
|
||||||
'level': 'INFO',
|
for key in keys:
|
||||||
'module': globals()['__name__'],
|
self.assertIn(key, value)
|
||||||
'name': globals()['__name__'],
|
|
||||||
'process': 'MainProcess',
|
|
||||||
'thread': threading.current_thread().name,
|
|
||||||
'timestamp':
|
|
||||||
logging_handler.formatter.formatTime(record),
|
|
||||||
'request': args}
|
|
||||||
self.assertEqual(result, json.dumps(expectation, sort_keys=True))
|
|
||||||
|
|
Loading…
Reference in a new issue