Skip to content

Commit

Permalink
Merge pull request #58 from dan-blanchard/add_storm_handler
Browse files Browse the repository at this point in the history
Add StormHandler for logging
  • Loading branch information
msukmanowsky committed Oct 31, 2014
2 parents 3ce614e + a53afd5 commit 7671efa
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions streamparse/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Base primititve classes for working with Storm."""
from __future__ import absolute_import, print_function, unicode_literals

from logging import Handler
from traceback import format_exc

from .ipc import send_message
Expand All @@ -16,10 +17,35 @@
'debug': _STORM_LOG_DEBUG,
'info': _STORM_LOG_INFO,
'warn': _STORM_LOG_WARN,
'warning': _STORM_LOG_WARN,
'error': _STORM_LOG_ERROR,
}


class StormHandler(Handler):
"""Handler that will send messages back to Storm."""

def __init__(self):
""" Initialize handler """
Handler.__init__(self)

def emit(self, record):
"""
Emit a record.
If a formatter is specified, it is used to format the record.
If exception information is present, it is formatted using
traceback.print_exception and sent to Storm.
"""
try:
msg = self.format(record)
level = _STORM_LOG_LEVELS.get(record.levelname.lower(),
_STORM_LOG_INFO)
send_message({'command': 'log', 'msg': str(msg), 'level': level})
except Exception:
self.handleError(record)


class Component(object):
"""Base class for Spouts and Bolts which contains class methods for
logging messages back to the Storm worker process."""
Expand Down

0 comments on commit 7671efa

Please sign in to comment.