log4j2与XMLLayout如何?

时间:2015-12-31 04:26:58

标签: log4j2 xml-layout

使用XMLLayout的log4j2网上的信息非常少。

我没有找到任何简单示例的链接( log4j2 + XMLLayout );

有人可以发布一个相同的简单例子......

问候 KARTHIK

1 个答案:

答案 0 :(得分:2)

以下是演示XmlLayout

使用的最小示例应用程序

的pom.xml

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test-log4j2</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>Test Log4J 2</name>
    <description>Test Log4J 2</description>
    <properties>
        <jackson.version>2.5.0</jackson.version>
        <log4j.version>2.5</log4j.version>
    </properties>
    <build>
        <plugins>            
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <finalName>${project.artifactId}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>${jackson.version}</version>
        </dependency>
    </dependencies>
</project>

的src /主/爪哇/ Main.java

import org.apache.logging.log4j.LogManager;

public class Main {
    public static void main(String[] args) {
        LogManager.getLogger(Main.class).info("This is a test.");
    }
}

的src /主/资源/ log4j2.properties

status = ERROR
name = PropertiesConfig

rootLogger.level = INFO
rootLogger.appenderRefs = CONSOLE
rootLogger.appenderRef.CONSOLE.ref = CONSOLE

appenders = CONSOLE

appender.CONSOLE.type = Console
appender.CONSOLE.name = CONSOLE
appender.CONSOLE.layout.type = XmlLayout
appender.CONSOLE.layout.complete = true
appender.CONSOLE.layout.compact = true

演示

> mvn clean package

> java -jar target/test-log4j2.jar
<?xml version="1.0" encoding="UTF-8"?><Events xmlns="http://logging.apache.org/log4j/2.0/events"><Event xmlns="" xmlns="http://logging.apache.org/log4j/2.0/events" timeMillis="1451539702378" thread="main" level="INFO" loggerName="Main" endOfBatch="false" loggerFqcn="org.apache.logging.log4j.spi.AbstractLogger"><Message>This is a test.</Message></Event></Events>

最棘手的部分是找出所需的杰克逊依赖性。如果没有这个,Log4J 2会遇到内部ClassNotFoundException错误,无法初始化XmlLayout

如果您更喜欢基于XML的配置,那么这里是等效的XML配置。

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error" name="XMLConfig">
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="CONSOLE"/>
        </Root>
    </Loggers>
    <Appenders>
        <Console name="CONSOLE">
            <XMLLayout complete="true" compact="true"/>
        </Console>
    </Appenders>
</Configuration>