首页  

log4j2使用要点     所属分类 log4j2 浏览量 728
log4j2.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>



%d{HH:mm:ss.SSS}  输出毫秒 
%t                当前线程名称
%-5level          日志级别,-5 左对齐 固定输出5个字符,右边补0
%logger           logger名 
%msg              日志文本
%n                换行
%F                类文件名 
%L              输出行号
%M              方法名
%l              类名 方法名 文件名 行数


%C  , %F or %file, %l or %location, %L or %line, %M or %method
对性能影响很大 ,谨慎使用

In order to provide caller location information, 
the logging library will take a snapshot of the stack, 
and walk the stack trace to find the location information.

capturing caller location has a similar impact across all logging libraries, 
and slows down asynchronous logging by about 30-100x 


Log4j2 Lookups 环境变量绑定
环境变量 ${xxx} ${env:xxx}
系统参数 ${sys:xxx}
${sys:catalina.base}/logs/xxx.log
-Dcatalina.base=xxx 

PatternLayout
%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n 

%c{1.}
org.apache.commons.Foo -> o.a.c.Foo

RollingFileAppender


<RollingFile name="RollingFile"
	fileName="${sys:catalina.base}/logs/xxx.log"
	filePattern="${sys:catalina.base}/logs/xxx.%d{yyyy-MM-dd}-%i.log.gz">
	 <PatternLayout>
	     <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
	 </PatternLayout>
	 <Policies>
	     <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
	     <SizeBasedTriggeringPolicy size="200 MB"/>
	 </Policies>
</RollingFile>


.gz 后缀 自动压缩 



桥接其他日志组件
log4j(1.2.x) common-log(jcl) self4j

org.apache.logging.log4j:log4j-api:2.7
org.apache.logging.log4j:log4j-core:2.7
org.apache.logging.log4j:log4j-1.2-api:2.7
org.apache.logging.log4j:log4j-jcl:2.7
org.apache.logging.log4j:log4j-slf4j-impl:2.7

上一篇     下一篇
Log4j2 lookup 安全漏洞

nginx常用优化

JFR实战

JNDI demo

logback简介

MySQL获取所有表和表结构信息