5

I have a Maven project with Java 9 and am using modules. Logback seems to support this since version 1.3.0-alpha1 but unfortunately I didn't got it to work.

I get the following message from SLF4J:

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.

It looks like it can't find Logback. And inspecting the (by Jlink generated) artifact using jimage list .../modules I can't find anything about logback apart my logback.xml configuration file.

Maybe the problem lies in my module-info.java:

open module my.super.app {
    requires jackson.annotations;
    requires jdk.jsobject;
    requires com.fasterxml.jackson.databind;
    requires com.fasterxml.jackson.core;
    requires javafx.graphics;
    requires javafx.controls;
    requires org.slf4j;

    exports my.super.app;
}

How and where do I declare to depend on Logback using Java 9 modules?

Gustave
  • 3,359
  • 4
  • 31
  • 64
Itchy
  • 2,263
  • 28
  • 41

2 Answers2

5

Short answer: You declare it in your jlink-call by adding logback to the module path and using the --bind-services option. In your module you only define an usage of SLF4j.

Long answer:

I made it work in a simple example with the following parts.

Starting from version 1.8.0 SLF4J is modularized and uses the ServiceLoader mechanism to find its logging backend.

Logback-classic:1.3.0-alpha4 is modularized, too, and declares a service both in META-INF/services/org.slf4j.spi.SLF4JServiceProvider and in its module descriptor:

provides org.slf4j.spi.SLF4JServiceProvider with ch.qos.logback.classic.spi.LogbackServiceProvider;

SLF4J declares in its module descriptor that it needs a SLF4JServiceProvider:

uses org.slf4j.spi.SLF4JServiceProvider

Therefore, in my POM (see below) I only declare a runtime dependency to Logback and my module descriptor only contains a "requires" clause to SLF4J.

My module "com.github.gv2011.j9mod.loguse" contains a main class that logs a statement to a SLF4J-Logger.

Further, it contains a logback.xml configuration file as resource. This file is accessible to Logback because it is a "miscellaneous resource", which is a resource not in a module package (see BuiltinClassLoader source).

Logback must be added explicitely to the runtime image via the jlink "--bind-services" option (because it is a service implementation).

Additionally, Logback and its dependencies must be available on the modulepath (jlink "--module-path" option).

Module descriptor:

module com.github.gv2011.j9mod.loguse {
  requires org.slf4j;
}

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.github.gv2011.j9mod</groupId>
  <artifactId>j9mod-log-use</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.8.0-beta4</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.3.0-alpha4</version>
        <scope>runtime</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>
    </plugins>
</build>

logback.xml:

<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <!-- "+++" just to make it obvious that this file is used: -->
      <pattern>+++ %d{"yyyy-MM-dd'T'HH:mm:ss,SSSXXX", UTC} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <logger name="com.github.gv2011.j9mod.loguse.Main" level="INFO" />

  <root level="WARN">
    <appender-ref ref="STDOUT" />
  </root>

</configuration>

Main-Class:

package com.github.gv2011.j9mod.loguse;
import static org.slf4j.LoggerFactory.getLogger;
import org.slf4j.Logger;

public class Main {

  private static final Logger LOG = getLogger(Main.class);

  public static void main(final String[] args) {
    LOG.info("Tach.");
  }
}

Output (Eclipse - Run As - Java Application):

+++ 2019-05-09T17:43:20,594Z [main] INFO  com.github.gv2011.j9mod.loguse.Main - Tach.

jlink:

%JAVA_HOME%\bin\jlink.exe ^
--output target\image ^
--module-path ^
target\classes;^
%M2_REPO%\org\slf4j\slf4j-api\1.8.0-beta4\slf4j-api-1.8.0-beta4.jar;^
%M2_REPO%\ch\qos\logback\logback-classic\1.3.0-alpha4\logback-classic-1.3.0-alpha4.jar;^
%M2_REPO%\ch\qos\logback\logback-core\1.3.0-alpha4\logback-core-1.3.0-alpha4.jar ^
--bind-services ^
--launcher hello=com.github.gv2011.j9mod.loguse/com.github.gv2011.j9mod.loguse.Main ^
--add-modules com.github.gv2011.j9mod.loguse

Output (target/image/bin/hello):

+++ 2019-05-10T11:45:24,844Z [main] INFO  com.github.gv2011.j9mod.loguse.Main - Tach.

Tested with openjdk-11.0.2_windows-x64, apache-maven-3.6.0 and eclipse-jee-2019-03-R-win32-x86_64 on Windows 10, 64 Bit.

Gustave
  • 3,359
  • 4
  • 31
  • 64
3

I finally figured it out. At the end, I just added Logback as a dependency in my module-info.java:

open module my.super.app {
    requires jackson.annotations;
    requires jdk.jsobject;
    requires com.fasterxml.jackson.databind;
    requires com.fasterxml.jackson.core;
    requires javafx.graphics;
    requires javafx.controls;
    requires org.slf4j;
    requires ch.qos.logback.classic; //only runtime dependency

    exports my.super.app;
}

I guess there is no way to declare a runtime dependency (similar to Maven) - it is now also required at compile time.

Itchy
  • 2,263
  • 28
  • 41