[logger] Add runtime level select (#8222)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Clyde Stubbs
2025-02-10 15:53:26 +13:00
committed by GitHub
co-authored by Jesse Hills
parent 0cd3af2fcd
commit ff7d232ee6
9 changed files with 154 additions and 26 deletions
+41 -7
View File
@@ -35,7 +35,7 @@ from esphome.const import (
PLATFORM_RP2040,
PLATFORM_RTL87XX,
)
from esphome.core import CORE, EsphomeError, Lambda, coroutine_with_priority
from esphome.core import CORE, Lambda, coroutine_with_priority
CODEOWNERS = ["@esphome/core"]
logger_ns = cg.esphome_ns.namespace("logger")
@@ -77,6 +77,9 @@ USB_SERIAL_JTAG = "USB_SERIAL_JTAG"
USB_CDC = "USB_CDC"
DEFAULT = "DEFAULT"
CONF_INITIAL_LEVEL = "initial_level"
CONF_LOGGER_ID = "logger_id"
UART_SELECTION_ESP32 = {
VARIANT_ESP32: [UART0, UART1, UART2],
VARIANT_ESP32S2: [UART0, UART1, USB_CDC],
@@ -154,11 +157,11 @@ def uart_selection(value):
def validate_local_no_higher_than_global(value):
global_level = value.get(CONF_LEVEL, "DEBUG")
global_level = LOG_LEVEL_SEVERITY.index(value[CONF_LEVEL])
for tag, level in value.get(CONF_LOGS, {}).items():
if LOG_LEVEL_SEVERITY.index(level) > LOG_LEVEL_SEVERITY.index(global_level):
raise EsphomeError(
f"The local log level {level} for {tag} must be less severe than the global log level {global_level}."
if LOG_LEVEL_SEVERITY.index(level) > global_level:
raise cv.Invalid(
f"The configured log level for {tag} ({level}) must be no more severe than the global log level {value[CONF_LEVEL]}."
)
return value
@@ -209,6 +212,7 @@ CONFIG_SCHEMA = cv.All(
cv.string: is_log_level,
}
),
cv.Optional(CONF_INITIAL_LEVEL): is_log_level,
cv.Optional(CONF_ON_MESSAGE): automation.validate_automation(
{
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(LoggerMessageTrigger),
@@ -227,7 +231,14 @@ CONFIG_SCHEMA = cv.All(
@coroutine_with_priority(90.0)
async def to_code(config):
baud_rate = config[CONF_BAUD_RATE]
log = cg.new_Pvariable(config[CONF_ID], baud_rate, config[CONF_TX_BUFFER_SIZE])
level = config[CONF_LEVEL]
initial_level = LOG_LEVELS[config.get(CONF_INITIAL_LEVEL, level)]
log = cg.new_Pvariable(
config[CONF_ID],
baud_rate,
config[CONF_TX_BUFFER_SIZE],
)
cg.add(log.set_log_level(initial_level))
if CONF_HARDWARE_UART in config:
cg.add(
log.set_uart_selection(
@@ -239,7 +250,6 @@ async def to_code(config):
for tag, level in config[CONF_LOGS].items():
cg.add(log.set_log_level(tag, LOG_LEVELS[level]))
level = config[CONF_LEVEL]
cg.add_define("USE_LOGGER")
this_severity = LOG_LEVEL_SEVERITY.index(level)
cg.add_build_flag(f"-DESPHOME_LOG_LEVEL={LOG_LEVELS[level]}")
@@ -367,3 +377,27 @@ async def logger_log_action_to_code(config, action_id, template_arg, args):
lambda_ = await cg.process_lambda(Lambda(text), args, return_type=cg.void)
return cg.new_Pvariable(action_id, template_arg, lambda_)
@automation.register_action(
"logger.set_level",
LambdaAction,
cv.maybe_simple_value(
{
cv.GenerateID(CONF_LOGGER_ID): cv.use_id(Logger),
cv.Required(CONF_LEVEL): is_log_level,
cv.Optional(CONF_TAG): cv.string,
},
key=CONF_LEVEL,
),
)
async def logger_set_level_to_code(config, action_id, template_arg, args):
level = LOG_LEVELS[config[CONF_LEVEL]]
logger = await cg.get_variable(config[CONF_LOGGER_ID])
if tag := config.get(CONF_TAG):
text = str(cg.statement(logger.set_log_level(tag, level)))
else:
text = str(cg.statement(logger.set_log_level(level)))
lambda_ = await cg.process_lambda(Lambda(text), args, return_type=cg.void)
return cg.new_Pvariable(action_id, template_arg, lambda_)