[slf4j-user] Question about throwing exception during initialization

Shawn Song mr.song.shawn at gmail.com
Mon May 17 01:42:21 CEST 2021


Hello,

I'm using slf4j with logback implementation. We have some critical system
properties that are used to configure logback and I want developers to
explicitly set them before they call `LoggerFactory.getLogger()`. I want to
throw an exception if the properties are not set. Below is my logback
setup:

The `logback.xml` only has one line of configuration which is a
ContextListener: `<contextListener
class="com.company.server.logging.LogbackConfigurator" />`. The context
listener extends `ContextAwareBase` and implements `LoggerContextListener`
and `LifeCycle`. I programmatically configure logback layouts based on the
system properties that I mentioned in the previous emails and if the
properties are not set, I want to fail the application from starting. I
tried to add a `StatusListener` which throws a runtime exception when
`addStatusEvent` adds an error status. However, the exception is caught by
SLF4J in (in `org.slf4j.impl.StaticLoggerBinder#init`) and is swallowed.
Does anyone have suggestions on how to mitigate this issue?

Thanks
Shawn

On Tue, May 11, 2021 at 9:41 PM Shawn Song <mr.song.shawn at gmail.com> wrote:

> Hi,
>
> I'm using slf4j with logback implementation. We have some critical system
> properties that are used to configure logback and I want developers to
> explicitly set them before they call `LoggerFactory.getLogger()`. I want to
> throw an exception if the properties are not set. I have tried to throw the
> exception from logback `StatusListener` but SLF4J catches all exceptions
> during its initialization (in org.slf4j.impl.StaticLoggerBinder#init). I
> know I can check the values of those properties at the start of the
> application but because they are purely used to configure logback, so I
> want to keep it inside the configuration code. Is there any way to stop
> SLF4J from catching the exceptions?
>
> Thanks
> Shawn
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/slf4j-user/attachments/20210517/0b46f57b/attachment.html>


More information about the slf4j-user mailing list