Skip to content

Commit

Permalink
Merge pull request alibaba#996 from Summerslyb/fix-log4j2
Browse files Browse the repository at this point in the history
[ISSUE alibaba#827] Fix Nacos Naming's log settings affect the root log
  • Loading branch information
hxy1991 authored Apr 19, 2019
2 parents 96e8e14 + ce0b863 commit 55242c8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,19 @@
package com.alibaba.nacos.client.logging.log4j2;

import com.alibaba.nacos.client.logging.AbstractNacosLogging;
import com.alibaba.nacos.client.utils.StringUtils;
import com.alibaba.nacos.common.util.ClassUtils;
import com.alibaba.nacos.common.util.ResourceUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.AbstractConfiguration;
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.composite.CompositeConfiguration;
import org.apache.logging.log4j.core.lookup.Interpolator;
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
import org.apache.logging.log4j.util.PropertiesUtil;
import org.apache.logging.log4j.core.config.LoggerConfig;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.apache.logging.log4j.core.config.ConfigurationFactory.CONFIGURATION_FILE_PROPERTY;
import java.util.Map;

/**
* Support for Log4j version 2.7 or higher
Expand All @@ -54,50 +42,32 @@ public class Log4J2NacosLogging extends AbstractNacosLogging {

private static final String FILE_PROTOCOL = "file";

private static final String YAML_PARSER_CLASS_NAME = "com.fasterxml.jackson.dataformat.yaml.YAMLParser";

private static final String JSON_PARSER_CLASS_NAME = "com.fasterxml.jackson.databind.ObjectMapper";
private static final String NACOS_LOGGER_PREFIX = "com.alibaba.nacos";

private final StrSubstitutor strSubstitutor = new StrSubstitutor(new Interpolator());

private Set<String> locationList = new HashSet<String>();

public Log4J2NacosLogging() {
String location = getLocation(NACOS_LOG4J2_LOCATION);
if (!StringUtils.isBlank(location)) {
locationList.add(location);
}
}
private String location = getLocation(NACOS_LOG4J2_LOCATION);

@Override
public void loadConfiguration() {
if (locationList.isEmpty()) {
return;
}
final LoggerContext loggerContext = (LoggerContext)LogManager.getContext(false);
final Configuration contextConfiguration = loggerContext.getConfiguration();

List<String> configList = findConfig(getCurrentlySupportedConfigLocations());
if (configList != null) {
locationList.addAll(configList);
}

final List<AbstractConfiguration> configurations = new ArrayList<AbstractConfiguration>();
// load and start nacos configuration
Configuration configuration = loadConfiguration(loggerContext, location);
configuration.start();

LoggerContext loggerContext = (LoggerContext)LogManager.getContext(false);
for (String location : locationList) {
try {
Configuration configuration = loadConfiguration(loggerContext, location);
if (configuration instanceof AbstractConfiguration) {
configurations.add((AbstractConfiguration)configuration);
}
} catch (Exception e) {
throw new IllegalStateException(
"Could not initialize Log4J2 Nacos logging from " + location, e);
// append loggers and appenders to contextConfiguration
Map<String, Appender> appenders = configuration.getAppenders();
for (Appender appender: appenders.values()) {
contextConfiguration.addAppender(appender);
}
Map<String, LoggerConfig> loggers = configuration.getLoggers();
for (String name : loggers.keySet()) {
if (name.startsWith(NACOS_LOGGER_PREFIX)) {
contextConfiguration.addLogger(name, loggers.get(name));
}
}

// since log4j 2.6
CompositeConfiguration compositeConfiguration = new CompositeConfiguration(configurations);
loggerContext.start(compositeConfiguration);
loggerContext.updateLoggers();
}

private Configuration loadConfiguration(LoggerContext loggerContext, String location) {
Expand All @@ -119,43 +89,4 @@ private ConfigurationSource getConfigurationSource(URL url) throws IOException {
}
return new ConfigurationSource(stream, url);
}

private String[] getCurrentlySupportedConfigLocations() {
List<String> supportedConfigLocations = new ArrayList<String>();

if (ClassUtils.isPresent(YAML_PARSER_CLASS_NAME)) {
Collections.addAll(supportedConfigLocations, "log4j2.yaml", "log4j2.yml", "log4j2-test.yaml",
"log4j2-test.yml");
}

if (ClassUtils.isPresent(JSON_PARSER_CLASS_NAME)) {
Collections.addAll(supportedConfigLocations, "log4j2.json", "log4j2.jsn", "log4j2-test.json",
"log4j2-test.jsn");
}

supportedConfigLocations.add("log4j2.xml");
supportedConfigLocations.add("log4j2-test.xml");

return supportedConfigLocations.toArray(new String[supportedConfigLocations.size()]);
}

private List<String> findConfig(String[] locations) {
final String configLocationStr = this.strSubstitutor.replace(PropertiesUtil.getProperties()
.getStringProperty(CONFIGURATION_FILE_PROPERTY));

if (configLocationStr != null) {
return Arrays.asList(configLocationStr.split(","));
}

for (String location : locations) {
ClassLoader defaultClassLoader = ClassUtils.getDefaultClassLoader();
if (defaultClassLoader != null && defaultClassLoader.getResource(location) != null) {
List<String> list = new ArrayList<String>();
list.add("classpath:" + location);
return list;
}
}
return null;
}

}
3 changes: 3 additions & 0 deletions client/src/main/resources/nacos-log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@
additivity="false">
<AppenderRef ref="NAMING_LOG_FILE"/>
</Logger>
<Root level="INFO">
<AppenderRef ref="CONFIG_LOG_FILE"/>
</Root>
</Loggers>
</Configuration>
60 changes: 0 additions & 60 deletions common/src/main/java/com/alibaba/nacos/common/util/ClassUtils.java

This file was deleted.

0 comments on commit 55242c8

Please sign in to comment.