core/homeassistant/components/logger.py

105 lines
2.7 KiB
Python
Raw Normal View History

2015-11-04 20:30:02 +00:00
"""
homeassistant.components.logger
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Component that will help guide the user taking its first steps.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/logger.html
Sample configuration
2015-11-08 10:29:56 +00:00
# By default log all messages and ignore log event lowest than critical
# for custom omponents
2015-11-04 20:30:02 +00:00
logger:
default: info
2015-11-04 20:30:02 +00:00
logs:
homeassistant.components.device_tracker: critical
homeassistant.components.camera: critical
2015-11-08 10:29:56 +00:00
# By default ignore all messages lowest than critical and log event
# for custom components
logger:
default: critical
logs:
homeassistant.components: info
homeassistant.components.rfxtrx: debug
homeassistant.components.device_tracker: critical
homeassistant.components.camera: critical
2015-11-04 20:30:02 +00:00
"""
import logging
2015-11-06 21:51:33 +00:00
from collections import OrderedDict
2015-11-04 20:30:02 +00:00
DOMAIN = 'logger'
DEPENDENCIES = []
LOGSEVERITY = {
'CRITICAL': 50,
'FATAL': 50,
'ERROR': 40,
'WARNING': 30,
'WARN': 30,
'INFO': 20,
'DEBUG': 10,
'NOTSET': 0
}
LOGGER_DEFAULT = 'default'
LOGGER_LOGS = 'logs'
class HomeAssistantLogFilter(logging.Filter):
2015-11-04 21:08:15 +00:00
"""A Home Assistant log filter"""
# pylint: disable=no-init,too-few-public-methods
2015-11-04 20:30:02 +00:00
def __init__(self, logfilter):
2015-11-04 21:08:15 +00:00
super().__init__()
2015-11-04 20:30:02 +00:00
self.logfilter = logfilter
def filter(self, record):
# Log with filterd severity
if LOGGER_LOGS in self.logfilter:
2015-11-06 21:51:33 +00:00
for filtername in self.logfilter[LOGGER_LOGS]:
logseverity = self.logfilter[LOGGER_LOGS][filtername]
2015-11-04 20:30:02 +00:00
if record.name.startswith(filtername):
return record.levelno >= logseverity
# Log with default severity
default = self.logfilter[LOGGER_DEFAULT]
return record.levelno >= default
def setup(hass, config=None):
""" Setup the logger component. """
logfilter = dict()
# Set default log severity
2015-11-06 21:51:33 +00:00
logfilter[LOGGER_DEFAULT] = LOGSEVERITY['DEBUG']
if LOGGER_DEFAULT in config.get(DOMAIN):
2015-11-04 21:08:15 +00:00
logfilter[LOGGER_DEFAULT] = LOGSEVERITY[
2015-11-06 21:51:33 +00:00
config.get(DOMAIN)[LOGGER_DEFAULT].upper()
2015-11-04 21:08:15 +00:00
]
2015-11-04 20:30:02 +00:00
# Compute logseverity for components
2015-11-06 21:51:33 +00:00
if LOGGER_LOGS in config.get(DOMAIN):
for key, value in config.get(DOMAIN)[LOGGER_LOGS].items():
config.get(DOMAIN)[LOGGER_LOGS][key] = LOGSEVERITY[value.upper()]
logs = OrderedDict(
sorted(
config.get(DOMAIN)[LOGGER_LOGS].items(),
key=lambda t: len(t[0]),
reverse=True
)
2015-11-04 21:08:15 +00:00
)
2015-11-06 21:51:33 +00:00
2015-11-04 20:30:02 +00:00
logfilter[LOGGER_LOGS] = logs
# Set log filter for all log handler
for handler in logging.root.handlers:
handler.addFilter(HomeAssistantLogFilter(logfilter))
return True