Federico Ressi b205ce2898 Remove import collectd from logger.py
collectd is now passed from the plugin by using logger
handler constructor

Closes-Bug: #1615352
Change-Id: I353dc42cc24669a38554ea952355e62b1942b219
2016-08-23 16:51:48 +01:00

60 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""Ceilometer collectd plugin implementation"""
import logging
import traceback
from collectd_ceilometer.settings import Config
class CollectdLogHandler(logging.Handler):
"""A handler class for collectd plugin"""
# pylint: disable=no-member
cfg = Config.instance()
def __init__(self, collectd, level=logging.NOTSET):
super(CollectdLogHandler, self).__init__(level=level)
self.priority_map = {
logging.DEBUG: collectd.debug,
logging.INFO: collectd.info,
logging.WARNING: collectd.warning,
logging.ERROR: collectd.error,
logging.CRITICAL: collectd.error
}
def emit(self, record):
try:
self.emit_message(
message=self.format(record),
level=record.levelno)
# pylint: disable=broad-except
except Exception:
self.emit_message(
message="Error emitting message:\n{}".format(
traceback.format_exc()),
level=logging.ERROR)
def emit_message(self, message, level):
if self.cfg.VERBOSE and level == logging.DEBUG:
level = logging.INFO
elif level not in self.priority_map:
level = logging.ERROR
hook = self.priority_map[level]
# collectd limits log size to 1023B, this is workaround
for i in range(0, len(message), 1023):
hook(message[i:i + 1023])