Jaroslav Safka f903de3f10 Fix plugin logging
Fix logging of debug, info and warning levels.
Also configuration parameter VERBOSE is working again.

fixes problems:
* VERBOSE parameter can be changed at runtime
* Initial log level was unset, then there was nothing logged lower than
exception level. (for example configuration dump was not logged)
* Initial VERBOSE was set to True, then it was too much verbose. And
this flag was mean to be used for debugging problems.

NOTE: real log level is driven by collectd configuration, not by plugin
config.

Change-Id: Ia7048ccb74f27a5d5885b9c0bda17d6fba603e9b
Closes-Bug: #1664973
2017-07-19 09:32:41 +00:00

63 lines
2.2 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
class CollectdLogHandler(logging.Handler):
"""A handler class for collectd plugin"""
# this is the maximum message length supported by collectd
# messages longer than this size have to be split
max_message_length = 1023
def __init__(self, collectd, config, level=logging.NOTSET):
super(CollectdLogHandler, self).__init__(level=level)
self.cfg = config
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):
"Called by loggers when a message has to be sent to collectd."
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 splits entries to smaller chunks
for i in range(0, len(message), self.max_message_length):
hook(message[i:i + self.max_message_length])