Skip to content

Commit 5789cac

Browse files
committed
Add apollo-log4j2 module to support log4j2.xml integration
1 parent f6670d5 commit 5789cac

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

apollo-plugin/apollo-log4j2/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Apollo Log4j2
2+
3+
This module could let you integrate log4j2 with apollo easily. You could create an `log42.xml` namespace.
4+
5+
## How to use it?
6+
7+
There are several steps that need to do:
8+
9+
1. Add log4j2 dependency to your classpath
10+
```xml
11+
<dependency>
12+
<groupId>org.apache.logging.log4j</groupId>
13+
<artifactId>log4j-api</artifactId>
14+
</dependency>
15+
<dependency>
16+
<groupId>org.apache.logging.log4j</groupId>
17+
<artifactId>log4j-core</artifactId>
18+
</dependency>
19+
```
20+
2. Add `apollo-log4j2` dependency to your classpath
21+
```xml
22+
<dependency>
23+
<groupId>com.ctrip.framework.apollo</groupId>
24+
<artifactId>apollo-log4j2</artifactId>
25+
</dependency>
26+
```
27+
3. Create a new namespace `log4j2.xml` (namespace format must be XML) in your apollo application
28+
4. Add system properties `apollo.log4j2.enabled` when you run java application
29+
```bash
30+
-Dapollo.log4j2.enabled=true
31+
```
32+
6. Now run the java application, then it could load log4j2 content from Apollo

apollo-plugin/apollo-log4j2/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>apollo-plugin</artifactId>
7+
<groupId>com.ctrip.framework.apollo</groupId>
8+
<version>${revision}</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
<artifactId>apollo-log4j2</artifactId>
13+
<name>Apollo Log4j2</name>
14+
<packaging>jar</packaging>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.ctrip.framework.apollo</groupId>
19+
<artifactId>apollo-client</artifactId>
20+
<optional>true</optional>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.apache.logging.log4j</groupId>
24+
<artifactId>log4j-core</artifactId>
25+
<optional>true</optional>
26+
</dependency>
27+
</dependencies>
28+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
}

apollo-plugin/pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>apollo-java</artifactId>
7+
<groupId>com.ctrip.framework.apollo</groupId>
8+
<version>${revision}</version>
9+
<relativePath>../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
<artifactId>apollo-plugin</artifactId>
13+
<name>Apollo Plugin</name>
14+
<packaging>pom</packaging>
15+
16+
<modules>
17+
<module>apollo-log4j2</module>
18+
</modules>
19+
20+
</project>

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<module>apollo-client-config-data</module>
8383
<module>apollo-mockserver</module>
8484
<module>apollo-openapi</module>
85+
<module>apollo-plugin</module>
8586
</modules>
8687

8788
<dependencyManagement>

0 commit comments

Comments
 (0)