Skip to content

Commit

Permalink
Refactor DefaultLoggingRuleConfigurationBuilder to reduce usage of …
Browse files Browse the repository at this point in the history
…`logback-classic` (#32418)
  • Loading branch information
linghengqian authored Aug 7, 2024
1 parent a1f5d22 commit 4216911
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion kernel/logging/core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>provided</scope>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@

package org.apache.shardingsphere.logging.rule.builder;

import org.slf4j.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.OutputStreamAppender;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import ch.qos.logback.core.pattern.PatternLayoutBase;
import org.apache.shardingsphere.infra.rule.builder.global.DefaultGlobalRuleConfigurationBuilder;
import org.apache.shardingsphere.logging.config.LoggingRuleConfiguration;
import org.apache.shardingsphere.logging.constant.LoggingOrder;
import org.apache.shardingsphere.logging.logger.ShardingSphereAppender;
import org.apache.shardingsphere.logging.logger.ShardingSphereLogger;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Objects;
import java.util.TreeSet;
import java.util.stream.Collectors;
Expand All @@ -46,8 +46,12 @@ public final class DefaultLoggingRuleConfigurationBuilder implements DefaultGlob

@Override
public LoggingRuleConfiguration build() {
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
return new LoggingRuleConfiguration(getDefaultLoggers(loggerContext), getDefaultAppenders(loggerContext));
ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
if ("ch.qos.logback.classic.LoggerContext".equals(iLoggerFactory.getClass().getName())) {
LoggerContext loggerContext = (LoggerContext) iLoggerFactory;
return new LoggingRuleConfiguration(getDefaultLoggers(loggerContext), getDefaultAppenders(loggerContext));
}
return new LoggingRuleConfiguration(Collections.emptyList(), Collections.emptySet());
}

private Collection<ShardingSphereLogger> getDefaultLoggers(final LoggerContext loggerContext) {
Expand All @@ -59,9 +63,8 @@ private Collection<ShardingSphereLogger> getDefaultLoggers(final LoggerContext l

private Collection<ShardingSphereAppender> getDefaultAppenders(final LoggerContext loggerContext) {
return loggerContext.getLoggerList().stream().filter(each -> null != each.getLevel()).filter(each -> !Logger.ROOT_LOGGER_NAME.equalsIgnoreCase(each.getName())).map(each -> {
Iterator<Appender<ILoggingEvent>> appenderIterator = each.iteratorForAppenders();
if (appenderIterator.hasNext()) {
Appender<ILoggingEvent> appender = appenderIterator.next();
if (each.iteratorForAppenders().hasNext()) {
Appender<?> appender = each.iteratorForAppenders().next();
ShardingSphereAppender shardingSphereAppender = new ShardingSphereAppender(appender.getName(), appender.getClass().getName(), getAppenderPattern(appender));
getFileOutput(appender, shardingSphereAppender);
return shardingSphereAppender;
Expand All @@ -70,19 +73,19 @@ private Collection<ShardingSphereAppender> getDefaultAppenders(final LoggerConte
}).filter(Objects::nonNull).collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ShardingSphereAppender::getAppenderName))));
}

private String getAppenderPattern(final Appender<ILoggingEvent> appender) {
private String getAppenderPattern(final Appender<?> appender) {
if (appender instanceof OutputStreamAppender) {
OutputStreamAppender<ILoggingEvent> consoleAppender = (OutputStreamAppender<ILoggingEvent>) appender;
LayoutWrappingEncoder<ILoggingEvent> encoder = (LayoutWrappingEncoder<ILoggingEvent>) consoleAppender.getEncoder();
PatternLayout layout = (PatternLayout) encoder.getLayout();
OutputStreamAppender<?> consoleAppender = (OutputStreamAppender<?>) appender;
LayoutWrappingEncoder<?> encoder = (LayoutWrappingEncoder<?>) consoleAppender.getEncoder();
PatternLayoutBase<?> layout = (PatternLayoutBase<?>) encoder.getLayout();
return layout.getPattern();
}
return "";
}

private void getFileOutput(final Appender<ILoggingEvent> appender, final ShardingSphereAppender shardingSphereAppender) {
private void getFileOutput(final Appender<?> appender, final ShardingSphereAppender shardingSphereAppender) {
if (appender instanceof FileAppender) {
shardingSphereAppender.setFile(((FileAppender<ILoggingEvent>) appender).getFile());
shardingSphereAppender.setFile(((FileAppender<?>) appender).getFile());
}
}

Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,7 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.cedarsoftware</groupId>
Expand Down

0 comments on commit 4216911

Please sign in to comment.