Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StormHandler for logging #58

Merged
merged 13 commits into from
Oct 31, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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