Exemplo n.º 1
0
class LoggrClient(object):
    """
        Loggr client. Provides simple log methods to send log messages to the
        Loggr service which stores all messages into MongoDB.
    """

    def __init__(self, daemon_name, loggr_broker, service_name, verbose, host=None):
        """
            Init & connect
        """
        super(LoggrClient, self).__init__()
        self.loggr_broker = loggr_broker
        self.service = service_name
        self.verbose = verbose
        self.daemon_name = daemon_name

        if host is None:
            host = socket.gethostname()

        self.host = host
        self.connect()

    def connect(self):
        """
            Create the client connection the the Majordomo broker.
        """
        self.client = MajorDomoClient(self.loggr_broker, verbose=self.verbose)

    def _zlog(self, level, message):
        """
            Actual log sender! Sends the message through zmq to Loggr.
        """
        log_message = LogMessage(
            log_level=level,
            daemon_name=self.daemon_name,
            host_name=self.host,
            log_line=message
        )

        try:
            if self.verbose:
                logging.info(log_message.as_dict())

            self.client.send(service=self.service, request=log_message.as_json())
            self.client.recv()
        except AttributeError, e:
            logging.error("Received illegal message: {}".format(message))
Exemplo n.º 2
0
 def connect(self):
     """
         Create the client connection the the Majordomo broker.
     """
     self.client = MajorDomoClient(self.loggr_broker, verbose=self.verbose)