How to programmatically configure slf4j logger using SLF4JBridgeHandler

I am trying to configure slf4j to intercept all logging statements, and then programmatically add handlers based on certain conditions. My code is:

private void init() { SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); if(condition1) appendHandler(console, Level.DEBUG); if(condition2) appendHandler(logfile1, Level.INFO); ... } 

How to write code for the appendHandler method? I just spent a few hours trying to read the documentation and cannot find a solution. There are many links on how to do this in configuration files, but not in code.

Also, will I fix that this code intercepts all logging instructions for all the different logging frameworks?

+7
java logging slf4j
source share
1 answer

Also, will I fix that this code intercepts all logging instructions for all the different logging frameworks?

SLF4JBridgeHandler is a java.util.logging (JUL) logging bridge that will "intercept" JUL log entries and redirect them to SLF4J.

Other bridges are available: jcl-over-slf4j (Jakarta Commons Logging => SL4J) and log4j-over-slf4j (Log4J => SL4J) (as well as their analogues SLF4J => X).

Depending on which logging framework is used in your code (directly or indirectly), you can include some or all of them to record all logging statements, as described here .

Outdated SL4J Bridges

How to write code for the appendHandler method?

Using SLF4J (e.g. Logback) as the Main Logging Framework

Once you set up your bridges, you can set up your SLF4J implementation in the usual way.

The following is an example of how to do this for Logback :

 //Install the JUL Bridge SLF4JBridgeHandler.removeHandlersForRootLogger(); SLF4JBridgeHandler.install(); //Obtain an instance of LoggerContext LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory(); //Create a new FileAppender FileAppender<ILoggingEvent> file = new FileAppender<ILoggingEvent>(); file.setName("FileLogger"); file.setFile("error.log"); file.setContext(context); file.setAppend(true); //Filter out anything < WARN ThresholdFilter warningFilter = new ThresholdFilter(); warningFilter.setLevel("WARN"); warningFilter.setContext(context); warningFilter.start(); file.addFilter(warningFilter); //Message Encoder PatternLayoutEncoder ple = new PatternLayoutEncoder(); ple.setContext(context); ple.setPattern("%date %level [%thread] %logger{10} %msg%n"); ple.start(); file.setEncoder(ple); file.start(); //Get ROOT logger, and add appender to it Logger root = context.getLogger(Logger.ROOT_LOGGER_NAME); root.setLevel(Level.DEBUG); root.addAppender(file); 

Using JUL as the Main Logging Framework

If you want to use JUL (java.util.logging) as the main logging structure, you do not need to register SLF4JBridgeHandler at all.

Just configure the JUL handlers as usual, and add the slf4j-jdk14 (i.e. SLF4J => JUL) to the dependency list.

 //Create a new Handler Handler fh = new FileHandler("error.log"); fh.setLevel(Level.WARNING); //Register it with the ROOT logger Logger.getLogger("").addHandler(fh); //Log some messages Logger.getLogger("scratchpad").info("Info !"); Logger.getLogger("scratchpad").warning("Warning !"); Logger.getLogger("scratchpad").severe("Severe !"); //Log some messages via SL4J LoggerFactory.getLogger("scratchpad").error("sl4j message"); 

Thus, any SL4J log statements will be redirected to the corresponding JUL handlers (you can also add jcl-over-slf4j and jcl-over-slf4j log4j-over-sl4j to the mix).

+6
source share

All Articles