diff --git a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/CommonLoggingApplicationListener.java b/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/CommonLoggingApplicationListener.java deleted file mode 100644 index 60ee5a4c9..000000000 --- a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/CommonLoggingApplicationListener.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alipay.sofa.boot.logging; - -import static com.alipay.sofa.common.log.Constants.LOG_ENCODING_PROP_KEY; -import static com.alipay.sofa.common.log.Constants.LOG_PATH; -import static com.alipay.sofa.common.log.Constants.OLD_LOG_PATH; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; -import org.springframework.context.ApplicationListener; -import org.springframework.core.Ordered; -import org.springframework.core.PriorityOrdered; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.core.env.EnumerablePropertySource; -import org.springframework.core.env.PropertySource; - -import com.alipay.sofa.common.log.Constants; -import com.alipay.sofa.common.log.LoggerSpaceManager; -import com.alipay.sofa.common.log.MultiAppLoggerSpaceManager; -import com.alipay.sofa.common.log.SpaceId; -import com.alipay.sofa.common.log.SpaceInfo; -import com.alipay.sofa.common.log.env.LogEnvUtils; -import com.alipay.sofa.common.log.factory.AbstractLoggerSpaceFactory; -import com.alipay.sofa.common.log.factory.Log4j2LoggerSpaceFactory; -import com.alipay.sofa.common.log.factory.LogbackLoggerSpaceFactory; -import com.alipay.sofa.common.utils.ReportUtil; -import com.alipay.sofa.common.utils.StringUtil; - -/** - * @author qilong.zql - * @since 1.0.15 - */ -public class CommonLoggingApplicationListener - implements - ApplicationListener, - Ordered { - - @Override - public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { - if (DefaultReInitializerChecker.isReInitialized.compareAndSet(false, true)) { - reInitializeLog(loadApplicationEnvironment(event.getEnvironment())); - } - } - - public void setReInitialize(boolean value) { - DefaultReInitializerChecker.isReInitialized.set(value); - } - - public void reInitializeLog(Map context) { - for (String key : context.keySet()) { - if (key.startsWith(Constants.SOFA_MIDDLEWARE_CONFIG_PREFIX) - && !key.equals(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH) - && key.endsWith(Constants.CONSOLE_SUFFIX)) { - int index = Constants.SOFA_MIDDLEWARE_CONFIG_PREFIX.length(); - // minus length of .console - int end = key.length() - Constants.CONSOLE_SUFFIX.length(); - String spaceId = key.substring(index, end); - LoggerSpaceManager.getLoggerBySpace(spaceId, spaceId); - } - } - - for (Map.Entry entry : MultiAppLoggerSpaceManager.getSpacesMap() - .entrySet()) { - SpaceId spaceId = entry.getKey(); - SpaceInfo spaceInfo = entry.getValue(); - ReportUtil.reportDebug("Re-initialize log of " + spaceId.getSpaceName()); - AbstractLoggerSpaceFactory abstractLoggerSpaceFactory = spaceInfo - .getAbstractLoggerSpaceFactory(); - if (abstractLoggerSpaceFactory instanceof LogbackLoggerSpaceFactory) { - ((LogbackLoggerSpaceFactory) abstractLoggerSpaceFactory).reInitialize(context); - } - if (abstractLoggerSpaceFactory instanceof Log4j2LoggerSpaceFactory) { - ((Log4j2LoggerSpaceFactory) abstractLoggerSpaceFactory).reInitialize(context); - } - } - } - - /** - * load log configuration in application.properties - * - * @param environment - * @return - */ - private Map loadApplicationEnvironment(ConfigurableEnvironment environment) { - Map context = new HashMap(); - readLogConfiguration(LOG_PATH, environment.getProperty(LOG_PATH), context, - Constants.LOGGING_PATH_DEFAULT); - readLogConfiguration(OLD_LOG_PATH, environment.getProperty(OLD_LOG_PATH), context, - context.get(LOG_PATH)); - readLogConfiguration(LOG_ENCODING_PROP_KEY, environment.getProperty(LOG_ENCODING_PROP_KEY), - context); - LogEnvUtils.keepCompatible(context, true); - - Set configKeys = new HashSet(); - Iterator> propertySourceIterator = environment.getPropertySources() - .iterator(); - while (propertySourceIterator.hasNext()) { - PropertySource propertySource = propertySourceIterator.next(); - if (propertySource instanceof EnumerablePropertySource) { - configKeys.addAll(Arrays.asList(((EnumerablePropertySource) propertySource) - .getPropertyNames())); - } - } - for (String key : configKeys) { - if (LogEnvUtils.filterAllLogConfig(key)) { - addToGlobalSystemProperties(key, environment.getProperty(key)); - readLogConfiguration(key, environment.getProperty(key), context); - } - } - return context; - } - - private void addToGlobalSystemProperties(String key, String value) { - if (!StringUtil.isBlank(key) && !StringUtil.isBlank(value)) { - LogEnvUtils.processGlobalSystemLogProperties().put(key, value); - } - } - - private void readLogConfiguration(String key, String value, Map context) { - if (!StringUtil.isBlank(value)) { - context.put(key, value); - } - } - - private void readLogConfiguration(String key, String value, Map context, - String defaultValue) { - if (!StringUtil.isBlank(value)) { - context.put(key, value); - } else { - context.put(key, defaultValue); - } - } - - @Override - public int getOrder() { - return PriorityOrdered.HIGHEST_PRECEDENCE + 20; - } - -} \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/DefaultReInitializerChecker.java b/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/DefaultReInitializerChecker.java deleted file mode 100644 index 8dff47dc7..000000000 --- a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/DefaultReInitializerChecker.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alipay.sofa.boot.logging; - -import java.util.concurrent.atomic.AtomicBoolean; - -import com.alipay.sofa.common.log.spi.ReInitializeChecker; - -/** - * @author qilong.zql - * @since 1.0.17 - */ -public class DefaultReInitializerChecker implements ReInitializeChecker { - - public final static AtomicBoolean isReInitialized = new AtomicBoolean(false); - - @Override - public boolean isReInitialize() { - return DefaultReInitializerChecker.isReInitialized.get(); - } -} \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/LogEnvironmentPreparingListener.java b/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/LogEnvironmentPreparingListener.java new file mode 100644 index 000000000..768415c11 --- /dev/null +++ b/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/LogEnvironmentPreparingListener.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alipay.sofa.boot.logging; + +import com.alipay.sofa.common.log.Constants; +import com.alipay.sofa.common.log.env.LogEnvUtils; +import com.alipay.sofa.common.utils.StringUtil; +import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.PropertySource; + +/** + * @author Alaneuler + * Created on 2020/11/7 + */ +public class LogEnvironmentPreparingListener + implements + ApplicationListener, + Ordered { + @Override + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { + prepare(event.getEnvironment()); + } + + @Override + public int getOrder() { + return Ordered.HIGHEST_PRECEDENCE + 100; + } + + private void prepare(ConfigurableEnvironment environment) { + loadLogConfiguration(Constants.LOG_PATH, environment.getProperty(Constants.LOG_PATH)); + loadLogConfiguration(Constants.OLD_LOG_PATH, + environment.getProperty(Constants.OLD_LOG_PATH)); + loadLogConfiguration(Constants.LOG_ENCODING_PROP_KEY, + environment.getProperty(Constants.LOG_ENCODING_PROP_KEY)); + + for (PropertySource propertySource : environment.getPropertySources()) { + if (propertySource instanceof EnumerablePropertySource) { + for (String key : ((EnumerablePropertySource) propertySource).getPropertyNames()) { + if (LogEnvUtils.filterAllLogConfig(key)) { + loadLogConfiguration(key, environment.getProperty(key)); + } + } + } + } + } + + private void loadLogConfiguration(String key, String value) { + if (!System.getProperties().contains(key) && !System.getenv().containsKey(key) + && StringUtil.isNotEmpty(value)) { + System.setProperty(key, value); + } + } +} diff --git a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/Mark.java b/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/Mark.java deleted file mode 100644 index aec8ec2ac..000000000 --- a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/Mark.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alipay.sofa.boot.logging; - -/** - * @author qilong.zql - * @since 1.0.17 - */ -public class Mark { -} \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/filter/DefaultLog4j2FilterGenerator.java b/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/filter/DefaultLog4j2FilterGenerator.java deleted file mode 100644 index baf3a80db..000000000 --- a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/filter/DefaultLog4j2FilterGenerator.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alipay.sofa.boot.logging.filter; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.core.Filter; -import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.Logger; -import org.apache.logging.log4j.core.filter.AbstractFilter; -import org.apache.logging.log4j.message.Message; - -import com.alipay.sofa.common.log.spi.Log4j2FilterGenerator; - -/** - * @author qilong.zql - * @since 1.0.15 - */ -public class DefaultLog4j2FilterGenerator implements Log4j2FilterGenerator { - - public static final Filter FILTER = new AbstractFilter() { - - @Override - public Result filter(LogEvent event) { - return Result.DENY; - } - - @Override - public Result filter(Logger logger, Level level, - Marker marker, Message msg, - Throwable t) { - return Result.DENY; - } - - @Override - public Result filter(Logger logger, Level level, - Marker marker, Object msg, - Throwable t) { - return Result.DENY; - } - - @Override - public Result filter(Logger logger, Level level, - Marker marker, String msg, - Object... params) { - return Result.DENY; - } - - }; - - @Override - public Filter[] generatorFilters() { - return new Filter[] { FILTER }; - } -} \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/filter/DefaultLogbackFilterGenerator.java b/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/filter/DefaultLogbackFilterGenerator.java deleted file mode 100644 index d28e933eb..000000000 --- a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/filter/DefaultLogbackFilterGenerator.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alipay.sofa.boot.logging.filter; - -import org.slf4j.Marker; - -import com.alipay.sofa.common.log.spi.LogbackFilterGenerator; - -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.turbo.TurboFilter; -import ch.qos.logback.core.spi.FilterReply; - -/** - * @author qilong.zql - * @since 1.0.15 - */ -public class DefaultLogbackFilterGenerator implements LogbackFilterGenerator { - public static final TurboFilter FILTER = new TurboFilter() { - - @Override - public FilterReply decide(Marker marker, - ch.qos.logback.classic.Logger logger, - Level level, - String format, - Object[] params, - Throwable t) { - return FilterReply.DENY; - } - - }; - - @Override - public TurboFilter[] generatorFilters() { - return new TurboFilter[] { FILTER }; - } -} \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/initializer/DefaultLog4j2ReInitializer.java b/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/initializer/DefaultLog4j2ReInitializer.java deleted file mode 100644 index 8acf1d36c..000000000 --- a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/initializer/DefaultLog4j2ReInitializer.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alipay.sofa.boot.logging.initializer; - -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL; -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH; -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOG4J2_PATTERN; -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOG4J2_PATTERN_DEFAULT; -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL; -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH; - -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Map; -import java.util.Properties; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.ThreadContext; -import org.apache.logging.log4j.core.Logger; -import org.apache.logging.log4j.core.LoggerContext; -import org.apache.logging.log4j.core.appender.ConsoleAppender; -import org.apache.logging.log4j.core.config.Configuration; -import org.apache.logging.log4j.core.config.ConfigurationFactory; -import org.apache.logging.log4j.core.config.ConfigurationSource; -import org.apache.logging.log4j.core.filter.AbstractFilter; -import org.apache.logging.log4j.core.layout.PatternLayout; -import org.apache.logging.log4j.message.Message; - -import com.alipay.sofa.boot.logging.filter.DefaultLog4j2FilterGenerator; -import com.alipay.sofa.boot.logging.util.SystemPropertiesGetter; -import com.alipay.sofa.common.log.SpaceId; -import com.alipay.sofa.common.log.spi.Log4j2ReInitializer; -import com.alipay.sofa.common.utils.ResourceUtil; -import com.alipay.sofa.common.utils.StringUtil; - -/** - * @author qilong.zql - * @since 1.0.15 - */ -public class DefaultLog4j2ReInitializer implements Log4j2ReInitializer { - - private static final String FILE_PROTOCOL = "file"; - private static final String SOFA_CONSOLE = "sofa-console"; - - @Override - public void reInitialize(final SpaceId spaceId, LoggerContext loggerContext, - final Properties properties, URL confFile) { - if (isAlreadyReInitialized(loggerContext)) { - return; - } - loggerContext.removeFilter(DefaultLog4j2FilterGenerator.FILTER); - initLogContext(properties); - markAsReInitialized(loggerContext); - if (isConsoleAppenderOpen(spaceId.getSpaceName(), properties)) { - final ConsoleAppender consoleAppender = consoleAppender(properties); - loggerContext.addFilter(new AbstractFilter() { - @Override - public Result filter(Logger logger, Level level, Marker marker, Message msg, - Throwable t) { - if (!logger.getAppenders().containsKey(SOFA_CONSOLE)) { - resetLog(logger); - } - return Result.NEUTRAL; - } - - @Override - public Result filter(Logger logger, Level level, Marker marker, Object msg, - Throwable t) { - if (!logger.getAppenders().containsKey(SOFA_CONSOLE)) { - resetLog(logger); - } - return Result.NEUTRAL; - } - - @Override - public Result filter(Logger logger, Level level, Marker marker, String msg, - Object... params) { - if (!logger.getAppenders().containsKey(SOFA_CONSOLE)) { - resetLog(logger); - } - return Result.NEUTRAL; - } - - private void resetLog(Logger logger) { - logger.getAppenders().clear(); - logger.addAppender(consoleAppender); - logger.setLevel(getConsoleLevel(spaceId.getSpaceName(), properties)); - logger.setAdditive(false); - } - }); - } else { - try { - // remove first initialize - loggerContext.setExternalContext(null); - ConfigurationSource source = getConfigurationSource(confFile); - Configuration config = ConfigurationFactory.getInstance().getConfiguration( - loggerContext, source); - for (Map.Entry entry : properties.entrySet()) { - config.getProperties().put((String) entry.getKey(), (String) entry.getValue()); - } - loggerContext.start(config); - } catch (Throwable ex) { - throw new IllegalStateException("log4j2 loggerSpaceFactory re-build error", ex); - } - - } - } - - private Level getConsoleLevel(String spaceId, Properties properties) { - SystemPropertiesGetter propertiesGetter = new SystemPropertiesGetter(properties); - String level = propertiesGetter.getProperty(SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL); - String defaultLevel = StringUtil.isBlank(level) ? "INFO" : level; - level = propertiesGetter.getProperty( - String.format(SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL, spaceId), defaultLevel); - return Level.toLevel(level, Level.INFO); - } - - private ConsoleAppender consoleAppender(Properties properties) { - SystemPropertiesGetter propertiesGetter = new SystemPropertiesGetter(properties); - String logPattern = propertiesGetter.getProperty( - SOFA_MIDDLEWARE_LOG_CONSOLE_LOG4J2_PATTERN, - SOFA_MIDDLEWARE_LOG_CONSOLE_LOG4J2_PATTERN_DEFAULT); - ConsoleAppender.Builder builder = ConsoleAppender.newBuilder(); - builder.withLayout(PatternLayout.newBuilder().withPattern(logPattern).build()).withName( - SOFA_CONSOLE); - ConsoleAppender consoleAppender = builder.build(); - consoleAppender.start(); - return consoleAppender; - } - - private ConfigurationSource getConfigurationSource(URL url) throws IOException { - InputStream stream = url.openStream(); - if (FILE_PROTOCOL.equals(url.getProtocol())) { - return new ConfigurationSource(stream, ResourceUtil.getFile(url)); - } - return new ConfigurationSource(stream, url); - } - - private boolean isAlreadyReInitialized(LoggerContext loggerContext) { - if (loggerContext.getConfiguration().getProperties() - .get(DefaultLog4j2ReInitializer.class.getCanonicalName()) != null) { - return true; - } - return false; - } - - private void markAsReInitialized(LoggerContext loggerContext) { - loggerContext.getConfiguration().getProperties() - .put(DefaultLog4j2ReInitializer.class.getCanonicalName(), ""); - } - - private void initLogContext(Properties properties) { - for (Map.Entry entry : properties.entrySet()) { - ThreadContext.put((String) entry.getKey(), - properties.getProperty((String) entry.getKey())); - } - } - - private boolean isConsoleAppenderOpen(String spaceId, Properties properties) { - SystemPropertiesGetter propertiesGetter = new SystemPropertiesGetter(properties); - String value = propertiesGetter.getProperty(String.format( - SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, spaceId)); - if (StringUtil.isBlank(value)) { - return "true".equalsIgnoreCase(propertiesGetter - .getProperty(SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH)); - } else { - return "true".equalsIgnoreCase(value); - } - } -} \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/initializer/DefaultLogbackReInitializer.java b/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/initializer/DefaultLogbackReInitializer.java deleted file mode 100644 index ada14614a..000000000 --- a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/initializer/DefaultLogbackReInitializer.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alipay.sofa.boot.logging.initializer; - -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL; -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH; -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN; -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN_DEFAULT; -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL; -import static com.alipay.sofa.common.log.Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH; - -import java.net.URL; -import java.util.Map; -import java.util.Properties; - -import org.slf4j.Marker; - -import com.alipay.sofa.boot.logging.filter.DefaultLogbackFilterGenerator; -import com.alipay.sofa.boot.logging.util.SystemPropertiesGetter; -import com.alipay.sofa.common.log.SpaceId; -import com.alipay.sofa.common.log.spi.LogbackReInitializer; -import com.alipay.sofa.common.utils.ClassUtil; -import com.alipay.sofa.common.utils.StringUtil; - -import ch.qos.logback.classic.Level; -import ch.qos.logback.classic.Logger; -import ch.qos.logback.classic.LoggerContext; -import ch.qos.logback.classic.encoder.PatternLayoutEncoder; -import ch.qos.logback.classic.jul.LevelChangePropagator; -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.classic.turbo.TurboFilter; -import ch.qos.logback.classic.util.ContextInitializer; -import ch.qos.logback.core.ConsoleAppender; -import ch.qos.logback.core.joran.spi.JoranException; -import ch.qos.logback.core.spi.FilterReply; -import ch.qos.logback.core.util.OptionHelper; - -/** - * @author qilong.zql - * @since 1.0.15 - */ -public class DefaultLogbackReInitializer implements LogbackReInitializer { - - private static final String BRIDGE_HANDLER = "org.slf4j.bridge.SLF4JBridgeHandler"; - - @Override - public void reInitialize(final SpaceId spaceId, LoggerContext loggerContext, - final Properties properties, URL confFile) { - if (isAlreadyReInitialized(loggerContext)) { - return; - } - stopAndReset(loggerContext); - loggerContext.getTurboFilterList().remove(DefaultLogbackFilterGenerator.FILTER); - markAsReInitialized(loggerContext); - initProperties(loggerContext, properties); - if (isConsoleAppenderOpen(spaceId.getSpaceName(), properties)) { - final ConsoleAppender appender = consoleAppender(loggerContext, properties); - loggerContext.addTurboFilter(new TurboFilter() { - @Override - public FilterReply decide(Marker marker, Logger logger, Level level, String format, - Object[] params, Throwable t) { - if (!logger.isAttached(appender)) { - logger.detachAndStopAllAppenders(); - logger.setLevel(getConsoleLevel(spaceId.getSpaceName(), properties)); - logger.addAppender(appender); - } - return FilterReply.NEUTRAL; - } - }); - } else { - try { - new ContextInitializer(loggerContext).configureByResource(confFile); - } catch (JoranException e) { - throw new IllegalStateException("Logback loggerSpaceFactory re-build error", e); - } - } - - } - - private Level getConsoleLevel(String spaceId, Properties properties) { - SystemPropertiesGetter propertiesGetter = new SystemPropertiesGetter(properties); - String level = propertiesGetter.getProperty(SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL); - String defaultLevel = StringUtil.isBlank(level) ? "INFO" : level; - level = propertiesGetter.getProperty( - String.format(SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL, spaceId), defaultLevel); - return Level.toLevel(level, Level.INFO); - } - - private ConsoleAppender consoleAppender(LoggerContext loggerContext, Properties properties) { - SystemPropertiesGetter propertiesGetter = new SystemPropertiesGetter(properties); - ConsoleAppender appender = new ConsoleAppender(); - PatternLayoutEncoder encoder = new PatternLayoutEncoder(); - String logPattern = propertiesGetter.getProperty( - SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN, - SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN_DEFAULT); - encoder.setPattern(OptionHelper.substVars(logPattern, loggerContext)); - encoder.setContext(loggerContext); - encoder.start(); - appender.setEncoder(encoder); - appender.setName("CONSOLE"); - appender.start(); - return appender; - } - - private boolean isConsoleAppenderOpen(String spaceId, Properties properties) { - SystemPropertiesGetter propertiesGetter = new SystemPropertiesGetter(properties); - String value = propertiesGetter.getProperty(String.format( - SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, spaceId)); - if (StringUtil.isBlank(value)) { - return "true".equalsIgnoreCase(propertiesGetter - .getProperty(SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH)); - } else { - return "true".equalsIgnoreCase(value); - } - } - - private void markAsReInitialized(LoggerContext loggerContext) { - loggerContext.putObject(DefaultLogbackReInitializer.class.getCanonicalName(), new Object()); - } - - private boolean isAlreadyReInitialized(LoggerContext loggerContext) { - return loggerContext.getObject(DefaultLogbackReInitializer.class.getCanonicalName()) != null; - } - - private void stopAndReset(LoggerContext loggerContext) { - loggerContext.stop(); - loggerContext.reset(); - if (isBridgeHandlerAvailable()) { - addLevelChangePropagator(loggerContext); - } - } - - protected final boolean isBridgeHandlerAvailable() { - return ClassUtil.isPresent(BRIDGE_HANDLER, this.getClass().getClassLoader()); - } - - private void addLevelChangePropagator(LoggerContext loggerContext) { - LevelChangePropagator levelChangePropagator = new LevelChangePropagator(); - levelChangePropagator.setResetJUL(true); - levelChangePropagator.setContext(loggerContext); - loggerContext.addListener(levelChangePropagator); - } - - private void initProperties(LoggerContext loggerContext, Properties properties) { - for (Map.Entry entry : properties.entrySet()) { - String key = (String) entry.getKey(); - String originValue = (String) entry.getValue(); - String value = System.getProperty(key, originValue); - loggerContext.putProperty(key, value); - } - } -} \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/log4j2/SOFAConfigurationFactory.java b/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/log4j2/SOFAConfigurationFactory.java deleted file mode 100644 index d1acd1f92..000000000 --- a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/log4j2/SOFAConfigurationFactory.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alipay.sofa.boot.logging.log4j2; - -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.core.LoggerContext; -import org.apache.logging.log4j.core.config.Configuration; -import org.apache.logging.log4j.core.config.ConfigurationFactory; -import org.apache.logging.log4j.core.config.ConfigurationSource; -import org.apache.logging.log4j.core.config.DefaultConfiguration; -import org.apache.logging.log4j.core.config.Order; -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.xml.XmlConfiguration; - -/** - * https://logging.apache.org/log4j/log4j-2.6.1/manual/plugins.html - * Processed by {@literal org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor} - * - * @author qilong.zql - * @since 1.0.15 - */ -@Plugin(name = "SOFAConfigurationFactory", category = ConfigurationFactory.CATEGORY) -@Order(Integer.MAX_VALUE) -public class SOFAConfigurationFactory extends ConfigurationFactory { - - private final String[] TYPES = { "log4j2/log-conf.xml", "log4j2/log-conf-custom.xml" }; - - @Override - protected String[] getSupportedTypes() { - return TYPES; - } - - @Override - public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { - if (source != null && source != ConfigurationSource.NULL_SOURCE) { - return loggerContext.getExternalContext() != null ? new SOFAConfiguration() - : new XmlConfiguration(loggerContext, source); - } - return null; - } - - public static final class SOFAConfiguration extends DefaultConfiguration { - private SOFAConfiguration() { - this.isShutdownHookEnabled = false; - String levelName = System.getProperty(DefaultConfiguration.DEFAULT_LEVEL, - Level.INFO.name()); - Level level = Level.valueOf(levelName); - getRootLogger().setLevel(level != null ? level : Level.INFO); - } - } - -} \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/util/SystemPropertiesGetter.java b/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/util/SystemPropertiesGetter.java deleted file mode 100644 index 977688b83..000000000 --- a/sofa-boot-project/sofa-boot-core/log-sofa-boot/src/main/java/com/alipay/sofa/boot/logging/util/SystemPropertiesGetter.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alipay.sofa.boot.logging.util; - -import java.util.Properties; - -import com.alipay.sofa.common.utils.StringUtil; - -/** - * @author qilong.zql - * @since 1.0.15 - */ -public class SystemPropertiesGetter { - Properties properties; - - public SystemPropertiesGetter(Properties properties) { - this.properties = properties; - } - - public String getProperty(String key) { - String value = System.getProperty(key); - if (StringUtil.isBlank(value)) { - return (String) properties.get(key); - } - return value; - } - - public String getProperty(String key, String defaultValue) { - String value = getProperty(key); - return StringUtil.isBlank(value) ? defaultValue : value; - } -} \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/context/ApplicationContextRefreshedListener.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/context/ApplicationContextRefreshedListener.java index e229bc03d..ec480f981 100644 --- a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/context/ApplicationContextRefreshedListener.java +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/context/ApplicationContextRefreshedListener.java @@ -17,7 +17,6 @@ package com.alipay.sofa.rpc.boot.context; import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; @@ -29,19 +28,15 @@ * * @author leizhiyuan */ -public class ApplicationContextRefreshedListener implements ApplicationListener { +public class ApplicationContextRefreshedListener implements + ApplicationListener { @Override - public void onApplicationEvent(ApplicationEvent event) { - if (event instanceof ContextRefreshedEvent) { - - ApplicationContext applicationContext = ((ContextRefreshedEvent) event) - .getApplicationContext(); - //rpc 开始启动事件监听器 - applicationContext.publishEvent(new SofaBootRpcStartEvent(applicationContext)); - - //rpc 启动完毕事件监听器 - applicationContext.publishEvent(new SofaBootRpcStartAfterEvent(applicationContext)); - } + public void onApplicationEvent(ContextRefreshedEvent event) { + ApplicationContext applicationContext = event.getApplicationContext(); + //rpc 开始启动事件监听器 + applicationContext.publishEvent(new SofaBootRpcStartEvent(applicationContext)); + //rpc 启动完毕事件监听器 + applicationContext.publishEvent(new SofaBootRpcStartAfterEvent(applicationContext)); } } diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/context/ApplicationEnvironmentPreparedListener.java b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/context/ApplicationEnvironmentPreparedListener.java index 3cf00d8a4..486619338 100644 --- a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/context/ApplicationEnvironmentPreparedListener.java +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/java/com/alipay/sofa/rpc/boot/context/ApplicationEnvironmentPreparedListener.java @@ -17,7 +17,6 @@ package com.alipay.sofa.rpc.boot.context; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; -import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.core.env.Environment; @@ -28,16 +27,16 @@ /** * @author khotyn */ -public class ApplicationEnvironmentPreparedListener implements ApplicationListener { +public class ApplicationEnvironmentPreparedListener + implements + ApplicationListener { @Override - public void onApplicationEvent(ApplicationEvent event) { - if (event instanceof ApplicationEnvironmentPreparedEvent) { - Environment env = ((ApplicationEnvironmentPreparedEvent) event).getEnvironment(); - String defaultTracer = env.getProperty(SofaBootRpcProperties.PREFIX + ".defaultTracer"); - if (defaultTracer != null) { - RpcConfigs.putValue(RpcOptions.DEFAULT_TRACER, defaultTracer); - } + public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { + Environment env = event.getEnvironment(); + String defaultTracer = env.getProperty(SofaBootRpcProperties.PREFIX + ".defaultTracer"); + if (defaultTracer != null) { + RpcConfigs.putValue(RpcOptions.DEFAULT_TRACER, defaultTracer); } } } diff --git a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/resources/com/alipay/sofa/rpc/boot/log/log4j2/log-conf.xml b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/resources/com/alipay/sofa/rpc/boot/log/log4j2/log-conf.xml index a9716c7b0..d99bb6c76 100644 --- a/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/resources/com/alipay/sofa/rpc/boot/log/log4j2/log-conf.xml +++ b/sofa-boot-project/sofa-boot-core/rpc-sofa-boot/src/main/resources/com/alipay/sofa/rpc/boot/log/log4j2/log-conf.xml @@ -49,7 +49,7 @@ - + %d %-5p %-32t %c{2} - %m%n %throwable diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/log/RuntimeLoggerFactory.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/log/RuntimeLoggerFactory.java index 8c6f5f896..5ede4dc5b 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/log/RuntimeLoggerFactory.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/log/RuntimeLoggerFactory.java @@ -32,7 +32,7 @@ public class RuntimeLoggerFactory { /** * get Logger Object * - * @param name + * @param name name of the logger * @return Logger Object */ public static Logger getLogger(String name) { @@ -44,13 +44,13 @@ public static Logger getLogger(String name) { /** * get Logger Object - * @param klazz + * @param clazz the returned logger will be named after clazz * @return Logger Object */ - public static Logger getLogger(Class klazz) { - if (klazz == null) { + public static Logger getLogger(Class clazz) { + if (clazz == null) { return null; } - return getLogger(klazz.getCanonicalName()); + return getLogger(clazz.getCanonicalName()); } } diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/log/SofaLogger.java b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/log/SofaLogger.java index 74efd0ce9..cd5b59608 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/log/SofaLogger.java +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/java/com/alipay/sofa/runtime/log/SofaLogger.java @@ -30,33 +30,23 @@ public static boolean isDebugEnabled() { } public static void debug(String msg) { - if (DEFAULT_LOG.isDebugEnabled()) { - DEFAULT_LOG.debug(msg); - } + DEFAULT_LOG.debug(msg); } public static void debug(String format, Object arg) { - if (DEFAULT_LOG.isDebugEnabled()) { - DEFAULT_LOG.debug(format, arg); - } + DEFAULT_LOG.debug(format, arg); } public static void debug(String format, Object arg1, Object arg2) { - if (DEFAULT_LOG.isDebugEnabled()) { - DEFAULT_LOG.debug(format, arg1, arg2); - } + DEFAULT_LOG.debug(format, arg1, arg2); } public static void debug(String format, Object... arguments) { - if (DEFAULT_LOG.isDebugEnabled()) { - DEFAULT_LOG.debug(format, arguments); - } + DEFAULT_LOG.debug(format, arguments); } public static void debug(String msg, Throwable t) { - if (DEFAULT_LOG.isDebugEnabled()) { - DEFAULT_LOG.debug(msg, t); - } + DEFAULT_LOG.debug(msg, t); } public static boolean isInfoEnabled() { @@ -64,33 +54,23 @@ public static boolean isInfoEnabled() { } public static void info(String msg) { - if (DEFAULT_LOG.isInfoEnabled()) { - DEFAULT_LOG.info(msg); - } + DEFAULT_LOG.info(msg); } public static void info(String format, Object arg) { - if (DEFAULT_LOG.isInfoEnabled()) { - DEFAULT_LOG.info(format, arg); - } + DEFAULT_LOG.info(format, arg); } public static void info(String format, Object arg1, Object arg2) { - if (DEFAULT_LOG.isInfoEnabled()) { - DEFAULT_LOG.info(format, arg1, arg2); - } + DEFAULT_LOG.info(format, arg1, arg2); } public static void info(String format, Object... arguments) { - if (DEFAULT_LOG.isInfoEnabled()) { - DEFAULT_LOG.info(format, arguments); - } + DEFAULT_LOG.info(format, arguments); } public static void info(String msg, Throwable t) { - if (DEFAULT_LOG.isInfoEnabled()) { - DEFAULT_LOG.info(msg, t); - } + DEFAULT_LOG.info(msg, t); } public static boolean isWarnEnabled() { @@ -98,33 +78,23 @@ public static boolean isWarnEnabled() { } public static void warn(String msg) { - if (DEFAULT_LOG.isWarnEnabled()) { - DEFAULT_LOG.warn(msg); - } + DEFAULT_LOG.warn(msg); } public static void warn(String format, Object arg) { - if (DEFAULT_LOG.isWarnEnabled()) { - DEFAULT_LOG.warn(format, arg); - } + DEFAULT_LOG.warn(format, arg); } public static void warn(String format, Object... arguments) { - if (DEFAULT_LOG.isWarnEnabled()) { - DEFAULT_LOG.warn(format, arguments); - } + DEFAULT_LOG.warn(format, arguments); } public static void warn(String format, Object arg1, Object arg2) { - if (DEFAULT_LOG.isWarnEnabled()) { - DEFAULT_LOG.warn(format, arg1, arg2); - } + DEFAULT_LOG.warn(format, arg1, arg2); } public static void warn(String msg, Throwable t) { - if (DEFAULT_LOG.isWarnEnabled()) { - DEFAULT_LOG.warn(msg, t); - } + DEFAULT_LOG.warn(msg, t); } public static boolean isErrorEnabled() { @@ -132,32 +102,22 @@ public static boolean isErrorEnabled() { } public static void error(String msg) { - if (DEFAULT_LOG.isErrorEnabled()) { - DEFAULT_LOG.error(msg); - } + DEFAULT_LOG.error(msg); } public static void error(String format, Object arg) { - if (DEFAULT_LOG.isErrorEnabled()) { - DEFAULT_LOG.error(format, arg); - } + DEFAULT_LOG.error(format, arg); } public static void error(String format, Object arg1, Object arg2) { - if (DEFAULT_LOG.isErrorEnabled()) { - DEFAULT_LOG.error(format, arg1, arg2); - } + DEFAULT_LOG.error(format, arg1, arg2); } public static void error(String format, Object... arguments) { - if (DEFAULT_LOG.isErrorEnabled()) { - DEFAULT_LOG.error(format, arguments); - } + DEFAULT_LOG.error(format, arguments); } public static void error(String msg, Throwable t) { - if (DEFAULT_LOG.isErrorEnabled()) { - DEFAULT_LOG.error(msg, t); - } + DEFAULT_LOG.error(msg, t); } } diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/resources/com/alipay/sofa/runtime/log/log4j2/log-conf.xml b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/resources/com/alipay/sofa/runtime/log/log4j2/log-conf.xml index 8d817ffdc..825afd5e7 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/resources/com/alipay/sofa/runtime/log/log4j2/log-conf.xml +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/resources/com/alipay/sofa/runtime/log/log4j2/log-conf.xml @@ -28,7 +28,7 @@ filePattern="${RUNTIME_LOG_PATH}/sofa-runtime/common-error.log.%d{yyyy-MM-dd}"> - %d %-5p %-32t - %m%n %throwable + %d %-5p %-32t - %m %throwable%n @@ -39,7 +39,7 @@ filePattern="${RUNTIME_LOG_PATH}/sofa-runtime/sofa-default.log.%d{yyyy-MM-dd}"> - %d %-5p %-32t - %m%n %throwable + %d %-5p %-32t - %m %throwable%n diff --git a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/resources/com/alipay/sofa/runtime/log/logback/log-conf.xml b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/resources/com/alipay/sofa/runtime/log/logback/log-conf.xml index a9c3d1086..3b7ced68a 100644 --- a/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/resources/com/alipay/sofa/runtime/log/logback/log-conf.xml +++ b/sofa-boot-project/sofa-boot-core/runtime-sofa-boot/src/main/resources/com/alipay/sofa/runtime/log/logback/log-conf.xml @@ -21,8 +21,6 @@ true error - ACCEPT - DENY ${logging.path}/sofa-runtime/common-error.log @@ -39,8 +37,6 @@ true ${logging.level.com.alipay.sofa.runtime} - ACCEPT - DENY ${logging.path}/sofa-runtime/sofa-default.log @@ -63,4 +59,4 @@ - \ No newline at end of file + diff --git a/sofa-boot-project/sofa-boot-core/tracer-sofa-boot/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java b/sofa-boot-project/sofa-boot-core/tracer-sofa-boot/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java index 054814569..d51fe8b60 100644 --- a/sofa-boot-project/sofa-boot-core/tracer-sofa-boot/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java +++ b/sofa-boot-project/sofa-boot-core/tracer-sofa-boot/src/main/java/com/alipay/sofa/tracer/boot/listener/SofaTracerConfigurationListener.java @@ -126,4 +126,4 @@ private boolean isSpringCloud() { return ClassUtils.isPresent("org.springframework.cloud.bootstrap.BootstrapConfiguration", null); } -} \ No newline at end of file +} diff --git a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/pom.xml b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/pom.xml index cac35805f..a6864799a 100644 --- a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/pom.xml +++ b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/pom.xml @@ -14,7 +14,7 @@ ${basedir}/../../.. 1.1.7 - 2.8 + 2.13.3 diff --git a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.Log4j2FilterGenerator b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.Log4j2FilterGenerator deleted file mode 100644 index e4e3db873..000000000 --- a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.Log4j2FilterGenerator +++ /dev/null @@ -1 +0,0 @@ -com.alipay.sofa.boot.logging.filter.DefaultLog4j2FilterGenerator \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.Log4j2ReInitializer b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.Log4j2ReInitializer deleted file mode 100644 index 977895de5..000000000 --- a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.Log4j2ReInitializer +++ /dev/null @@ -1 +0,0 @@ -com.alipay.sofa.boot.logging.initializer.DefaultLog4j2ReInitializer \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.LogbackFilterGenerator b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.LogbackFilterGenerator deleted file mode 100644 index d0cce3ee9..000000000 --- a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.LogbackFilterGenerator +++ /dev/null @@ -1 +0,0 @@ -com.alipay.sofa.boot.logging.filter.DefaultLogbackFilterGenerator \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.LogbackReInitializer b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.LogbackReInitializer deleted file mode 100644 index a244fdfa4..000000000 --- a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.LogbackReInitializer +++ /dev/null @@ -1 +0,0 @@ -com.alipay.sofa.boot.logging.initializer.DefaultLogbackReInitializer \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.ReInitializeChecker b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.ReInitializeChecker deleted file mode 100644 index 9be550058..000000000 --- a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/services/com.alipay.sofa.common.log.spi.ReInitializeChecker +++ /dev/null @@ -1 +0,0 @@ -com.alipay.sofa.boot.logging.DefaultReInitializerChecker \ No newline at end of file diff --git a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/spring.factories b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/spring.factories index b8263d9ba..1c35d1cc0 100644 --- a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/spring.factories +++ b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/main/resources/META-INF/spring.factories @@ -1 +1 @@ -org.springframework.context.ApplicationListener=com.alipay.sofa.boot.logging.CommonLoggingApplicationListener \ No newline at end of file +org.springframework.context.ApplicationListener=com.alipay.sofa.boot.logging.LogEnvironmentPreparingListener diff --git a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/BaseLogIntegrationTest.java b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/BaseLogIntegrationTest.java index 1e77fccce..bdabd7856 100644 --- a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/BaseLogIntegrationTest.java +++ b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/BaseLogIntegrationTest.java @@ -17,17 +17,14 @@ package com.alipay.sofa.common.boot.logging.test; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.PrintStream; import java.lang.reflect.Field; import java.util.Map; +import com.alipay.sofa.common.log.env.LogEnvUtils; import org.junit.After; -import org.junit.Before; import org.slf4j.Logger; -import com.alipay.sofa.boot.logging.CommonLoggingApplicationListener; -import com.alipay.sofa.common.log.LoggerSpaceManager; import com.alipay.sofa.common.log.MultiAppLoggerSpaceManager; import com.alipay.sofa.common.log.SpaceId; @@ -44,21 +41,23 @@ public abstract class BaseLogIntegrationTest { protected ByteArrayOutputStream errContent; protected final PrintStream originalOut = System.out; protected final PrintStream originalErr = System.err; + protected static Field globalSystemPropertiesField; static { try { Field spacesMapField = MultiAppLoggerSpaceManager.class.getDeclaredField("SPACES_MAP"); spacesMapField.setAccessible(true); SPACES_MAP = (Map) spacesMapField.get(MultiAppLoggerSpaceManager.class); + + globalSystemPropertiesField = LogEnvUtils.class + .getDeclaredField("globalSystemProperties"); + globalSystemPropertiesField.setAccessible(true); } catch (Throwable throwable) { // ignore } } - @Before public void setUpStreams() { - logger = LoggerSpaceManager.getLoggerBySpace( - LogbackIntegrationTest.class.getCanonicalName(), TEST_SPACE); outContent = new ByteArrayOutputStream(); errContent = new ByteArrayOutputStream(); System.setOut(new PrintStream(outContent)); @@ -66,12 +65,12 @@ public void setUpStreams() { } @After - public void restoreStreams() throws IOException { + public void restoreStreams() throws Exception { System.setOut(originalOut); System.setErr(originalErr); outContent.close(); errContent.close(); SPACES_MAP.remove(new SpaceId(TEST_SPACE)); - new CommonLoggingApplicationListener().setReInitialize(false); + globalSystemPropertiesField.set(null, null); } -} \ No newline at end of file +} diff --git a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/Log4j2IntegrationTest.java b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/Log4j2IntegrationTest.java index 0dbbeaa08..7dd788a02 100644 --- a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/Log4j2IntegrationTest.java +++ b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/Log4j2IntegrationTest.java @@ -16,23 +16,21 @@ */ package com.alipay.sofa.common.boot.logging.test; -import java.io.IOException; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; +import com.alipay.sofa.common.log.LoggerSpaceManager; +import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.core.appender.AbstractManager; -import org.apache.logging.log4j.core.config.DefaultConfiguration; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.slf4j.Logger; import org.springframework.boot.SpringApplication; import com.alipay.sofa.common.log.Constants; -import com.alipay.sofa.common.log.LoggerSpaceManager; -import com.alipay.sofa.common.log.SpaceId; -import com.alipay.sofa.common.log.env.LogEnvUtils; /** * @author qilong.zql @@ -53,64 +51,59 @@ public class Log4j2IntegrationTest extends BaseLogIntegrationTest { } @Before - @Override - public void setUpStreams() { + public void before() { MANAGER_MAP.clear(); System.setProperty(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true"); - super.setUpStreams(); + setUpStreams(); } @After @Override - public void restoreStreams() throws IOException { + public void restoreStreams() throws Exception { super.restoreStreams(); System.getProperties().remove(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY); } + protected Logger getLogger() { + return LoggerSpaceManager.getLoggerBySpace(Log4j2IntegrationTest.class.getCanonicalName(), + TEST_SPACE); + } + /** * test log4j2 info log to console */ @Test public void testLog4j2InfoToConsole() { - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put( String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE), "true"); SpringApplication springApplication = new SpringApplication( LogbackIntegrationTest.EmptyConfig.class); springApplication.setDefaultProperties(properties); - springApplication.run(new String[] {}); + springApplication.run(); + logger = getLogger(); logger.info("space console"); Assert.assertTrue(outContent.toString().contains("space console")); - LogEnvUtils.processGlobalSystemLogProperties().remove( - String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE)); + System.clearProperty(String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, + TEST_SPACE)); } - /** - * test log4j2 root level config - */ @Test - public void testRootLevelConfig() { - SPACES_MAP.remove(new SpaceId(TEST_SPACE)); - System.setProperty(DefaultConfiguration.DEFAULT_LEVEL, "ERROR"); - - logger = LoggerSpaceManager.getLoggerBySpace( - LogbackIntegrationTest.class.getCanonicalName(), TEST_SPACE); - - Map properties = new HashMap(); - properties.put( - String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE), "true"); - SpringApplication springApplication = new SpringApplication( - LogbackIntegrationTest.EmptyConfig.class); - springApplication.setDefaultProperties(properties); - springApplication.run(new String[] {}); - - logger.info("space info console"); - logger.error("space error console"); - Assert.assertFalse(outContent.toString().contains("space info console")); - Assert.assertTrue(outContent.toString().contains("space error console")); - - System.getProperties().remove(DefaultConfiguration.DEFAULT_LEVEL); - LogEnvUtils.processGlobalSystemLogProperties().remove( - String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE)); + public void testThreadContextConfiguration() { + try { + System.setProperty(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true"); + ThreadContext.put("testKey", "testValue"); + ThreadContext.put("logging.path", "anyPath"); + Map properties = new HashMap<>(); + SpringApplication springApplication = new SpringApplication( + LogbackIntegrationTest.EmptyConfig.class); + springApplication.setDefaultProperties(properties); + springApplication.run(); + logger = getLogger(); + Assert.assertEquals("testValue", ThreadContext.get("testKey")); + Assert.assertEquals(Constants.LOGGING_PATH_DEFAULT, ThreadContext.get("logging.path")); + } finally { + System.getProperties().remove(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY); + } } -} \ No newline at end of file +} diff --git a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/LogbackIntegrationTest.java b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/LogbackIntegrationTest.java index 2d4d02b8a..0f9832cbd 100644 --- a/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/LogbackIntegrationTest.java +++ b/sofa-boot-project/sofa-boot-starters/log-sofa-boot-starter/src/test/java/com/alipay/sofa/common/boot/logging/test/LogbackIntegrationTest.java @@ -22,10 +22,12 @@ import java.util.List; import java.util.Map; +import com.alipay.sofa.common.log.LoggerSpaceManager; import org.apache.commons.io.FileUtils; -import org.apache.logging.log4j.ThreadContext; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.slf4j.Logger; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.ConfigurableApplicationContext; @@ -33,8 +35,6 @@ import org.springframework.core.env.Environment; import com.alipay.sofa.common.log.Constants; -import com.alipay.sofa.common.log.LoggerSpaceManager; -import com.alipay.sofa.common.log.SpaceId; import com.alipay.sofa.common.log.env.LogEnvUtils; import com.alipay.sofa.common.utils.ReportUtil; import com.alipay.sofa.common.utils.StringUtil; @@ -44,12 +44,26 @@ * @since 1.0.15 */ public class LogbackIntegrationTest extends BaseLogIntegrationTest { + @Before + public void before() { + setUpStreams(); + //disable logback + System.setProperty(Constants.LOG4J2_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true"); + //disable log4j + System.setProperty(Constants.LOG4J_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true"); + } + + protected Logger getLogger() { + return LoggerSpaceManager.getLoggerBySpace(LogbackIntegrationTest.class.getCanonicalName(), + TEST_SPACE); + } @Test public void testDefaultLevel() throws IOException { SpringApplication springApplication = new SpringApplication(EmptyConfig.class); - ConfigurableApplicationContext applicationContext = springApplication.run(new String[] {}); + ConfigurableApplicationContext applicationContext = springApplication.run(); Environment environment = applicationContext.getEnvironment(); + logger = getLogger(); File logFile = getLogbackDefaultFile(environment); FileUtils.write(logFile, StringUtil.EMPTY_STRING, environment.getProperty(Constants.LOG_ENCODING_PROP_KEY)); @@ -63,16 +77,17 @@ public void testDefaultLevel() throws IOException { /** * test logging.level.com.* config - * @throws IOException + * @throws IOException not handling */ @Test public void testWildLogLevel() throws IOException { - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put(Constants.LOG_LEVEL_PREFIX + "test.*", "debug"); SpringApplication springApplication = new SpringApplication(EmptyConfig.class); springApplication.setDefaultProperties(properties); - ConfigurableApplicationContext applicationContext = springApplication.run(new String[] {}); + ConfigurableApplicationContext applicationContext = springApplication.run(); Environment environment = applicationContext.getEnvironment(); + logger = getLogger(); File logFile = getLogbackDefaultFile(environment); FileUtils.write(logFile, StringUtil.EMPTY_STRING, environment.getProperty(Constants.LOG_ENCODING_PROP_KEY)); @@ -89,16 +104,17 @@ public void testWildLogLevel() throws IOException { /** * test logging.level.{space id} config - * @throws IOException + * @throws IOException not handling */ @Test public void testSpecifyLogLevel() throws IOException { - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put(Constants.LOG_LEVEL_PREFIX + "test.space", "debug"); SpringApplication springApplication = new SpringApplication(EmptyConfig.class); springApplication.setDefaultProperties(properties); - ConfigurableApplicationContext applicationContext = springApplication.run(new String[] {}); + ConfigurableApplicationContext applicationContext = springApplication.run(); Environment environment = applicationContext.getEnvironment(); + logger = getLogger(); File logFile = getLogbackDefaultFile(environment); FileUtils.write(logFile, StringUtil.EMPTY_STRING, environment.getProperty(Constants.LOG_ENCODING_PROP_KEY)); @@ -109,22 +125,22 @@ public void testSpecifyLogLevel() throws IOException { Assert.assertEquals(2, contents.size()); Assert.assertTrue(contents.get(0).contains("info level")); Assert.assertTrue(contents.get(1).contains("debug level")); - LogEnvUtils.processGlobalSystemLogProperties().remove( - Constants.LOG_LEVEL_PREFIX + "test.space"); + System.clearProperty(Constants.LOG_LEVEL_PREFIX + "test.space"); } /** * test logging.config.{space id} config - * @throws IOException + * @throws IOException not handling */ @Test public void testLogConfig() throws IOException { - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put(Constants.LOG_CONFIG_PREFIX + TEST_SPACE, "logback-test-conf.xml"); SpringApplication springApplication = new SpringApplication(EmptyConfig.class); springApplication.setDefaultProperties(properties); - ConfigurableApplicationContext applicationContext = springApplication.run(new String[] {}); + ConfigurableApplicationContext applicationContext = springApplication.run(); Environment environment = applicationContext.getEnvironment(); + logger = getLogger(); File logFile = getLogbackDefaultFile(environment); FileUtils.write(logFile, StringUtil.EMPTY_STRING, environment.getProperty(Constants.LOG_ENCODING_PROP_KEY)); @@ -133,8 +149,7 @@ public void testLogConfig() throws IOException { environment.getProperty(Constants.LOG_ENCODING_PROP_KEY)); Assert.assertEquals(1, contents.size()); Assert.assertTrue(contents.get(0).contains("logback-test-conf")); - LogEnvUtils.processGlobalSystemLogProperties().remove( - Constants.LOG_CONFIG_PREFIX + TEST_SPACE); + System.clearProperty(Constants.LOG_CONFIG_PREFIX + TEST_SPACE); } /** @@ -154,18 +169,19 @@ public void testInternalLogLevel() { */ @Test public void testSpaceConsoleConfig() { - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put( String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE), "true"); SpringApplication springApplication = new SpringApplication(EmptyConfig.class); springApplication.setDefaultProperties(properties); - springApplication.run(new String[] {}); + springApplication.run(); + logger = getLogger(); logger.info("space console"); logger.debug("space console debug"); Assert.assertTrue(outContent.toString().contains("space console")); Assert.assertFalse(outContent.toString().contains("space console debug")); - LogEnvUtils.processGlobalSystemLogProperties().remove( - String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE)); + System.clearProperty(String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, + TEST_SPACE)); } /** @@ -173,22 +189,23 @@ public void testSpaceConsoleConfig() { */ @Test public void testSpaceConsoleLevelConfig() { - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put( String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE), "true"); properties.put( String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL, TEST_SPACE), "debug"); SpringApplication springApplication = new SpringApplication(EmptyConfig.class); springApplication.setDefaultProperties(properties); - springApplication.run(new String[] {}); + springApplication.run(); + logger = getLogger(); logger.info("space console"); logger.debug("space console debug"); Assert.assertTrue(outContent.toString().contains("space console")); Assert.assertTrue(outContent.toString().contains("space console debug")); - LogEnvUtils.processGlobalSystemLogProperties().remove( - String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE)); - LogEnvUtils.processGlobalSystemLogProperties().remove( - String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL, TEST_SPACE)); + System.clearProperty(String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, + TEST_SPACE)); + System.clearProperty(String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL, + TEST_SPACE)); } /** @@ -196,17 +213,17 @@ public void testSpaceConsoleLevelConfig() { */ @Test public void testGlobalConsoleConfig() { - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH, "true"); SpringApplication springApplication = new SpringApplication(EmptyConfig.class); springApplication.setDefaultProperties(properties); - springApplication.run(new String[] {}); + springApplication.run(); + logger = getLogger(); logger.info("global space console"); logger.debug("global space console debug"); Assert.assertTrue(outContent.toString().contains("global space console")); Assert.assertFalse(outContent.toString().contains("global space console debug")); - LogEnvUtils.processGlobalSystemLogProperties().remove( - Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH); + System.clearProperty(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH); } /** @@ -214,29 +231,28 @@ public void testGlobalConsoleConfig() { */ @Test public void testGlobalConsoleLevelConfig() { - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH, "true"); properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL, "debug"); SpringApplication springApplication = new SpringApplication(EmptyConfig.class); springApplication.setDefaultProperties(properties); - springApplication.run(new String[] {}); + springApplication.run(); + logger = getLogger(); logger.info("global space console"); logger.debug("global space console debug"); Assert.assertTrue(outContent.toString().contains("global space console")); Assert.assertTrue(outContent.toString().contains("global space console debug")); - LogEnvUtils.processGlobalSystemLogProperties().remove( - Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH); - LogEnvUtils.processGlobalSystemLogProperties().remove( - Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL); + System.clearProperty(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH); + System.clearProperty(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL); } /** * test space config override global config - * @throws IOException + * @throws IOException not handling */ @Test public void testSpaceOverrideGlobalConfig() throws IOException { - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH, "true"); properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL, "debug"); properties @@ -247,9 +263,9 @@ public void testSpaceOverrideGlobalConfig() throws IOException { SpringApplication springApplication = new SpringApplication(EmptyConfig.class); springApplication.setDefaultProperties(properties); - springApplication.run(new String[] {}); - ConfigurableApplicationContext applicationContext = springApplication.run(new String[] {}); + ConfigurableApplicationContext applicationContext = springApplication.run(); Environment environment = applicationContext.getEnvironment(); + logger = getLogger(); File logFile = getLogbackDefaultFile(environment); FileUtils.write(logFile, StringUtil.EMPTY_STRING, environment.getProperty(Constants.LOG_ENCODING_PROP_KEY)); @@ -261,14 +277,12 @@ public void testSpaceOverrideGlobalConfig() throws IOException { Assert.assertTrue(contents.get(0).contains("info level")); Assert.assertFalse(outContent.toString().contains("info level")); Assert.assertFalse(outContent.toString().contains("debug level")); - LogEnvUtils.processGlobalSystemLogProperties().remove( - Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH); - LogEnvUtils.processGlobalSystemLogProperties().remove( - Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL); - LogEnvUtils.processGlobalSystemLogProperties().remove( - String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, TEST_SPACE)); - LogEnvUtils.processGlobalSystemLogProperties().remove( - String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL, TEST_SPACE)); + System.clearProperty(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH); + System.clearProperty(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_LEVEL); + System.clearProperty(String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_SWITCH, + TEST_SPACE)); + System.clearProperty(String.format(Constants.SOFA_MIDDLEWARE_SINGLE_LOG_CONSOLE_LEVEL, + TEST_SPACE)); } /** @@ -276,41 +290,19 @@ public void testSpaceOverrideGlobalConfig() throws IOException { */ @Test public void testLogbackLogConsolePattern() { - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH, "true"); properties.put(Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN, "logback-test-console-pattern" + Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN_DEFAULT); SpringApplication springApplication = new SpringApplication(EmptyConfig.class); springApplication.setDefaultProperties(properties); - springApplication.run(new String[] {}); + springApplication.run(); + logger = getLogger(); logger.info("global space console"); Assert.assertTrue(outContent.toString().contains("logback-test-console-pattern")); - LogEnvUtils.processGlobalSystemLogProperties().remove( - Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH); - LogEnvUtils.processGlobalSystemLogProperties().remove( - Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN); - } - - @Test - public void testThreadContextConfiguration() { - try { - System.setProperty(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true"); - SPACES_MAP.remove(new SpaceId(TEST_SPACE)); - LoggerSpaceManager.getLoggerBySpace(LogbackIntegrationTest.class.getCanonicalName(), - TEST_SPACE); - ThreadContext.put("testKey", "testValue"); - ThreadContext.put("logging.path", "anyPath"); - Map properties = new HashMap(); - SpringApplication springApplication = new SpringApplication(EmptyConfig.class); - springApplication.setDefaultProperties(properties); - springApplication.run(new String[] {}); - Assert.assertTrue("testValue".equals(ThreadContext.get("testKey"))); - Assert.assertTrue(Constants.LOGGING_PATH_DEFAULT.equals(ThreadContext - .get("logging.path"))); - } finally { - System.getProperties().remove(Constants.LOGBACK_MIDDLEWARE_LOG_DISABLE_PROP_KEY); - } + System.clearProperty(Constants.SOFA_MIDDLEWARE_ALL_LOG_CONSOLE_SWITCH); + System.clearProperty(Constants.SOFA_MIDDLEWARE_LOG_CONSOLE_LOGBACK_PATTERN); } /** @@ -318,17 +310,16 @@ public void testThreadContextConfiguration() { */ @Test public void testDisableMiddleLog() { - Map properties = new HashMap(); + Map properties = new HashMap<>(); properties.put(Constants.SOFA_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true"); SpringApplication springApplication = new SpringApplication(EmptyConfig.class); springApplication.setDefaultProperties(properties); - springApplication.run(new String[] {}); + springApplication.run(); logger.info("global space console"); logger.debug("global space console debug"); Assert.assertFalse(outContent.toString().contains("global space console")); Assert.assertFalse(outContent.toString().contains("global space console debug")); - LogEnvUtils.processGlobalSystemLogProperties().remove( - Constants.SOFA_MIDDLEWARE_LOG_DISABLE_PROP_KEY); + System.clearProperty(Constants.SOFA_MIDDLEWARE_LOG_DISABLE_PROP_KEY); } protected File getLogbackDefaultFile(Environment environment) { @@ -344,4 +335,4 @@ protected File getLogbackDefaultFile(Environment environment) { @Configuration static class EmptyConfig { } -} \ No newline at end of file +} diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/test/log/AllInfraLoggerFactoryTest.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/test/log/AllInfraLoggerFactoryTest.java index 8eb694f97..1eaa46bf2 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/test/log/AllInfraLoggerFactoryTest.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/test/log/AllInfraLoggerFactoryTest.java @@ -33,7 +33,6 @@ public class AllInfraLoggerFactoryTest extends BaseLogTest { @Before public void before() throws Exception { - super.before(); // disable all log space System.setProperty(Constants.SOFA_MIDDLEWARE_LOG_DISABLE_PROP_KEY, "true"); } diff --git a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/test/log/base/BaseLogTest.java b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/test/log/base/BaseLogTest.java index acbd63da9..838a61788 100644 --- a/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/test/log/base/BaseLogTest.java +++ b/sofa-boot-project/sofa-boot/src/test/java/com/alipay/sofa/boot/test/log/base/BaseLogTest.java @@ -20,10 +20,10 @@ import java.util.Map; import com.alipay.sofa.boot.log.InfraLoggerFactory; -import com.alipay.sofa.boot.logging.CommonLoggingApplicationListener; import com.alipay.sofa.common.log.Constants; import com.alipay.sofa.common.log.MultiAppLoggerSpaceManager; import com.alipay.sofa.common.log.SpaceId; +import com.alipay.sofa.common.log.SpaceInfo; import com.alipay.sofa.common.log.env.LogEnvUtils; /** @@ -33,18 +33,18 @@ */ public abstract class BaseLogTest { - public static final String restLogLevel = Constants.LOG_LEVEL_PREFIX - + InfraLoggerFactory.INFRASTRUCTURE_LOG_SPACE; - private static Map SPACES_MAP; - private static Field globalSystemProperties; + public static final String restLogLevel = Constants.LOG_LEVEL_PREFIX + + InfraLoggerFactory.INFRASTRUCTURE_LOG_SPACE; + private static Map SPACES_MAP; static { try { Field field = MultiAppLoggerSpaceManager.class.getDeclaredField("SPACES_MAP"); field.setAccessible(true); - SPACES_MAP = (Map) field.get(null); + SPACES_MAP = (Map) field.get(null); - globalSystemProperties = LogEnvUtils.class.getDeclaredField("globalSystemProperties"); + Field globalSystemProperties = LogEnvUtils.class + .getDeclaredField("globalSystemProperties"); globalSystemProperties.setAccessible(true); } catch (Throwable throwable) { // ignore @@ -53,7 +53,6 @@ public abstract class BaseLogTest { public void before() throws Exception { SPACES_MAP.remove(new SpaceId(InfraLoggerFactory.INFRASTRUCTURE_LOG_SPACE)); - new CommonLoggingApplicationListener().setReInitialize(true); } public void after() throws Exception { diff --git a/sofa-boot-project/sofaboot-dependencies/pom.xml b/sofa-boot-project/sofaboot-dependencies/pom.xml index 7e5d8557d..663cc516f 100644 --- a/sofa-boot-project/sofaboot-dependencies/pom.xml +++ b/sofa-boot-project/sofaboot-dependencies/pom.xml @@ -35,7 +35,7 @@ 5.7.6 - 1.0.22 + 1.3.0-SNAPSHOT 1.5.6 3.3.10