Sriram
Sriram

Reputation: 101

DropWizard without using LogBack and support for my log4j.properties

kafka source Connector uses Dropwizard for metrics registration in my code. I enabled custom port to see in my browser. Initially without dropwizard in connector, I used log4j.properties for logging( slf4j). When i used dropwizard in connector automatically it switches to logback and showing the folowing results:

SLF4J: Found binding in [jar:file:jarfilename/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/kafka_2.12-0.10.2.1/libs/slf4j-log4j12-1.7.21.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
Apr 13, 2018 8:58:47 AM org.glassfish.jersey.internal.Errors logErrors
WARNING: The following warnings have been detected: WARNING: The (sub)resource method createConnector in org.apache.kafka.connect.runtime.rest.resources.ConnectorsResource contains empty path annotation.
WARNING: The (sub)resource method listConnectors in org.apache.kafka.connect.runtime.rest.resources.ConnectorsResource contains empty path annotation.
WARNING: The (sub)resource method listConnectorPlugins in org.apache.kafka.connect.runtime.rest.resources.ConnectorPluginsResource contains empty path annotation.
WARNING: The (sub)resource method serverInfo in org.apache.kafka.connect.runtime.rest.resources.RootResource contains empty path annotation.

conf.yml file for dropwizard to run server

server:
  adminConnectors:
  - type: http
    port: 8989

code snippet

public class KafkaMetricsPort extends Application<MetricsConfiguration>{
@Override
    public void run(MetricsConfiguration configuration, Environment environment) throws Exception {
    }
}
import io.dropwizard.Configuration;
public class MetricsConfiguration extends Configuration{
}

When i run the code automatically goes into debug mode and not using my custom log4j.properties. Some of dependecies added in my pom.xml

<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>0.9.2</version>
<dependency>
<groupId>io.dropwizard.metrics</groupId>
 <artifactId>metrics-core</artifactId>
 <version>3.1.2</version>

When i excluded dependecnies of all dropwizard logback logging levels from io.dropwizard-core then the code raising an exception:

 Caused by: java.lang.ClassNotFoundException: 
        ch.qos.logback.classic.filter.ThresholdFilter
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

below links which they are suggedted are tired in my code https://github.com/dropwizard/dropwizard/pull/2112 https://github.com/dropwizard/dropwizard/pull/1900 Can't exclude logback-classic dependency from dropwizard project Point: I copied myconnector jar to kafka libs(Because kafka needs to identify myconnecotr jar) so that If i run any other kafka dependecy programming jars then programm goes into debug mode and showing above warnings Multiple slf4j bindings and binding to ch.qos.logback.

Upvotes: 1

Views: 1523

Answers (1)

RVS
RVS

Reputation: 145

I was trying to solve basically the same problem in a slightly different context and ran across key info here: https://www.dropwizard.io/1.3.5/docs/manual/core.html#logging. I was already excluding the logback jars and overriding the bootstrapLogging method based on information found elsewhere, but the part I was missing (in the config.yml file) was:

server:
  type: simple
  applicationContextPath: /application
  adminContextPath: /admin
  requestLog:
    type: external
logging:
  type: external

I just found this minutes ago, so that's as much as I know at the moment. (Not sure if every bit of the above is necessary, nor exactly what it does.) But it did let me use log4j without exceptions being thrown from code trying to use logback.

Edit: I discovered the same info is already in https://github.com/dropwizard/dropwizard/pull/1900 referenced in the OP, but was not official at that time.

Upvotes: 1

Related Questions