Skip to main content

dawdler-server-logback

Module introduction​

dawdler-server-logback is the logging implementation module for the dawdler server side, built as a logback extension that implements multi-module (multi-class-loader) log isolation through a custom SLF4J ServiceProvider.

dawdler-server deploys multiple services with isolated class loaders (DeployClassLoader), while standard logback uses a single global LoggerContext, which causes the logging configurations of different services to overwrite and conflict with each other. This module keys on the current thread context class loader and maintains an independent LoggerContext for each deployed module, so that every service can load its own logback.xml configuration independently without affecting the others.

1. Add the dependency to the pom​

<groupId>club.dawdler</groupId>
<artifactId>dawdler-server-logback</artifactId>

2. Implementation notes​

This module registers a custom SLF4J ServiceProvider through the SPI mechanism; the registration file is located at:

src/main/resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider

with the content:

club.dawdler.server.log.DawdlerLogbackServiceProvider

Core classes:

2.1 DawdlerLogbackServiceProvider​

Implements the org.slf4j.spi.SLF4JServiceProvider interface; the core logic is in the getLoggerFactory() method:

  • It keys on the current thread context class loader (Thread.currentThread().getContextClassLoader()).
  • It caches the LoggerContext for each class loader in a Map<ClassLoader, LoggerContext>.
  • On first retrieval it creates a LoggerContext, calls DawdlerLogbackContextInitializer to perform automatic configuration, and starts that context.
  • Repeated retrieval with the same class loader returns the same LoggerContext, guaranteeing a consistent logging context within a module.

2.2 DawdlerLogbackContextInitializer​

Responsible for initializing a single LoggerContext and loading its configuration; the configuration file lookup order matches standard logback:

  1. The configuration file specified by the system property logback.configurationFile.
  2. logback-test.xml on the classpath.
  3. logback.xml on the classpath.

Once a configuration file is found it is parsed with JoranConfigurator; if no configuration file is found, it falls back to BasicConfigurator for basic console output.

3. Configuration​

Each deployed module simply places a logback.xml in the root of its own classpath; the configuration is exactly the same as with standard logback.

Example:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

4. Prerequisites​

  • This module applies only to the dawdler-server side; dawdler-server deploys each service with an isolated class loader, and this module relies on that mechanism to achieve log isolation.
  • Do not also include the SLF4J binding of standard logback-classic on the classpath (whose default ServiceProvider is ch.qos.logback.classic.spi.LogbackServiceProvider), to avoid an SPI conflict with this module. This module already provides the core logback capabilities through logback-classic, so no additional binding is needed.