一、日志pattern规范
log4j2:
错误实例:%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{requestId}] %-5level | %logger{1.}:%line - %msg%n
正确实例:%d{yyyy-MM-dd HH:mm:ss,SSS} | %level | %logger{1.} - %X{requestId} -> %msg%n
错误点: 1. 去掉 -5level 中的-5,否则 kibana解析不了level 2. logger{1.}:%line 中,去掉:%line,否则kibana解析不了class 3. 将 {requestId} 放到 - 号后边,否则 message中查找不到该标识。
logback: %d{ISO8601} | %p | %logger{20} - [%thread] %msg%n
log4j: %d{ISO8601} | %p | %C - [%thread] %m%n
注意:- 后一定留个空格。-后放啥都行。
输出格式:2019-07-01 10:55:33,794 | INFO | com.ebc.log4j2.test.TestLog4j2 - [main] this is demo.
log4j2的缩写包名:http://logging.apache.org/log4j/2.x/manual/layouts.html
二、例子
1、log4j2
pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!--当spring-boot-starter-web时,需要加入以下的log4j-web依赖--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-web</artifactId> </dependency> <!-- 异步日志 --> <dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>3.4.2</version> </dependency>
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="INFO"> <Appenders> <Console name="LogToConsole" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{requestId}] %-5level | %logger{1.}:%line - %msg%n"/> <!--<PatternLayout pattern="%d{HH:mm:ss,SSS}:%4p(%F:%L) - %m%n"/>--> </Console> <!--生产中,使用了ELK,不用使用以下LogToFile--> <File name="LogToFile" fileName="logs/app.log"> <PatternLayout> <Pattern>%d %p %c{1.} [%t] %m%n</Pattern> </PatternLayout> </File> </Appenders> <Loggers> <Logger name="com.ebc" level="info" additivity="false"> <AppenderRef ref="LogToFile"/> <AppenderRef ref="LogToConsole"/> </Logger> <Logger name="org.springframework.boot" level="error" additivity="false"> <AppenderRef ref="LogToConsole"/> </Logger> <Root level="error"> <AppenderRef ref="LogToFile"/> <AppenderRef ref="LogToConsole"/> </Root> </Loggers> </Configuration>
执行异步日志:
$ java -Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector -jar target/logging-log4j2-1.0.jar
2、logback
pom.xml
<!-- Logback --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.8</version> </dependency> <!-- --> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.8</version> </dependency>
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration debug="false"> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{ISO8601} | %p | %logger{20} - [%thread] %msg%n</pattern> </encoder> </appender> <!-- TRACE < DEBUG < INFO < WARN < ERROR --> <root level="INFO"> <appender-ref ref="console"/> </root> </configuration>
3、log4j
pom.xml
<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>apache-log4j-extras</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.2</version> </dependency>
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="true" xmlns:log4j='http://jakarta.apache.org/log4j/'> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{ISO8601} | %p | %C - [%thread] %m%n" /> </layout> </appender> <!-- DEBUG < INFO < WARN < ERROR 没有定义的logger将会使用root logger --> <root> <priority value="INFO" /> <appender-ref ref="console" /> </root> </log4j:configuration>