New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow specifying a custom log function for template render #99572
Conversation
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
70effbf
to
a74ce5e
Compare
homeassistant/helpers/template.py
Outdated
log_warning = _LOGGER.warning | ||
else: | ||
# Mypy does not agree log_fn is not None here | ||
log_error = log_warning = lambda msg, *args: log_fn(msg % args) # type: ignore[misc] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we pass warning
or error
as an level
arg ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, implemented 👍
@@ -2178,45 +2198,62 @@ def _render_with_context( | |||
return template.render(**kwargs) | |||
|
|||
|
|||
class LoggingUndefined(jinja2.Undefined): | |||
def make_logging_undefined( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not add an __init__
to the class ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we're not returning an instance of the class, we're returning the class itself.
* Allow specifying a custom log function for template render * Bypass template cache when reporting errors + fix tests * Send errors as events * Fix logic for creating new TemplateEnvironment * Add strict mode back * Only send error events if report_errors is True * Force test of websocket_api only * Debug test * Run pytest with higher verbosity * Timeout after 1 minute, enable syslog output * Adjust timeout * Add debug logs * Fix unsafe call to WebSocketHandler._send_message * Remove debug code * Improve test coverage * Revert accidental change * Include severity in error events * Remove redundant information from error events
Proposed change
Allow specifying a custom log function for template render and use it for the
render_template
WS commandDrop the
strict
parameter to therender_template
Background:
The
strict
parameter to therender_template
was added to avoid logging errors from the template developer tool. However, thestrict
mode also unintentionally changed the result when rendering templates.This PR captures the log messages and send them over the websocket without affecting the result of rendering templates.
Needs (and currently includes) #99571
Type of change
Additional information
Checklist
black --fast homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: