2015-08-10 06:34:39 +00:00
|
|
|
"""Django DDP logging helpers."""
|
|
|
|
|
from __future__ import absolute_import, print_function
|
|
|
|
|
|
2015-09-22 01:46:50 +00:00
|
|
|
import datetime
|
2015-08-10 06:34:39 +00:00
|
|
|
import logging
|
2015-09-25 01:32:04 +00:00
|
|
|
import traceback
|
2015-08-10 06:34:39 +00:00
|
|
|
|
|
|
|
|
from dddp import THREAD_LOCAL as this, meteor_random_id, ADDED
|
|
|
|
|
|
|
|
|
|
|
2015-09-25 01:32:04 +00:00
|
|
|
LOGS_NAME = 'dddp.logs'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def stacklines_or_none(exc_info):
|
|
|
|
|
"""Return list of stack text lines or None."""
|
|
|
|
|
if exc_info is None:
|
|
|
|
|
return None
|
|
|
|
|
return traceback.format_exception(*exc_info)
|
|
|
|
|
|
|
|
|
|
|
2015-08-10 06:34:39 +00:00
|
|
|
class DDPHandler(logging.Handler):
|
|
|
|
|
|
|
|
|
|
"""Logging handler that streams log events via DDP to the current client."""
|
|
|
|
|
|
2015-09-25 01:32:04 +00:00
|
|
|
formatter = logging.BASIC_FORMAT
|
|
|
|
|
|
2015-08-10 06:34:39 +00:00
|
|
|
def emit(self, record):
|
|
|
|
|
"""Emit a formatted log record via DDP."""
|
2015-09-25 01:32:04 +00:00
|
|
|
if getattr(this, 'subs', {}).get(LOGS_NAME, False):
|
|
|
|
|
self.format(record)
|
2015-08-10 06:34:39 +00:00
|
|
|
this.send({
|
|
|
|
|
'msg': ADDED,
|
2015-09-25 01:32:04 +00:00
|
|
|
'collection': LOGS_NAME,
|
|
|
|
|
'id': meteor_random_id('/collection/%s' % LOGS_NAME),
|
2015-08-10 06:34:39 +00:00
|
|
|
'fields': {
|
2015-09-25 01:32:04 +00:00
|
|
|
attr: {
|
|
|
|
|
# typecasting methods for specific attributes
|
|
|
|
|
'args': lambda args: [repr(arg) for arg in args],
|
|
|
|
|
'created': datetime.datetime.fromtimestamp,
|
|
|
|
|
'exc_info': stacklines_or_none,
|
|
|
|
|
}.get(
|
|
|
|
|
attr,
|
|
|
|
|
lambda val: val # default typecasting method
|
|
|
|
|
)(getattr(record, attr, None))
|
|
|
|
|
for attr in (
|
|
|
|
|
'args',
|
|
|
|
|
'asctime',
|
|
|
|
|
'created',
|
|
|
|
|
'exc_info',
|
|
|
|
|
'filename',
|
|
|
|
|
'funcName',
|
|
|
|
|
'levelname',
|
|
|
|
|
'levelno',
|
|
|
|
|
'lineno',
|
|
|
|
|
'module',
|
|
|
|
|
'msecs',
|
|
|
|
|
'message',
|
|
|
|
|
'name',
|
|
|
|
|
'pathname',
|
|
|
|
|
'process',
|
|
|
|
|
'processName',
|
|
|
|
|
'relativeCreated',
|
|
|
|
|
'thread',
|
|
|
|
|
'threadName',
|
|
|
|
|
)
|
2015-08-10 06:34:39 +00:00
|
|
|
},
|
|
|
|
|
})
|