Skip to content

Commit 611dc54

Browse files
committed
优化 solon-logging 初始化处理
1 parent 58c0968 commit 611dc54

File tree

3 files changed

+59
-41
lines changed

3 files changed

+59
-41
lines changed

solon-jakarta-projects/solon-logging/solon-logging-logback-jakarta/src/main/java/ch/qos/logback/solon/integration/LogIncubatorImpl.java

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2024 noear.org and authors
2+
* Copyright 2017-2025 noear.org and authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818
import ch.qos.logback.classic.Level;
1919
import ch.qos.logback.classic.Logger;
2020
import ch.qos.logback.classic.LoggerContext;
21-
import ch.qos.logback.core.joran.spi.JoranException;
2221
import ch.qos.logback.core.status.Status;
2322
import ch.qos.logback.core.status.StatusUtil;
2423
import ch.qos.logback.core.util.StatusPrinter;
@@ -27,14 +26,14 @@
2726
import org.noear.solon.Solon;
2827
import org.noear.solon.Utils;
2928
import org.noear.solon.core.runtime.NativeDetector;
30-
import org.noear.solon.core.util.*;
29+
import org.noear.solon.core.util.ClassUtil;
30+
import org.noear.solon.core.util.JavaUtil;
31+
import org.noear.solon.core.util.ResourceUtil;
3132
import org.noear.solon.logging.LogIncubator;
3233
import org.noear.solon.logging.LogOptions;
3334
import org.noear.solon.logging.model.LoggerLevelEntity;
3435
import org.slf4j.LoggerFactory;
3536

36-
import java.io.File;
37-
import java.net.MalformedURLException;
3837
import java.net.URL;
3938
import java.util.List;
4039

@@ -45,7 +44,6 @@
4544
* @since 2.4
4645
*/
4746
public class LogIncubatorImpl implements LogIncubator {
48-
4947
@Override
5048
public void incubate() throws Throwable {
5149
if (JavaUtil.IS_WINDOWS && Solon.cfg().isFilesMode() == false) {
@@ -65,48 +63,56 @@ public void incubate() throws Throwable {
6563
//尝试从配置里获取
6664
URL url = getUrlOfConfig();
6765

66+
//加载配置文件
67+
doLoadUrl(url);
68+
doInit();
69+
}
70+
71+
protected void doLoadUrl(URL url) throws Exception {
6872
//尝试包内定制加载
6973
if (url == null) {
7074
//检查是否有原生配置文件
7175
if (ResourceUtil.hasResource("logback.xml")) {
7276
//如果有直接返回(不支持对它进行 Solon 扩展)
7377
return;
7478
}
75-
}
7679

77-
//1::尝试应用环境加载
78-
if (url == null) {
79-
if (Utils.isNotEmpty(Solon.cfg().env())) {
80-
url = ResourceUtil.getResource("logback-solon-" + Solon.cfg().env() + ".xml");
80+
//1::尝试应用环境加载
81+
if (url == null) {
82+
if (Utils.isNotEmpty(Solon.cfg().env())) {
83+
url = ResourceUtil.getResource("logback-solon-" + Solon.cfg().env() + ".xml");
84+
}
85+
}
86+
87+
//2::尝试应用加载
88+
if (url == null) {
89+
url = ResourceUtil.getResource("logback-solon.xml");
8190
}
8291
}
8392

84-
//2::尝试应用加载
93+
/// ///////////
94+
95+
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
96+
97+
loggerContext.reset();
98+
99+
SolonConfigurator configurator = new SolonConfigurator();
100+
configurator.setContext(loggerContext);
101+
85102
if (url == null) {
86-
url = ResourceUtil.getResource("logback-solon.xml");
103+
//::尝试默认加载
104+
DefaultLogbackConfiguration configuration = new DefaultLogbackConfiguration();
105+
configuration.apply(new LogbackConfigurator(loggerContext));
106+
} else {
107+
//::加载 xml url
108+
configurator.doConfigure(url);
87109
}
88-
89-
initDo(url);
90110
}
91111

92-
private void initDo(URL url) {
112+
protected void doInit() {
93113
try {
94114
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
95115

96-
loggerContext.reset();
97-
98-
SolonConfigurator configurator = new SolonConfigurator();
99-
configurator.setContext(loggerContext);
100-
101-
if (url == null) {
102-
//::尝试默认加载
103-
DefaultLogbackConfiguration configuration = new DefaultLogbackConfiguration();
104-
configuration.apply(new LogbackConfigurator(loggerContext));
105-
} else {
106-
//::加载url
107-
configurator.doConfigure(url);
108-
}
109-
110116
//同步 logger level 配置
111117
if (LogOptions.getLoggerLevels().size() > 0) {
112118
for (LoggerLevelEntity lle : LogOptions.getLoggerLevels()) {
@@ -118,17 +124,18 @@ private void initDo(URL url) {
118124
if (NativeDetector.inNativeImage()) {
119125
reportConfigurationErrorsIfNecessary(loggerContext);
120126
}
121-
} catch (JoranException e) {
127+
} catch (Exception e) {
122128
throw new IllegalStateException(e);
123129
}
124130
}
125131

132+
126133
/**
127134
* 报告配置错误(原生运行时)
128-
*
129135
*/
130136
private void reportConfigurationErrorsIfNecessary(LoggerContext loggerContext) {
131137
List<Status> statuses = loggerContext.getStatusManager().getCopyOfStatusList();
138+
132139
StringBuilder errors = new StringBuilder();
133140
for (Status status : statuses) {
134141
if (status.getLevel() == Status.ERROR) {
@@ -149,16 +156,15 @@ private void reportConfigurationErrorsIfNecessary(LoggerContext loggerContext) {
149156
/**
150157
* 基于配置,获取日志配置文件
151158
*/
152-
private URL getUrlOfConfig() throws MalformedURLException {
159+
private URL getUrlOfConfig() {
153160
String logConfig = Solon.cfg().get("solon.logging.config");
154161

155162
if (Utils.isNotEmpty(logConfig)) {
156-
File logConfigFile = new File(logConfig);
157-
if (logConfigFile.exists()) {
158-
return logConfigFile.toURI().toURL();
163+
URL logConfigUrl = ResourceUtil.findResource(logConfig);
164+
if (logConfigUrl != null) {
165+
return logConfigUrl;
159166
} else {
160-
//改成异步,不然 LogUtil.global() 初始化未完成
161-
System.err.println("Props: No log config file: " + logConfig);
167+
System.err.println("Props: No logging config file exists: " + logConfig);
162168
}
163169
}
164170

solon-jakarta-projects/solon-logging/solon-logging-logback-jakarta/src/main/java/ch/qos/logback/solon/integration/XPluginImp.java renamed to solon-jakarta-projects/solon-logging/solon-logging-logback-jakarta/src/main/java/ch/qos/logback/solon/integration/LogbackPlugin.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2024 noear.org and authors
2+
* Copyright 2017-2025 noear.org and authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,14 +18,26 @@
1818
import org.noear.solon.core.AppContext;
1919
import org.noear.solon.core.Plugin;
2020

21+
import java.net.URL;
22+
2123
/**
2224
* @author noear
2325
* @since 1.6
2426
*/
25-
public class XPluginImp extends LogIncubatorImpl implements Plugin {
27+
public class LogbackPlugin extends LogIncubatorImpl implements Plugin {
2628
@Override
2729
public void start(AppContext context) throws Throwable {
2830
//容器加载完后,允许再次处理
2931
incubate();
3032
}
33+
34+
@Override
35+
protected void doLoadUrl(URL url) throws Exception {
36+
if (url == null) {
37+
//说明没有指定配置文件。。。不需要再次默认加载
38+
return;
39+
}
40+
41+
super.doLoadUrl(url);
42+
}
3143
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
solon.plugin=ch.qos.logback.solon.integration.XPluginImp
1+
solon.plugin=ch.qos.logback.solon.integration.LogbackPlugin
22
solon.plugin.priority=70

0 commit comments

Comments
 (0)