mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Add better handling of deprecated configs (#20565)
* Add better handling of deprecated configs * Embed the call to has_at_most_one_key in deprecated * Add tests for checking the deprecated logs * Add thoroughly documented tests * Always check has_at_most_one_key * Fix typing * Move logging helpers to homea new logging helper * Lint * Rename to KeywordMessage instead of BraceMessage * Remove unneeded KeywordStyleAdapter * Lint * Use dict directly rather than dict.keys() when creating set * Patch the version in unit tests, update logging and use parse_version * Re-add KeywordStyleAdapter and fix tests * Lint * Lint
This commit is contained in:
committed by
Martin Hjelmare
parent
ee3631e93e
commit
d5fad33599
49
homeassistant/helpers/logging.py
Normal file
49
homeassistant/helpers/logging.py
Normal file
@@ -0,0 +1,49 @@
|
||||
"""Helpers for logging allowing more advanced logging styles to be used."""
|
||||
import inspect
|
||||
import logging
|
||||
|
||||
|
||||
class KeywordMessage:
|
||||
"""
|
||||
Represents a logging message with keyword arguments.
|
||||
|
||||
Adapted from: https://stackoverflow.com/a/24683360/2267718
|
||||
"""
|
||||
|
||||
def __init__(self, fmt, args, kwargs):
|
||||
"""Initialize a new BraceMessage object."""
|
||||
self._fmt = fmt
|
||||
self._args = args
|
||||
self._kwargs = kwargs
|
||||
|
||||
def __str__(self):
|
||||
"""Convert the object to a string for logging."""
|
||||
return str(self._fmt).format(*self._args, **self._kwargs)
|
||||
|
||||
|
||||
class KeywordStyleAdapter(logging.LoggerAdapter):
|
||||
"""Represents an adapter wrapping the logger allowing KeywordMessages."""
|
||||
|
||||
def __init__(self, logger, extra=None):
|
||||
"""Initialize a new StyleAdapter for the provided logger."""
|
||||
super(KeywordStyleAdapter, self).__init__(logger, extra or {})
|
||||
|
||||
def log(self, level, msg, *args, **kwargs):
|
||||
"""Log the message provided at the appropriate level."""
|
||||
if self.isEnabledFor(level):
|
||||
msg, log_kwargs = self.process(msg, kwargs)
|
||||
self.logger._log( # pylint: disable=protected-access
|
||||
level, KeywordMessage(msg, args, kwargs), (), **log_kwargs
|
||||
)
|
||||
|
||||
def process(self, msg, kwargs):
|
||||
"""Process the keyward args in preparation for logging."""
|
||||
return (
|
||||
msg,
|
||||
{
|
||||
k: kwargs[k]
|
||||
for k in inspect.getfullargspec(
|
||||
self.logger._log # pylint: disable=protected-access
|
||||
).args[1:] if k in kwargs
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user