美文网首页
001、简单Log4j 2配置

001、简单Log4j 2配置

作者: airkisser | 来源:发表于2017-03-01 00:07 被阅读0次

1、POM依赖配置

    <properties>
        <log4j.version>2.8</log4j.version>
    </properties>
    <dependencies>
        <!--
            log4j-api:必需,提供了日志的API,是唯一需要添加到编译作用域("<scope>compile</scope>")中的Log4j依赖
            log4j-core:必需,包含标准的Log4j 2实现
            log4j-jcl:支持Commons Logging API的适配器,使用的是Commons Logging API,并且该适配器将使用Log4j 2作为Commons Logging的实现
            log4j-slf4j-impl:SLF4J实现适配器,使用的是SLF4J API,并且该适配器将使用Log4j 2作为SLF4J的实现
        -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>${log4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        <!--
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${log4j.version}</version>
            <scope>runtime</scope>
        </dependency>
        -->
    </dependencies>

2、log4j2.xml或log4j2-test.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} | %-5level %logger{36} - %msg%n"/>
        </Console>
        <RollingFile name="AppFileAppender" fileName="../logs/application.log"
                     filePattern="../logs/application-%d{MM-dd-yyyy}-%i.log">
            <PatternLayout>
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} | [%t] %-5level %l: %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy min="1" max="4" />
        </RollingFile>
    </appenders>
    <loggers>
        <root level="warn">
            <appender-ref ref="Console" />
            <appender-ref ref="AppFileAppender" />
        </root>
        <logger name="com.airkisser" level="info" />
        <logger name="org.apache" level="info" />
    </loggers>
</configuration>

3、log4j2.xml配置说明

3.1、LayPattern参数简单说明

参数名 参数意义 详细描述
%c{参数}或%logger{参数} 输出logger的名称,即语句private static final Logger logger = LogManager.getLogger(App.class.getName())中App.class.getName()的值。也可以使用其他字符串。 如果不带参数,则输出完整的logger名称;如果参数是整数n(只支持正整数),则先将logger名称依照小数点(.)分割成n段,然后取右侧的n段;如果参数不是整数,则除了最右侧的一段,其他整段字符都用这个字符代替,保留小数点;不管怎么写,最右侧的一段都保持不变。默认不带参数,并输出logger的完整名称
%d{参数} 输出时间。 保留关键字有:DEFAULT,ABSOLUTE, COMPACT, DATE, ISO8601, ISO8601_BASIC。或者使用SimpleDateFormat自定义格式,如:%d{yyyy-MM-dd HH:mm:ss.SSS}
%l 输出Log位置,如:com.airkisser.Main.main(Main.java:13) ~
%L 输出行号 ~
%m或%msg或%message 输出信息,如logger.error(String msg)中的msg ~
%M或%method 输出方法名 ~
%n 换行符 ~
%level 日志信息的级别 如:%-5level表示占5个字符右对齐,%level{WARN=W, DEBUG=D, ERROR=E, TRACE=T, INFO=I}级别显示别名
%t或%thread 创建logging事件的线程名 ~

4、简单使用

package com.airkisser;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Main {

    private static final Logger LOGGER = LogManager.getLogger(Main.class);

    public static void main(String[] args) {
        LOGGER.debug("Logger Level: debug");
        LOGGER.warn("Logger Level: warn");
        LOGGER.info("Logger Level: info");
        LOGGER.error("Logger Level: error");
    }

}

控制台打印如下:

2017-02-28 23:28:41.700 | WARN  com.airkisser.Main - Logger Level: warn
2017-02-28 23:28:41.703 | INFO  com.airkisser.Main - Logger Level: info
2017-02-28 23:28:41.703 | ERROR com.airkisser.Main - Logger Level: error

日志文件../logs/application.log文件内容如下:

2017-02-28 23:28:41.700 | [main] WARN  com.airkisser.Main.main(Main.java:12): Logger Level: warn
2017-02-28 23:28:41.703 | [main] INFO  com.airkisser.Main.main(Main.java:13): Logger Level: info
2017-02-28 23:28:41.703 | [main] ERROR com.airkisser.Main.main(Main.java:14): Logger Level: error

相关文章

网友评论

      本文标题:001、简单Log4j 2配置

      本文链接:https://www.haomeiwen.com/subject/tmifgttx.html