最牛逼 Java 日志框架 — Log4j2,性能无敌,横扫对手.....
阅读本文大概需要 8 分钟。
来自:https://juejin.cn/post/6945753017878577165
更简化的配置
更强大的参数格式化
最夸张的异步性能
org.apache.logging.log4j » log4j-api
org.apache.logging.log4j » log4j-core
logger.debug("Logging in user {} with birthday {}", user.getName(), user.getBirthdayCalendar());
public static Logger logger = LogManager.getFormatterLogger("Foo");
logger.debug("Logging in user %s with birthday %s", user.getName(), user.getBirthdayCalendar());
logger.debug("Logging in user %1$s with birthday %2$tm %2$te,%2$tY", user.getName(), user.getBirthdayCalendar());
logger.debug("Integer.MAX_VALUE = %,d", Integer.MAX_VALUE);
logger.debug("Long.MAX_VALUE = %,d", Long.MAX_VALUE);
注意,如果想使用String.format的形式,需要使用LogManager.getFormatterLogger而不是LogManager.getLogger
logger.printf(Level.INFO, "Logging in user %1$s with birthday %2$tm %2$te,%2$tY", user.getName(), user.getBirthdayCalendar());
logger.debug("Opening connection to {}...", someDataSource);
logger.debug("入参报文:{}",JSON.toJSONString(policyDTO));
if(logger.isDebugEnabled()){
logger.debug("入参报文:{}",JSON.toJSONString(policyDTO));
}
void debug(String message, Supplier>... paramSuppliers);
void info(String message, Supplier>... paramSuppliers);
void trace(String message, Supplier>... paramSuppliers);
void error(String message, Supplier>... paramSuppliers);
//等同于下面的先判断,后打印
logger.debug("入参报文:{}",() -> JSON.toJSONString(policyDTO));
if(logger.isDebugEnabled()){
logger.debug("入参报文:{}",JSON.toJSONString(policyDTO));
}
<configuration>
<appender name = "File" class= "ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/app.logfile>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/archives/app-%d{yyyy-MM-dd}.log.gzfileNamePattern>
<maxFileSize>1 GBmaxFileSize>
rollingPolicy>
appender>
<root level="info">
<appender-ref ref="File"/>
root>
configuration>
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
status="warn" name="XInclude">
<Appenders>
<RollingFile name="File" fileName="logs/app.log" filePattern="logs/archives/app-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] %-40.40c{1.} : %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 GB"/>
Policies>
RollingFile>
Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="File"/>
Root>
Loggers>
Configuration>
<RollingFile name="File">
<appender name = "File" class= "ch.qos.logback.core.rolling.RollingFileAppender">
异步队列使用高性能队列 - **LMAX Disruptor **
Appender丰富,有JMS/JPA/KAFKA/Http/MONGODB/CouchDB/Socket/Script等各种Appender的支持
支持自定义日志级别
……
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.14.1version>
dependency>
<Configuration xmlns:xi="http://www.w3.org/2001/XInclude"
status="warn" name="XInclude">
<Properties>
<Property name="PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] %-40.40c{1.} : %m%n"/>
Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${PATTERN}"/>
Console>
<RollingFile name="File" fileName="logs/app.log" filePattern="logs/archives/app-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="${PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 GB"/>
Policies>
RollingFile>
Appenders>
<Loggers>
<Logger name="your logger/package name" level="debug" additivity="false"/>
<Root level="INFO">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
Root>
Loggers>
Configuration>
;
<Configuration>
<Properties>
<Property name="name1">valueproperty>
<Property name="name2" value="value2"/>
Properties>
<filter ... />
<Appenders>
<appender ... >
<filter ... />
appender>
...
Appenders>
<Loggers>
<Logger name="name1">
<filter ... />
Logger>
...
<Root level="level">
<AppenderRef ref="name"/>
Root>
Loggers>
Configuration>
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Logger logger = LogManager.getLogger(Log4j2Test.class);
logger.error(...);
logger.warn(...);
logger.info(...);
logger.debug(...);
logger.trace(...);
-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector
推荐阅读:
蔚来一面:用Object做hashMap的Key时需要做什么?
最近面试BAT,整理一份面试资料《Java面试BATJ通关手册》,覆盖了Java核心技术、JVM、Java并发、SSM、微服务、数据库、数据结构等等。
朕已阅
评论