-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #11250 参考Log4J2NacosLogging处理,当location不存在时则不加载 * #11250 nacosLogbackConfigurators.stream() .filter(c -> c.getVersion() == userVersion).collect(Collectors.toList())优化存在越界情况 * 调整异常输出 * #11250 移除无用注释代码 * #11250 调整代码风格样式 * #11250 默认加载方式为log4j,导致该测试用例异常;已修订 * #11250 按模板重新格式化代码
- Loading branch information
Showing
2 changed files
with
40 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,16 +25,16 @@ | |
import com.alibaba.nacos.common.log.NacosLogbackConfigurator; | ||
import com.alibaba.nacos.common.spi.NacosServiceLoader; | ||
import com.alibaba.nacos.common.utils.ResourceUtils; | ||
import com.alibaba.nacos.common.utils.StringUtils; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collection; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Support for Logback version 1.0.8 or higher | ||
* | ||
* @author <a href="mailto:[email protected]">hxy1991</a> | ||
* @author <a href="mailto:[email protected]">hujun</a> | ||
* | ||
* @since 0.9.0 | ||
*/ | ||
public class LogbackNacosLogging extends AbstractNacosLogging { | ||
|
@@ -61,7 +61,7 @@ public void loadConfiguration() { | |
addListener(loggerContext); | ||
} | ||
} | ||
|
||
private boolean hasListener(LoggerContext loggerContext) { | ||
for (LoggerContextListener loggerContextListener : loggerContext.getCopyOfListenerList()) { | ||
if (loggerContextListener instanceof NacosLoggerContextListener) { | ||
|
@@ -70,50 +70,55 @@ private boolean hasListener(LoggerContext loggerContext) { | |
} | ||
return false; | ||
} | ||
|
||
private LoggerContext loadConfigurationOnStart() { | ||
String location = getLocation(NACOS_LOGBACK_LOCATION); | ||
try { | ||
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
Collection<NacosLogbackConfigurator> nacosLogbackConfigurators = NacosServiceLoader.load( | ||
NacosLogbackConfigurator.class); | ||
NacosLogbackConfigurator nacosLogbackConfigurator = nacosLogbackConfigurators.stream() | ||
.filter(c -> c.getVersion() == userVersion).collect(Collectors.toList()).get(0); | ||
nacosLogbackConfigurator.setContext(loggerContext); | ||
nacosLogbackConfigurator.configure(ResourceUtils.getResourceUrl(location)); | ||
return loggerContext; | ||
} catch (Exception e) { | ||
throw new IllegalStateException("Could not initialize Logback Nacos logging from " + location, e); | ||
} | ||
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
Collection<NacosLogbackConfigurator> nacosLogbackConfigurators = NacosServiceLoader.load( | ||
NacosLogbackConfigurator.class); | ||
nacosLogbackConfigurators.stream().filter(c -> c.getVersion() == userVersion).findFirst() | ||
.ifPresent(nacosLogbackConfigurator -> { | ||
nacosLogbackConfigurator.setContext(loggerContext); | ||
if (StringUtils.isNotBlank(location)) { | ||
try { | ||
nacosLogbackConfigurator.configure(ResourceUtils.getResourceUrl(location)); | ||
} catch (Exception e) { | ||
throw new IllegalStateException( | ||
"Could not initialize Logback Nacos logging from " + location, e); | ||
} | ||
} | ||
}); | ||
return loggerContext; | ||
} | ||
|
||
class NacosLoggerContextListener implements LoggerContextListener { | ||
|
||
@Override | ||
public boolean isResetResistant() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onReset(LoggerContext context) { | ||
loadConfigurationOnStart(); | ||
} | ||
|
||
@Override | ||
public void onStart(LoggerContext context) { | ||
|
||
} | ||
|
||
@Override | ||
public void onStop(LoggerContext context) { | ||
|
||
} | ||
|
||
@Override | ||
public void onLevelChange(Logger logger, Level level) { | ||
|
||
} | ||
} | ||
|
||
private void addListener(LoggerContext loggerContext) { | ||
loggerContext.addListener(new NacosLoggerContextListener()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters