Unable to make logback work with karaf 3.0.4

2019-09-20 12:52发布

I am attempting to make karaf 3.0.4 use logback. In the code i attempt to programatically create a FileAppender for a logback logger.

ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("bingo");

Here the LoggerFactor is SLF4J LoggerFactory. I have not posted the code which adds the file appender.

This throws the following exception in karaf:

Caused by: java.lang.ClassCastException: org.ops4j.pax.logging.slf4j.Slf4jLogger cannot be cast to ch.qos.logback.classic.Logger
at bingo.concurrent.TaskContextImpl.<init>(TaskContextImpl.java:34)
at bingo.concurrent.TaskExecutorImpl.execute(TaskExecutorImpl.java:83)

I have gone through the already reported similar questions: How to make Pax logging to use the loggback config

I have already done the following:

  • Made pax-logging-logback as part of the startup.properties so it always gets installed on karaf startup. The version that i have used is 1.8.3.
  • Changed configuration file org.ops4j.pax.logging.cfg org.ops4j.pax.logging.logback.config.file=${karaf.home}/etc/logback.xml

This did not help. There are no logs that gets captured in the karaf.log. I also tried manually installing pax-logging-logback but that did not help either and i has the same result.

Am i missing something?

Regards, Madhav

1条回答
我只想做你的唯一
2楼-- · 2019-09-20 13:55

I have managed to resolve the issue. Unfortunately karaf comes bundled with pax logging service which wraps its own implementation for Slf4j logger and always delegates to that implementation. If one wishes to use logback as the default logging implementation not only for karaf logs but for defining custom logback appenders in the application code that runs on karaf then we need to do the following:

There are 2 ways to make it work - use osgi boot delegation or explicitly add packages to the org.osgi.framework.system.packages.extra in etc/config.properties. We went with the latter as karaf was simply a local runtime where we wished to quickly test stuff and we did not want to change the boot delegation as that would have required changing the way we import/export packages in the individual bundles which would have affected the productive runtime which was not karaf.

  • Following bundles needs to be removed: pax-logging-service, lib/bin/karaf-client.jar
  • To the lib folder add the following bundles (select versions which are supported by your karaf runtime) slf4j-api, logback-core, logback-classic, jul-to-slfj, jcl-over-slf4j, osgi-over-slf4j
  • In etc/startup.properties - remove references to pax-logging-service and pax-logging-api
  • Add required packages in etc/config.properties to org.osgi.framework.system.packages.extra, make sure that there are no extra spaces as karaf complains
  • Create a logback.xml and add it to either the $karaf.home or $karaf.home/etc
  • Comment out everything in etc/org.ops4j.pax.logging.cfg
  • In etc/system.properties add logback.configurationFile="path to logback.xml file"

That is all!! We were not able to make org.apache.karaf.log.core and org.apache.karaf.log.command work and since we did not use any karaf logging console commands we left it as is.

Cheers!! Madhav

查看更多
登录 后发表回答