|
| 1 | +package com.ctrip.framework.apollo.log4j2; |
| 2 | + |
| 3 | +import com.ctrip.framework.apollo.ConfigFile; |
| 4 | +import com.ctrip.framework.apollo.ConfigService; |
| 5 | +import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; |
| 6 | +import java.io.ByteArrayInputStream; |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.URI; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import org.apache.logging.log4j.core.LoggerContext; |
| 11 | +import org.apache.logging.log4j.core.config.Configuration; |
| 12 | +import org.apache.logging.log4j.core.config.ConfigurationException; |
| 13 | +import org.apache.logging.log4j.core.config.ConfigurationFactory; |
| 14 | +import org.apache.logging.log4j.core.config.ConfigurationSource; |
| 15 | +import org.apache.logging.log4j.core.config.Order; |
| 16 | +import org.apache.logging.log4j.core.config.plugins.Plugin; |
| 17 | +import org.apache.logging.log4j.core.config.xml.XmlConfiguration; |
| 18 | +import org.apache.logging.log4j.util.Strings; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author nisiyong |
| 22 | + */ |
| 23 | +@Plugin(name = "ApolloLog4j2ConfigurationFactory", category = ConfigurationFactory.CATEGORY) |
| 24 | +@Order(50) |
| 25 | +public class ApolloLog4j2ConfigurationFactory extends ConfigurationFactory { |
| 26 | + |
| 27 | + private final boolean isActive; |
| 28 | + |
| 29 | + public ApolloLog4j2ConfigurationFactory() { |
| 30 | + String enabled = System.getProperty("apollo.log4j2.enabled"); |
| 31 | + isActive = Boolean.parseBoolean(enabled); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + protected boolean isActive() { |
| 36 | + return this.isActive; |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected String[] getSupportedTypes() { |
| 41 | + return new String[]{"*"}; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public Configuration getConfiguration(LoggerContext loggerContext, String name, URI configLocation) { |
| 46 | + return getConfiguration(loggerContext, null); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource configurationSource) { |
| 51 | + if (!isActive) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + ConfigFile configFile = ConfigService.getConfigFile("log4j2", ConfigFileFormat.XML); |
| 56 | + |
| 57 | + if (configFile == null || Strings.isBlank(configFile.getContent())) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + byte[] bytes = configFile.getContent().getBytes(StandardCharsets.UTF_8); |
| 62 | + try { |
| 63 | + configurationSource = new ConfigurationSource(new ByteArrayInputStream(bytes)); |
| 64 | + } catch (IOException e) { |
| 65 | + throw new ConfigurationException("Unable to initialize ConfigurationSource from Apollo", e); |
| 66 | + } |
| 67 | + |
| 68 | + // TODO add ConfigFileChangeListener, dynamic load log4j2.xml in runtime |
| 69 | + LOGGER.debug("Initializing configuration ApolloLog4j2Configuration[namespace=log4j2.xml]\n{}", configFile.getContent()); |
| 70 | + return new XmlConfiguration(loggerContext, configurationSource); |
| 71 | + } |
| 72 | +} |
0 commit comments