I'm trying to tweak some logback functionality (custom Appenders and the like). In order to test it I would like to configure Logback and call its logging methods directly without going through sl4j.
The reason for this weird requirement is to be able to test logback functionality in an environment where also other SLF4J bridges are available.
So I want to do the stuff described when invoking JoranConfigurator directly without a reference to SLF4J.
Note that logback-classic transitively includes the slf4j-api and logback-core , so only having the logback-classic is enough to setup logback with slf4j. When using Logback with SLF4j, a console appender with DEBUG log level is configured automatically. For custom logback configuration, we need to create logback.
To disable this behavior, you must add a logback configuration file called logback. xml in your java classpath root. You can download this minimal logback. xml file and add it in the src/main/resources directory for a maven project or beside fr directory for a simple java project.
Logback and SLF4J can be primarily classified as "Log Management" tools. According to the StackShare community, Logback has a broader approval, being mentioned in 4 company stacks & 9 developers stacks; compared to SLF4J, which is listed in 5 company stacks and 7 developer stacks.
This is the main purpose of SLF4J (Simple Logging Facade for Java) – a logging abstraction which helps to decouple your application from the underlying logger by allowing it to be plugged in – at runtime. Of course, the flexibility that such an abstraction provides is the main reason to use SLF4J.
There is a way to find out.
Here's the example how you can configure LOGBack
// Here we create context
LoggerContext loggerContext = new LoggerContext();
// Initializer is used to enrich context with details
ContextInitializer contextInitializer = new ContextInitializer(loggerContext);
try {
// Get a configuration file from classpath
URL configurationUrl = Thread.currentThread().getContextClassLoader().getResource("custom-logback-configuration.xml");
if (configurationUrl == null) {
throw new IllegalStateException("Unable to find custom logback configuration file");
}
// Ask context initializer to load configuration into context
contextInitializer.configureByResource(configurationUrl);
// Here we get logger from context
logger = loggerContext.getLogger(LogReporter.class);
} catch (JoranException e) {
throw new RuntimeException("Unable to configure logger", e);
}
In general if you want to know how any SLF4J backend works, you may just to look at org.slf4j.impl.StaticLoggerBinder class source from that backend.
Unfortunately, I don't think its possible. If you take a look at the Logback Logger source (or other classes) you will see it depends on slf4j.
This is not nice, imho, as logback should be unaware of slf4j.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With