Skip to content

Commit 0560bf5

Browse files
nisiyongnobodyiam
andauthored
Add apollo-plugin-log4j2 module to support log4j2.xml integration (#6)
* Add `apollo-log4j2` module to support log4j2.xml integration * Add License header * Rename `apollo-log4j2` as `apollo-plugin-log4j2` * Rename `ApolloLog4j2ConfigurationFactory` to `ApolloClientConfigurationFactory` * Update CHANGES.md * Update apollo-plugin/apollo-plugin-log4j2/pom.xml Co-authored-by: Jason Song <[email protected]> * Support env APOLLO_LOG4J2_ENABLED * Update apollo-plugin/apollo-plugin-log4j2/README.md Co-authored-by: Jason Song <[email protected]> Co-authored-by: Jason Song <[email protected]>
1 parent 0a7b041 commit 0560bf5

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Apollo Java 2.1.0
1515
* [Fix ApolloBootstrapPropertySources precedence issue](https://github.com/apolloconfig/apollo-java/pull/3)
1616
* [Apollo Client Support Spring Boot 3.0](https://github.com/apolloconfig/apollo-java/pull/4)
1717
* [apollo-client-config-data support spring boot 3.0](https://github.com/apolloconfig/apollo-java/pull/5)
18+
* [Add apollo-plugin-log4j2 module to support log4j2.xml integration](https://github.com/apolloconfig/apollo-java/pull/6)
1819

1920
------------------
2021
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/1?closed=1)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Apollo Log4j2
2+
3+
This module could let you integrate log4j2 with apollo easily.
4+
You could create a namespace `log42.xml`, then log4j2 will load the configuration from it.
5+
6+
## How to use it?
7+
8+
There are several steps that need to do:
9+
10+
1. Add `apollo-plugin-log4j2` dependency with `log4j2` dependency
11+
```xml
12+
<dependency>
13+
<groupId>com.ctrip.framework.apollo</groupId>
14+
<artifactId>apollo-plugin-log4j2</artifactId>
15+
</dependency>
16+
<!-- log4j2 dependency -->
17+
<dependency>
18+
<groupId>org.apache.logging.log4j</groupId>
19+
<artifactId>log4j-api</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.apache.logging.log4j</groupId>
23+
<artifactId>log4j-core</artifactId>
24+
</dependency>
25+
```
26+
2. Create a new namespace `log4j2.xml` (namespace format must be XML) in your apollo application
27+
3. Add system properties `apollo.log4j2.enabled` or set env variable `APOLLO_LOG4J2_ENABLED` when you run java application
28+
```bash
29+
-Dapollo.log4j2.enabled=true
30+
```
31+
```bash
32+
APOLLO_LOG4J2_ENABLED=true
33+
```
34+
4. Now run the java application, then it could load log4j2 content from Apollo
35+
36+
## Notice
37+
38+
By default, log4j2 will load the configuration from the classpath.
39+
This module only affects when you set the system properties `apollo.log4j2.enabled=true` or set the env variable `APOLLO_LOG4J2_ENABLED =true` to enable it.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2022 Apollo Authors
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
~
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<parent>
22+
<artifactId>apollo-plugin</artifactId>
23+
<groupId>com.ctrip.framework.apollo</groupId>
24+
<version>${revision}</version>
25+
<relativePath>../pom.xml</relativePath>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
<artifactId>apollo-plugin-log4j2</artifactId>
29+
<name>Apollo Plugin Log4j2</name>
30+
<packaging>jar</packaging>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>com.ctrip.framework.apollo</groupId>
35+
<artifactId>apollo-client</artifactId>
36+
<scope>provided</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.apache.logging.log4j</groupId>
40+
<artifactId>log4j-core</artifactId>
41+
<scope>provided</scope>
42+
</dependency>
43+
</dependencies>
44+
</project>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

apollo-plugin/pom.xml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2022 Apollo Authors
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
~
17+
-->
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<parent>
22+
<artifactId>apollo-java</artifactId>
23+
<groupId>com.ctrip.framework.apollo</groupId>
24+
<version>${revision}</version>
25+
<relativePath>../pom.xml</relativePath>
26+
</parent>
27+
<modelVersion>4.0.0</modelVersion>
28+
<artifactId>apollo-plugin</artifactId>
29+
<name>Apollo Plugin</name>
30+
<packaging>pom</packaging>
31+
32+
<modules>
33+
<module>apollo-plugin-log4j2</module>
34+
</modules>
35+
36+
</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)