
Having a method that raises NotImplementedError is usually not ideal, since the implementations of the class can't delegate control through super to the parent or to other classes found in the MRO. Instead, we mark a priori ReportingHandler as a base class, leaving the implementation of `publish_event` empty. Change-Id: Id5c442b6998743b1caffbad627847ee5e88f2982
34 lines
864 B
Python
34 lines
864 B
Python
import abc
|
|
import logging
|
|
|
|
import six
|
|
|
|
from cloudinit.registry import DictRegistry
|
|
|
|
|
|
@six.add_metaclass(abc.ABCMeta)
|
|
class ReportingHandler(object):
|
|
"""Base class for report handlers.
|
|
|
|
Implement :meth:`~publish_event` for controlling what
|
|
the handler does with an event.
|
|
"""
|
|
|
|
@abc.abstractmethod
|
|
def publish_event(self, event):
|
|
"""Publish an event to the ``INFO`` log level."""
|
|
|
|
|
|
class LogHandler(ReportingHandler):
|
|
"""Publishes events to the cloud-init log at the ``INFO`` log level."""
|
|
|
|
def publish_event(self, event):
|
|
"""Publish an event to the ``INFO`` log level."""
|
|
logger = logging.getLogger(
|
|
'.'.join(['cloudinit', 'reporting', event.event_type, event.name]))
|
|
logger.info(event.as_string())
|
|
|
|
|
|
available_handlers = DictRegistry()
|
|
available_handlers.register_item('log', LogHandler)
|