Skip to content

Commit

Permalink
Add apollo-log4j2 module to support log4j2.xml integration
Browse files Browse the repository at this point in the history
  • Loading branch information
nisiyong committed Dec 13, 2022
1 parent 0a7b041 commit c8e76fc
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
32 changes: 32 additions & 0 deletions apollo-plugin/apollo-log4j2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Apollo Log4j2

This module could let you integrate log4j2 with apollo easily. You could create an `log42.xml` namespace.

## How to use it?

There are several steps that need to do:

1. Add log4j2 dependency to your classpath
```xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
```
2. Add `apollo-log4j2` dependency to your classpath
```xml
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-log4j2</artifactId>
</dependency>
```
3. Create a new namespace `log4j2.xml` (namespace format must be XML) in your apollo application
4. Add system properties `apollo.log4j2.enabled` when you run java application
```bash
-Dapollo.log4j2.enabled=true
```
6. Now run the java application, then it could load log4j2 content from Apollo
28 changes: 28 additions & 0 deletions apollo-plugin/apollo-log4j2/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>apollo-plugin</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-log4j2</artifactId>
<name>Apollo Log4j2</name>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.ctrip.framework.apollo.log4j2;

import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationException;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.ConfigurationSource;
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;
import org.apache.logging.log4j.util.Strings;

/**
* @author nisiyong
*/
@Plugin(name = "ApolloLog4j2ConfigurationFactory", category = ConfigurationFactory.CATEGORY)
@Order(50)
public class ApolloLog4j2ConfigurationFactory extends ConfigurationFactory {

private final boolean isActive;

public ApolloLog4j2ConfigurationFactory() {
String enabled = System.getProperty("apollo.log4j2.enabled");
isActive = Boolean.parseBoolean(enabled);
}

@Override
protected boolean isActive() {
return this.isActive;
}

@Override
protected String[] getSupportedTypes() {
return new String[]{"*"};
}

@Override
public Configuration getConfiguration(LoggerContext loggerContext, String name, URI configLocation) {
return getConfiguration(loggerContext, null);
}

@Override
public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource configurationSource) {
if (!isActive) {
return null;
}

ConfigFile configFile = ConfigService.getConfigFile("log4j2", ConfigFileFormat.XML);

if (configFile == null || Strings.isBlank(configFile.getContent())) {
return null;
}

byte[] bytes = configFile.getContent().getBytes(StandardCharsets.UTF_8);
try {
configurationSource = new ConfigurationSource(new ByteArrayInputStream(bytes));
} catch (IOException e) {
throw new ConfigurationException("Unable to initialize ConfigurationSource from Apollo", e);
}

// TODO add ConfigFileChangeListener, dynamic load log4j2.xml in runtime
LOGGER.debug("Initializing configuration ApolloLog4j2Configuration[namespace=log4j2.xml]\n{}", configFile.getContent());
return new XmlConfiguration(loggerContext, configurationSource);
}
}
20 changes: 20 additions & 0 deletions apollo-plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>apollo-java</artifactId>
<groupId>com.ctrip.framework.apollo</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>apollo-plugin</artifactId>
<name>Apollo Plugin</name>
<packaging>pom</packaging>

<modules>
<module>apollo-log4j2</module>
</modules>

</project>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<module>apollo-client-config-data</module>
<module>apollo-mockserver</module>
<module>apollo-openapi</module>
<module>apollo-plugin</module>
</modules>

<dependencyManagement>
Expand Down

0 comments on commit c8e76fc

Please sign in to comment.