在嵌入Spring Boot的遗留OSGI应用中使用JUL logging.properties的方法
Yes, you absolutely can use your existing logging.properties (JUL) with Spring Boot in an OSGi environment—let’s work through why your initial attempts didn’t stick and how to fix it step by step.
Why Your Initial Config Didn’t Work
Spring Boot’s logging system initializes extremely early in the startup process, often before application properties are loaded. In an OSGi context, classloader isolation and bundle startup order can also prevent your logging.config or LoggingSystem settings from being picked up in time. Additionally, if Logback dependencies are still present in your Spring Boot bundle, it might still take precedence despite your configuration.
Step 1: Fully Disable Logback in Spring Boot
First, you need to ensure Logback is completely excluded from your Spring Boot bundle—even transitive dependencies can trigger Logback’s auto-initialization.
For Maven:
Add exclusions to your Spring Boot starters to remove Logback, then ensure you’re using a JUL-compatible setup:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- Use core starter without Logback --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
For Gradle:
configurations { all { exclude group: 'ch.qos.logback', module: 'logback-classic' exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' } } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter' }
Step 2: Force Spring Boot to Use JUL Early
Since Spring Boot initializes logging before application properties, you need to set the LoggingSystem property as a system property (not just in application.properties), so it’s available during the earliest startup phase.
Option 1: Set via OSGi Framework Launch Parameters
Add these arguments when starting your OSGi framework:
-Dorg.springframework.boot.logging.LoggingSystem=org.springframework.boot.logging.java.JavaLoggingSystem -Djava.util.logging.config.file=/xxx/yyy/zzz/logging.properties
Option 2: Set in Your Bundle’s Activator
If you have a custom OSGi Activator for your Spring Boot bundle, set the system properties before initializing Spring Boot:
public class SpringBootBundleActivator implements BundleActivator { @Override public void start(BundleContext context) throws Exception { // Configure JUL before Spring Boot starts System.setProperty("org.springframework.boot.logging.LoggingSystem", "org.springframework.boot.logging.java.JavaLoggingSystem"); System.setProperty("java.util.logging.config.file", "/xxx/yyy/zzz/logging.properties"); // Launch Spring Boot application SpringApplication.run(YourSpringBootApp.class); } @Override public void stop(BundleContext context) throws Exception { SpringApplication.exit(SpringApplication.run(YourSpringBootApp.class)); } }
Step 3: Verify the Configuration
To confirm your setup is working, add a quick check in your Spring Boot application’s main method:
public static void main(String[] args) { // Validate JUL configuration before startup Logger globalLogger = Logger.getLogger(""); System.out.println("Active JUL Handlers: " + Arrays.toString(globalLogger.getHandlers())); System.out.println("Loaded JUL Config File: " + System.getProperty("java.util.logging.config.file")); SpringApplication.run(YourSpringBootApp.class, args); }
This will print the active JUL handlers and config file path, confirming your logging.properties is being used.
Key OSGi-Specific Notes
- ClassLoader Access: Ensure the path to
logging.propertiesis accessible to the Spring Boot bundle’s classloader. If the file lives in another OSGi bundle, expose it viaExport-Packageor move it to a shared classpath directory. - Global Logging Scope: JUL is a global system, so your
logging.propertieswill apply to all bundles. You can define bundle-specific rules using the bundle’s symbolic name (e.g.,com.yourcompany.legacybundle.level=FINE).
内容的提问来源于stack exchange,提问作者yk42b




