Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Logback directly without going through SLF4J

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.

like image 641
Jens Schauder Avatar asked Mar 29 '12 06:03

Jens Schauder


People also ask

Does Logback require 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.

How do I disable slf4j?

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.

Is Logback the same as slf4j?

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.

Do I need slf4j?

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.


2 Answers

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.

like image 177
Alexander Avatar answered Oct 12 '22 15:10

Alexander


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.

like image 37
igr Avatar answered Oct 12 '22 14:10

igr