user2508615
user2508615

Reputation: 220

how to wrap log4j logger inside slf4j logger?

I have a log4j.Logger configured like this:

private final Logger logger = Logger.getLogger("loggerName");

and it works just fine. However, for camel purposes I need to get slf4j and trying to feed log4j instead of slf4j causing me a ClassCastException.

I tried this, but it didn't work:

public org.slf4j.Logger getSlf4jLogger() {
        return org.slf4j.LoggerFactory.getLogger("loggerName");
    }

Is there any way to wrap log4j Logger inside of slf4j or use another trick?

Upvotes: 1

Views: 1269

Answers (2)

Paul Vargas
Paul Vargas

Reputation: 42060

There is one option. Remove the log4j's libraries. Use log4j-over-slf4j-1.7.5.jar.

So, in jars view, you can have:

log4j-over-slf4j-1.7.5.jar
            ↓
    slf4j-api-1.7.5.jar
            ↓
logback-classic-1.0.13.jar
  logback-core-1.0.13.jar

Upvotes: 0

Ralf
Ralf

Reputation: 6853

I think there is a misunderstanding about slf4j here: It is a facade API with bindings to several popular logging frameworks, log4j being one of them.

Camel uses slf4j for its own logging so not to impose the use of a particular logging framework on you. If you use log4j then you can keep your code as it is and besides the slf4j-api.jar you need to add the slf4j-log4j12.jar to your classpath. Now your log4j logging configuration also applies to the log output of Camel.

Upvotes: 1

Related Questions