-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
apollo-log4j2
module to support log4j2.xml integration
- Loading branch information
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
72 changes: 72 additions & 0 deletions
72
...4j2/src/main/java/com/ctrip/framework/apollo/log4j2/ApolloLog4j2ConfigurationFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters