-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(template-st): log StringTemplate compile/runtime errors via SLF4J #3708
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
base: main
Are you sure you want to change the base?
feat(template-st): log StringTemplate compile/runtime errors via SLF4J #3708
Conversation
Fixes spring-projectsGH-3604 (spring-projects#3604) ### Why StringTemplate’s default ErrorManager writes diagnostics to stderr, which is invisible in most Spring deployments. This change redirects those messages to SLF4J so they reach Logstash / CloudWatch / etc. ### What * Add `Slf4jStErrorListener` (STErrorListener -> SLF4J). * Create STGroup with listener inside `StTemplateRenderer#createST`. * JUnit `malformedTemplateShouldLogErrorViaSlf4j`. * Javadoc updates. ### Impact No API changes; applications automatically get structured error logs. Signed-off-by: Your Name <[email protected]> Signed-off-by: renechoi <[email protected]>
0f55d4d
to
31db4d2
Compare
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.
Please reorganize the imports in the following order:
- java.*
- other imports
- org.springframework.*
- static imports
Also, please avoid using wildcard imports .*
For reference, you can refer to the example in this file:
OpenAiApiIT.java.
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.
Additionally, unnecessary tabs have been added. It would be great if you could remove those as well.
} | ||
catch (Exception ex) { | ||
throw new IllegalArgumentException("The template string is not valid.", ex); | ||
} | ||
} | ||
|
||
|
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.
This line appears to have an unnecessary line break.
Why
StringTemplate v4 defaults to
ErrorManager.DEFAULT_ERROR_LISTENER
, whichwrites compile-time and runtime diagnostics directly to stderr. In most
Spring-based deployments the process stderr stream is either discarded or
treated as an unstructured blob that never reaches Logstash/Grafana, making
template failures virtually invisible at run-time.
Issue #3604 “PromptTemplate should configure error reporting in String
Template” highlighted this gap and requested a way to surface those
messages through the conventional SLF4J pipeline. This commit fulfils that
request and aligns error handling with the rest of the framework logging
strategy.
What
Slf4jStErrorListener
– newSTErrorListener
implementation thatdelegates
compileTimeError
,runTimeError
,IOError
, andinternalError
events to theLogger
ofStTemplateRenderer
.StTemplateRenderer
STGroup
per render operation and registers theSLF4J listener so every template instance receives structured logging.
malformedTemplateShouldLogErrorViaSlf4j
whichListAppender
to capture log events.ERROR
log entry andthat nothing is emitted to stderr.
behaviour-identical.
StTemplateRenderer
to mention the newerror-reporting mechanism.
How
When
createST(String template)
is invoked, the renderer now:STGroup
with the configured delimiters.Slf4jStErrorListener
instance on the group.ST
template from that group.Because the listener is stateless and threads only interact with it via the
SLF4J facade, the change is thread-safe and produces no additional
allocation hot-spots beyond the lifetime of the template rendering call.
Impact
application logs with timestamps, context, and stack traces, making them
searchable and monitorable.
validation semantics remain untouched. Projects depending on
StTemplateRenderer
require no code changes.dependencies.
Migration Notes
No migration steps are required. Applications will automatically benefit
from structured error logging after updating to the version that includes
this commit.
References
String Template”.
logging conventions.
Signed-off-by: [email protected]