[slf4j-user] Question about throwing exception during initialization

Ceki Gülcü ceki at qos.ch
Wed May 12 14:27:18 CEST 2021


Hi Shawn,

How about checking for the presence of the particular Status message 
during the initialization phase of your application?

  void checkIninitialization() {

    ILoggerFactory lf = org.slf4j.LoggerFactory.getILoggerFactory();
    // if using logback
    LoggerContext loggerContext = (LoggerContext) lf;

    StatusManaget sm = loggerContext.getStatusManager();
    List<Status> statusList = sm.getCopyOfStatusList();

    for(Status s: statusList) {
      if("property x not set".equals(s.getMessage()) {
        throw new IllegalStateException("property x not set");
      }
    }
}

You could call the checkIninitialization() during the initialization of 
your application.

Otherwise, there is no configurable way for SLF4J not to ignore exceptions.

-- 
Ceki Gülcü


On 11/05/2021 13:41, Shawn Song 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
> 
> _______________________________________________
> slf4j-user mailing list
> slf4j-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/slf4j-user
> 


More information about the slf4j-user mailing list