typesense/typesense-java

org.slf4j:slf4j-api should not be included in the jar

querry43 opened this issue · 9 comments

Description

We are primarily logging with log4j2, however some of our dependent packaged rely on slf4j logging. To get around this, we use log4j-slf4j-impl to consolidate logs into log4j2.

The typesense typesense-java-0.0.3.jar includes org.slf4j:slf4j-api and that makes it difficult to select the slf4j binding. We can use maven excludes to exclude ch.qos.logback:logback-classic and that works great, but removing org.slf4j:slf4j-api from the jar is quite a bit more work.

Steps to reproduce

  1. Create a slf4j application which uses log4j-slf4j-impl to log through log4j
  2. Include the typesense java client jar
  3. Run the app

You will see a bootstrap warning similar to this:

SLF4J: Found binding in [jar:file:/opt/tomcat/webapps/ROOT/WEB-INF/lib/log4j-slf4j-impl-2.18.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/tomcat/webapps/ROOT/WEB-INF/lib/typesense-java-0.0.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]

Expected Behavior

The typesense jars should not contain a slf4j binding.

Actual Behavior

SLF4J: Found binding in [jar:file:/opt/tomcat/webapps/ROOT/WEB-INF/lib/log4j-slf4j-impl-2.18.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/tomcat/webapps/ROOT/WEB-INF/lib/typesense-java-0.0.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]

Metadata

Typsense Version:

      <groupId>org.typesense</groupId>
      <artifactId>typesense-java</artifactId>
      <version>0.0.3</version>

OS:

OSX 12.3.1

If others are encountering this, here is our fix:

      <!-- remove some files from the typesense jar to fix logging -->
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>truezip-maven-plugin</artifactId>
        <version>1.2</version>
        <executions>
          <execution>
            <id>remove-a-file-in-sub-archive</id>
            <goals>
              <goal>remove</goal>
            </goals>
            <phase>package</phase>
            <configuration>
              <fileset>
                <directory>target/mywarfile.war/WEB-INF/lib/typesense-java-0.0.3.jar</directory>
                <includes>
                  <include>org/slf4j/impl</include>
                </includes>
              </fileset>
            </configuration>
          </execution>
        </executions>
      </plugin>

...

    <!-- https://mvnrepository.com/artifact/org.typesense/typesense-java -->
    <dependency>
      <groupId>org.typesense</groupId>
      <artifactId>typesense-java</artifactId>
      <version>0.0.3</version>

      <exclusions>
        <exclusion>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
        </exclusion>
        <exclusion>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
        </exclusion>
        <exclusion>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter</artifactId>
        </exclusion>
      </exclusions>
    </dependency>

Have same issue with library. Get a warning on Spring Boot 3 startup:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/*/.m2/repository/org/typesense/typesense-java/0.0.3/typesense-java-0.0.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/*/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

I've tried a few different exclusions but on Spring Boot 3 atleast, could not get it to work.

I've had to take out the SDK entirely and use HTTP directly so my JUnit 5 tests can run 🙈

Would it help if the client used log4j or log4j2 directly?

Can you please elaborate? Happy to find a permanent solution -- a PR will be most welcome as well.

@querry43

I've switched the client to use log4j2 in 0.0.6 -- can you please check now?

@kishorenc , thanks this looks good. It also removes some other dependencies i was explicitely ignoring.

Thank you for confirming!