2020-01-31 16:33:00 +00:00
|
|
|
"""Support for setting the level of logging for components."""
|
2019-12-09 13:22:30 +00:00
|
|
|
import logging
|
2020-05-23 16:12:55 +00:00
|
|
|
import re
|
2015-11-04 20:30:02 +00:00
|
|
|
|
2016-09-18 06:23:45 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "logger"
|
2015-11-04 20:30:02 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_LOGGER = "logger"
|
2017-02-26 22:15:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_SET_DEFAULT_LEVEL = "set_default_level"
|
|
|
|
SERVICE_SET_LEVEL = "set_level"
|
2017-02-26 22:15:44 +00:00
|
|
|
|
2020-07-12 05:33:25 +00:00
|
|
|
HIGHEST_LOG_LEVEL = logging.CRITICAL
|
|
|
|
|
2015-11-04 20:30:02 +00:00
|
|
|
LOGSEVERITY = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"CRITICAL": 50,
|
|
|
|
"FATAL": 50,
|
|
|
|
"ERROR": 40,
|
|
|
|
"WARNING": 30,
|
|
|
|
"WARN": 30,
|
|
|
|
"INFO": 20,
|
|
|
|
"DEBUG": 10,
|
|
|
|
"NOTSET": 0,
|
2015-11-04 20:30:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
LOGGER_DEFAULT = "default"
|
|
|
|
LOGGER_LOGS = "logs"
|
2015-11-04 20:30:02 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_LEVEL = "level"
|
2018-06-07 21:03:04 +00:00
|
|
|
|
2016-09-20 04:12:56 +00:00
|
|
|
_VALID_LOG_LEVEL = vol.All(vol.Upper, vol.In(LOGSEVERITY))
|
2016-09-18 06:23:45 +00:00
|
|
|
|
2018-06-07 21:03:04 +00:00
|
|
|
SERVICE_SET_DEFAULT_LEVEL_SCHEMA = vol.Schema({ATTR_LEVEL: _VALID_LOG_LEVEL})
|
2017-02-26 22:15:44 +00:00
|
|
|
SERVICE_SET_LEVEL_SCHEMA = vol.Schema({cv.string: _VALID_LOG_LEVEL})
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(LOGGER_DEFAULT): _VALID_LOG_LEVEL,
|
|
|
|
vol.Optional(LOGGER_LOGS): vol.Schema({cv.string: _VALID_LOG_LEVEL}),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2016-09-18 06:23:45 +00:00
|
|
|
|
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
|
|
|
|
2020-05-23 16:12:55 +00:00
|
|
|
def __init__(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Initialize the filter."""
|
2015-11-04 21:08:15 +00:00
|
|
|
super().__init__()
|
|
|
|
|
2020-05-23 16:12:55 +00:00
|
|
|
self._default = None
|
2020-07-12 05:33:25 +00:00
|
|
|
self._logs = {}
|
2020-05-23 16:12:55 +00:00
|
|
|
self._log_rx = None
|
|
|
|
|
|
|
|
def update_default_level(self, default_level):
|
|
|
|
"""Update the default logger level."""
|
|
|
|
self._default = default_level
|
|
|
|
|
|
|
|
def update_log_filter(self, logs):
|
|
|
|
"""Rebuild the internal filter from new config."""
|
|
|
|
#
|
|
|
|
# A precompiled regex is used to avoid
|
|
|
|
# the overhead of a list transversal
|
|
|
|
#
|
|
|
|
# Sort to make sure the longer
|
|
|
|
# names are always matched first
|
|
|
|
# so they take precedence of the shorter names
|
|
|
|
# to allow for more granular settings.
|
|
|
|
#
|
|
|
|
names_by_len = sorted(list(logs), key=len, reverse=True)
|
|
|
|
self._log_rx = re.compile("".join(["^(?:", "|".join(names_by_len), ")"]))
|
|
|
|
self._logs = logs
|
2015-11-04 20:30:02 +00:00
|
|
|
|
2020-07-12 05:33:25 +00:00
|
|
|
def set_logger_level(self):
|
|
|
|
"""Find the lowest log level set to allow logger to pre-filter log messages."""
|
|
|
|
#
|
|
|
|
# We set the root logger level to lowest log level
|
|
|
|
# specified in default or for in the log filter so
|
|
|
|
# logger.isEnabledFor function will work as designed
|
|
|
|
# to avoid making logger records that will always be
|
|
|
|
# discarded.
|
|
|
|
#
|
|
|
|
# This can make the logger performance significantly
|
|
|
|
# faster if no integrations are requesting debug logs
|
|
|
|
# because we can avoid the record creation and filtering
|
|
|
|
# overhead.
|
|
|
|
#
|
|
|
|
logger = logging.getLogger("")
|
|
|
|
logger.setLevel(min(HIGHEST_LOG_LEVEL, self._default, *self._logs.values()))
|
|
|
|
|
2015-11-04 20:30:02 +00:00
|
|
|
def filter(self, record):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Filter the log entries."""
|
2015-11-12 17:04:48 +00:00
|
|
|
# Log with filtered severity
|
2020-05-23 16:12:55 +00:00
|
|
|
if self._log_rx:
|
|
|
|
match = self._log_rx.match(record.name)
|
|
|
|
if match:
|
|
|
|
return record.levelno >= self._logs[match.group(0)]
|
2015-11-04 20:30:02 +00:00
|
|
|
|
|
|
|
# Log with default severity
|
2020-05-23 16:12:55 +00:00
|
|
|
return record.levelno >= self._default
|
2015-11-04 20:30:02 +00:00
|
|
|
|
|
|
|
|
2018-04-28 23:26:20 +00:00
|
|
|
async def async_setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the logger component."""
|
2016-09-20 04:12:56 +00:00
|
|
|
logfilter = {}
|
2020-05-23 16:12:55 +00:00
|
|
|
hass_filter = HomeAssistantLogFilter()
|
2015-11-04 20:30:02 +00:00
|
|
|
|
2018-06-07 21:03:04 +00:00
|
|
|
def set_default_log_level(level):
|
|
|
|
"""Set the default log level for components."""
|
|
|
|
logfilter[LOGGER_DEFAULT] = LOGSEVERITY[level]
|
2020-05-23 16:12:55 +00:00
|
|
|
hass_filter.update_default_level(LOGSEVERITY[level])
|
2015-11-04 20:30:02 +00:00
|
|
|
|
2017-02-26 22:15:44 +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
|
|
|
|
2018-01-29 22:37:19 +00:00
|
|
|
# Add new logpoints mapped to correct severity
|
2017-02-26 22:15:44 +00:00
|
|
|
for key, value in logpoints.items():
|
|
|
|
logs[key] = LOGSEVERITY[value]
|
|
|
|
|
2020-05-23 16:12:55 +00:00
|
|
|
logfilter[LOGGER_LOGS] = logs
|
|
|
|
|
|
|
|
hass_filter.update_log_filter(logs)
|
2015-11-06 21:51:33 +00:00
|
|
|
|
2018-06-07 21:03:04 +00:00
|
|
|
# Set default log severity
|
|
|
|
if LOGGER_DEFAULT in config.get(DOMAIN):
|
|
|
|
set_default_log_level(config.get(DOMAIN)[LOGGER_DEFAULT])
|
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
set_default_log_level("DEBUG")
|
2018-06-07 21:03:04 +00:00
|
|
|
|
2015-11-04 20:30:02 +00:00
|
|
|
# Set log filter for all log handler
|
|
|
|
for handler in logging.root.handlers:
|
2015-12-22 19:50:59 +00:00
|
|
|
handler.setLevel(logging.NOTSET)
|
2020-05-23 16:12:55 +00:00
|
|
|
handler.addFilter(hass_filter)
|
2015-11-04 20:30:02 +00:00
|
|
|
|
2017-02-26 22:15:44 +00:00
|
|
|
if LOGGER_LOGS in config.get(DOMAIN):
|
|
|
|
set_log_levels(config.get(DOMAIN)[LOGGER_LOGS])
|
|
|
|
|
2020-07-12 05:33:25 +00:00
|
|
|
hass_filter.set_logger_level()
|
|
|
|
|
2018-04-28 23:26:20 +00:00
|
|
|
async def async_service_handler(service):
|
2017-02-26 22:15:44 +00:00
|
|
|
"""Handle logger services."""
|
2018-06-07 21:03:04 +00:00
|
|
|
if service.service == SERVICE_SET_DEFAULT_LEVEL:
|
|
|
|
set_default_log_level(service.data.get(ATTR_LEVEL))
|
|
|
|
else:
|
|
|
|
set_log_levels(service.data)
|
2020-07-12 05:33:25 +00:00
|
|
|
hass_filter.set_logger_level()
|
2018-06-07 21:03:04 +00:00
|
|
|
|
|
|
|
hass.services.async_register(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_DEFAULT_LEVEL,
|
|
|
|
async_service_handler,
|
|
|
|
schema=SERVICE_SET_DEFAULT_LEVEL_SCHEMA,
|
|
|
|
)
|
2017-02-26 22:15:44 +00:00
|
|
|
|
|
|
|
hass.services.async_register(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_LEVEL,
|
|
|
|
async_service_handler,
|
|
|
|
schema=SERVICE_SET_LEVEL_SCHEMA,
|
|
|
|
)
|
2017-02-26 22:15:44 +00:00
|
|
|
|
2015-11-04 20:30:02 +00:00
|
|
|
return True
|