ThreadLocal guard is utilized in doAppend method of Logback base appenders to prevent calling back on itself.
Under what circumstances would doAppend call back on itself?
Under what circumstances would doAppend call back on itself?
Anytime the Appender::append creates logging events that feedback into the appender itself. This is any Appender makes method calls against a 3rd party library which in itself uses a logging framework that uses slf4j or can be bridged to logback/slf4j.
Take the SMTPAppender as an example. Per the docs:
The SMTPAppender relies on the JavaMail API.
If the following statements are true then you have a feedback loop:
When all of this is true, your application log message that you wanted to send via email ended up creating X number of log message debug statements from JavaMail. Those X number of message now end up calling SMTPAppender::append and so on.
This same type of example could be applied to the DBAppender which requires a JDBC driver that could be directly logging to slf4j. Same rules for any custom appenders applies here too.
In general, you don't what your application logs to be mixed with this appender generated log information or create some sort of feedback loop that takes down your server.