Log4j 2研究(一):示例程序

来源:互联网 发布:为什么淘宝黑搜要四天 编辑:程序博客网 时间:2024/05/09 12:46

简介

本文介绍了Log4j 2.2日志组件的示例程序,读者可以根据自己的情况,在自己的项目中快速使用起log4j组件。学习一门新的技术的方式,是首先快速的使用它,然后再研究其实现。本文的示例程序在Maven+Eclipse的开发环境环境中创建。

创建工程

1. 创建Maven工程,在pom.xml文件中添加log4j-core和log4j-api依赖文件。
<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"><modelVersion>4.0.0</modelVersion><groupId>com.csdn.jinguangliu</groupId><artifactId>log4j2</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.2</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.2</version></dependency></dependencies>    <build>        <finalName>log4j2</finalName>        <plugins>            <plugin>                <artifactId>maven-compiler-plugin</artifactId>                <version>2.3.2</version>                <configuration>                    <source>1.7</source>                    <target>1.7</target>                </configuration>            </plugin>        </plugins>    </build></project>
2. 创建示例程序
使用log4j非常简单,
1. 定义类使用的Logger静态实例,其由LogManager.getLogger()工厂函数获取
2. 调用Logger的系列日志接口,产生日志
示例代码Bar和MyApp的源码文件如下:
Bar.java:
package log4jtest;import org.apache.logging.log4j.Logger;import org.apache.logging.log4j.LogManager; public class Bar {  static final Logger logger = LogManager.getLogger(Bar.class.getName());   public boolean doIt() {    logger.entry();    logger.error("Did it again!");    return logger.exit(false);  }}
MyApp.java:
package log4jtest;import org.apache.logging.log4j.Logger;import org.apache.logging.log4j.LogManager; public class MyApp {     // Define a static logger variable so that it references the    // Logger instance named "MyApp".    private static final Logger logger = LogManager.getLogger(MyApp.class);     public static void main(String[] args) {         // Set up a simple configuration that logs on the console.         logger.trace("Entering application.");        Bar bar = new Bar();        if (!bar.doIt()) {            logger.error("Didn't do it.");        }        logger.trace("Exiting application.");    }}
运行MyApp.java文件,得到的输出如下:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.13:20:11.720 [main] ERROR log4jtest.Bar - Did it again!13:20:11.721 [main] ERROR log4jtest.MyApp - Didn't do it.

缺省配置文件解析

MyApp.java文件输出中,有一条log信息输出“ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.”,提示log4j2配置文件未找到,使用缺省配置,仅输出error级别的日志到控制窗口。在日志的输出中,也确实仅看到了ERROR级别的告警。
那么,缺省配置文件定义了哪些内容呢。下面显示的是log4j 2.2的缺省xml配置文件:
<?xml version="1.0" encoding="UTF-8"?><Configuration status="WARN">  <Appenders>    <Console name="Console" target="SYSTEM_OUT">      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>    </Console>  </Appenders>  <Loggers>    <Root level="error">      <AppenderRef ref="Console"/>    </Root>  </Loggers></Configuration>
1. lo4g4j 2仅支持XML, JSON及YAML格式的配置文件
2. 在xml配置文件,定义了日志输出类型为Console,并定义了输出的格式;日志的输出级别为ERROR。
3. 如果你向对log4j2 进行详细配置,可基于缺省配置文件进行修改和添加,并将配置文件log4j2.xml放置在src/main/resources目录下。
4. XML配置文件的schema文件在log4j-core-2.2.jar中,名字为log4j-config.xsd。

工程目录结构

下图为示例程序的工程目录结构,供参考使用:

参考资料

1. http://logging.apache.org/log4j/2.x/manual/configuration.html
0 0