|
| 1 | +/* |
| 2 | + * Copyright 2022 Apollo Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | + |
| 18 | +package com.ctrip.framework.apollo.plugin.log4j2; |
| 19 | + |
| 20 | +import com.ctrip.framework.apollo.ConfigFile; |
| 21 | +import com.ctrip.framework.apollo.ConfigService; |
| 22 | +import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; |
| 23 | +import java.io.ByteArrayInputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.URI; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | +import org.apache.logging.log4j.core.LoggerContext; |
| 28 | +import org.apache.logging.log4j.core.config.Configuration; |
| 29 | +import org.apache.logging.log4j.core.config.ConfigurationException; |
| 30 | +import org.apache.logging.log4j.core.config.ConfigurationFactory; |
| 31 | +import org.apache.logging.log4j.core.config.ConfigurationSource; |
| 32 | +import org.apache.logging.log4j.core.config.Order; |
| 33 | +import org.apache.logging.log4j.core.config.plugins.Plugin; |
| 34 | +import org.apache.logging.log4j.core.config.xml.XmlConfiguration; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author nisiyong |
| 38 | + */ |
| 39 | +@Plugin(name = "ApolloClientConfigurationFactory", category = ConfigurationFactory.CATEGORY) |
| 40 | +@Order(50) |
| 41 | +public class ApolloClientConfigurationFactory extends ConfigurationFactory { |
| 42 | + |
| 43 | + private final boolean isActive; |
| 44 | + |
| 45 | + public ApolloClientConfigurationFactory() { |
| 46 | + String enabled = System.getProperty("apollo.log4j2.enabled"); |
| 47 | + if (enabled == null) { |
| 48 | + enabled = System.getenv("APOLLO_LOG4J2_ENABLED"); |
| 49 | + } |
| 50 | + isActive = Boolean.parseBoolean(enabled); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected boolean isActive() { |
| 55 | + return this.isActive; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected String[] getSupportedTypes() { |
| 60 | + return new String[]{"*"}; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Configuration getConfiguration(LoggerContext loggerContext, String name, URI configLocation) { |
| 65 | + return getConfiguration(loggerContext, null); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource configurationSource) { |
| 70 | + if (!isActive) { |
| 71 | + LOGGER.warn("Apollo log4j2 plugin is not enabled, please check your configuration"); |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + ConfigFile configFile = ConfigService.getConfigFile("log4j2", ConfigFileFormat.XML); |
| 76 | + |
| 77 | + if (configFile == null || configFile.getContent() == null || configFile.getContent().isEmpty()) { |
| 78 | + LOGGER.warn("Apollo log4j2 plugin is enabled, but no log4j2.xml namespace or content found in Apollo"); |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + byte[] bytes = configFile.getContent().getBytes(StandardCharsets.UTF_8); |
| 83 | + try { |
| 84 | + configurationSource = new ConfigurationSource(new ByteArrayInputStream(bytes)); |
| 85 | + } catch (IOException e) { |
| 86 | + throw new ConfigurationException("Unable to initialize ConfigurationSource from Apollo", e); |
| 87 | + } |
| 88 | + |
| 89 | + // TODO add ConfigFileChangeListener, dynamic load log4j2.xml in runtime |
| 90 | + LOGGER.info("Apollo log4j2 plugin is enabled, loading log4j2.xml from Apollo, content:\n{}", configFile.getContent()); |
| 91 | + return new XmlConfiguration(loggerContext, configurationSource); |
| 92 | + } |
| 93 | +} |
0 commit comments