core/homeassistant/components/logger.py

129 lines
3.3 KiB
Python
Raw Normal View History

2015-11-04 20:30:02 +00:00
"""
2015-11-12 17:04:48 +00:00
Component that will help set the level of logging for components.
2015-11-04 20:30:02 +00:00
For more details about this component, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/logger/
2015-11-04 20:30:02 +00:00
"""
import asyncio
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
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
2015-11-04 20:30:02 +00:00
DOMAIN = 'logger'
DATA_LOGGER = 'logger'
SERVICE_SET_LEVEL = 'set_level'
2015-11-04 20:30:02 +00:00
LOGSEVERITY = {
'CRITICAL': 50,
'FATAL': 50,
'ERROR': 40,
'WARNING': 30,
'WARN': 30,
'INFO': 20,
'DEBUG': 10,
'NOTSET': 0
}
LOGGER_DEFAULT = 'default'
LOGGER_LOGS = 'logs'
2016-09-20 04:12:56 +00:00
_VALID_LOG_LEVEL = vol.All(vol.Upper, vol.In(LOGSEVERITY))
SERVICE_SET_LEVEL_SCHEMA = vol.Schema({cv.string: _VALID_LOG_LEVEL})
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
2016-09-20 04:12:56 +00:00
vol.Optional(LOGGER_DEFAULT): _VALID_LOG_LEVEL,
vol.Optional(LOGGER_LOGS): vol.Schema({cv.string: _VALID_LOG_LEVEL}),
}),
}, extra=vol.ALLOW_EXTRA)
2015-11-04 20:30:02 +00:00
def set_level(hass, logs):
"""Set log level for components."""
hass.services.call(DOMAIN, SERVICE_SET_LEVEL, logs)
2015-11-04 20:30:02 +00:00
class HomeAssistantLogFilter(logging.Filter):
2016-03-07 17:49:31 +00:00
"""A log filter."""
2016-03-08 16:55:57 +00:00
# pylint: disable=no-init
2015-11-04 20:30:02 +00:00
def __init__(self, logfilter):
2016-03-08 16:55:57 +00:00
"""Initialize the filter."""
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):
"""Filter the log entries."""
2015-11-12 17:04:48 +00:00
# Log with filtered severity
2015-11-04 20:30:02 +00:00
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
@asyncio.coroutine
def async_setup(hass, config):
"""Set up the logger component."""
2016-09-20 04:12:56 +00:00
logfilter = {}
2015-11-04 20:30:02 +00:00
# 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[
2016-09-20 04:12:56 +00:00
config.get(DOMAIN)[LOGGER_DEFAULT]
2015-11-04 21:08:15 +00:00
]
2015-11-04 20:30:02 +00:00
def set_log_levels(logpoints):
"""Set the specified log levels."""
logs = {}
# Preserve existing logs
if LOGGER_LOGS in logfilter:
logs.update(logfilter[LOGGER_LOGS])
2015-11-06 21:51:33 +00:00
# Add new logpoints mapped to correct severity
for key, value in logpoints.items():
logs[key] = LOGSEVERITY[value]
logfilter[LOGGER_LOGS] = OrderedDict(
2015-11-06 21:51:33 +00:00
sorted(
logs.items(),
2015-11-06 21:51:33 +00:00
key=lambda t: len(t[0]),
reverse=True
)
2015-11-04 21:08:15 +00:00
)
2015-11-06 21:51:33 +00:00
logger = logging.getLogger('')
logger.setLevel(logging.NOTSET)
2015-11-04 20:30:02 +00:00
# Set log filter for all log handler
for handler in logging.root.handlers:
handler.setLevel(logging.NOTSET)
2015-11-04 20:30:02 +00:00
handler.addFilter(HomeAssistantLogFilter(logfilter))
if LOGGER_LOGS in config.get(DOMAIN):
set_log_levels(config.get(DOMAIN)[LOGGER_LOGS])
@asyncio.coroutine
def async_service_handler(service):
"""Handle logger services."""
set_log_levels(service.data)
hass.services.async_register(
DOMAIN, SERVICE_SET_LEVEL, async_service_handler,
schema=SERVICE_SET_LEVEL_SCHEMA)
2015-11-04 20:30:02 +00:00
return True