虽然一直在用log4j,但是实际对它还不够了解,今天做下笔记。
文章目录
rootLogger
语法:
log4j.rootLogger = [ level ] , appenderName, appenderName, …
示例:
log4j.rootLogger = INFO , CONSOLE, appenderName, …
也就是说后面可以跟日志级别,以及记录期名称。
appender
appender是log4j中很重要的一个元素。叫:追加器,日志记录期 都行。 主要负责日志的输出。
常见的有console、fileAppender2类。
console
标准控制台输出。
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
当然也可以自定义名称。
#Console2
log4j.appender.Console2=org.apache.log4j.ConsoleAppender
log4j.appender.Console2.layout=org.apache.log4j.PatternLayout
log4j.appender.Console2.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
FileAppender
FileAppender
#File
log4j.appender.File = org.apache.log4j.FileAppender # 哪种appender
log4j.appender.File.File = d://log4j2.log # appender 文件名
log4j.appender.File.layout = org.apache.log4j.PatternLayout # appender的布局
log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n # appender的布局格式
RollingFileAppender
log4j.rootCategory=info, Console,File
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.base}/logs/portal.log
log4j.appender.R.Encoding=GBK
log4j.appender.R.MaxFileSize=256KB
log4j.appender.R.Append=true
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%-d %t %-5p [%c{1}:%L] %m%n
DailyRollingFileAppender
log4j.rootCategory=INFO, CONSOLE,DailyRolling
log4j.appender.DailyRolling=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DailyRolling.File=${catalina.base}/logs/portal.log
log4j.appender.DailyRolling.DatePattern=yyyy-MM-dd'.log'
log4j.appender.DailyRolling.layout=org.apache.log4j.PatternLayout
log4j.appender.DailyRolling.layout.ConversionPattern=%-d %t %-5p [%c{1}:%L] %m%n
设置年月日
'.'yyyy-MM-dd'.log' 按天生成,这个用的最多
'.'yyyy-MM-dd-HH'.log' 按小时生成
'.'yyyy-MM-dd-HH-mm'.log' 按分钟生成
appender 日志格式(pattern)
pattern其实是一大块内容,自定义日志格式肯定要用到。
下列是较常见的,肯定不全(官网的是全量的)
而且logback和log4j也不是完全兼容的,主要功能都兼容,但是有些是不支持的,例如pid(log4j文档中有,logback中配置报错)。
appender日志格式
%c(%logger) 输出所属类的全名,可在修改为 %d{Num} ,Num类名输出的维(如:"org.apache.elathen.ClassName",%C{2}将输出elathen.ClassName)
%C(%class) 描述是输出全名,实际和%c、%logger基本一致
%d(%data) 输出日志时间其格式为 %d{yyyy-MM-dd HH:mm:ss,SSS},可指定格式 如 %d{HH:mm:ss}
%F file表示文件名
%l(%Line) 输出日志事件发生位置,包括类目名、发生线程,在代码中的行数 注:实测无效,报错%PARSER_ERROR[l]
%L
%n 换行符
%m(%msg)(%message) 输出代码指定信息,如info(“aaa”),输出aaa
%M method输出方法名
%L 代码所在的行数
%p(%level) 输出优先级,即 FATAL ,ERROR 等 这里可以设置长度
%r 输出从启动到显示该log信息所耗费的毫秒数 注:是项目启动,到打印当前日志的时间,没什么意义,所以一般用不到
%t(%thread)(%tn)(%threadName) 输出产生该日志事件的线程名
pattern%
百分号是什么意思?
%通常用来表示变量。
那么%X
呢,一般用来表示自定义变量。如:%X{requestId}
%是基础中的基础。
%20c 正数表示右对齐,如果不足20位长度,左侧补全空格。如果超过20位长度,原长度输出。
%-20c 负数表是左对齐,如果不足20位长度,右侧补全空格。
%.30c 点号表示如果超过30个长度,从右开始截断30个字符。(也就是说点号表示截断,非点号表示补全)
%20.30c 如果不超过20位长度,左边补全空格。如果超过30位长度,从右端开始截取。
%-20.30c 左对齐,如果不足20位长度,右侧补全空格。
%-20.-30c
总结下:
.号之前是补全部分(如果没有点号表示就是补全)。
.号之后是截取部分。
正数表示右对齐,左侧补全或截取。
负数表示左对齐,右侧补全或截取。
pattern c(小c)、logger
c和logger是一个意思,都是表示class。
如下表格也来自官网,拿过来主要为了直观。
Conversion Pattern | Logger Name | Result |
---|---|---|
%c{1} | org.apache.commons.Foo | Foo |
%c{2} | org.apache.commons.Foo | commons.Foo |
%c{10} | org.apache.commons.Foo | org.apache.commons.Foo |
%c{-1} | org.apache.commons.Foo | apache.commons.Foo |
%c{-2} | org.apache.commons.Foo | commons.Foo |
%c{-10} | org.apache.commons.Foo | org.apache.commons.Foo |
%c{1.} | org.apache.commons.Foo | o.a.c.Foo |
%c{1.1..} | org.apache.commons.test.Foo | o.a...Foo |
%c{.} | org.apache.commons.test.Foo | …Foo |
%c{1.1.1.*} | org.apache.commons.test.Foo | o.a.c.test.Foo |
%c{1.2.*} | org.apache.commons.test.Foo | o.a.c.test.Foo |
%c{1.3.*} | org.apache.commons.test.Foo | o.a.commons.test.Foo |
%c{1.8.*} | org.apache.commons.test.Foo | org.apache.commons.test.Foo |
英文描述:
Outputs the name of the logger that published the logging event. The logger conversion specifier can be optionally followed by precision specifier, which consists of a decimal integer, or a pattern starting with a decimal integer.
When the precision specifier is an integer value, it reduces the size of the logger name. If the number is positive, the layout prints the corresponding number of rightmost logger name components. If negative, the layout removes the corresponding number of leftmost logger name components. If the precision contains periods then the number before the first period identifies the length to be printed from items that precede tokens in the rest of the pattern. If the number after the first period is followed by an asterisk it indicates how many of the rightmost tokens will be printed in full. See the table below for abbreviation examples.
If the precision contains any non-integer characters, then the layout abbreviates the name based on the pattern. If the precision integer is less than one, the layout still prints the right-most token in full. By default, the layout prints the logger name in full.
英文造诣有限,读不太懂。
一般来说直接用%c就行,有需要的可以研究下细节。
pattern C(大C)、class
pattern p、level
这两个是一个意思
%-5p 负数表示左对齐(这个用的比较多),因为日志最多5个长度,这样已经满足需要了。
%5p 正数表示右对齐(实际用的比较少)
%p 默认输出,如果对level的对齐没有要求,直接用默认就行。
%-1p 非主流(毫无作用),相当于默认输出,负号表示左对齐(默认就是左对齐),1表示level长度,因为1<INFO、debug的长度,会按实际输出,所以和默认一个意思。
pattern的官网文档
这个链接可以直接到log4j官网pattern地址。
https://logging.apache.org/log4j/2.x/manual/layouts.html#patterns
日志级别
略。
其他
log4j区分大小写吗
好问题。
log4j和slf4j的区别
slf4j是日志接口,是抽象的。 他可以有多种实现方式,例如log4j,logback等。
log4j是slf4j的一种实现。
main方法如何调试logback日志
如果用的是logback,可以在main方法中调试logger日志吗。
首先,main方法中是可以引入logger的,也可以打印出日志。
但是发现和logback中配置的不一样。
是因为main方法执行时,走的是默认的log4.properties配置,并没有走logback。
那么如何走logback呢?
可以在StartupApplication的main
方法中打印日志,这里的main方法是会加载logback的。
注:日志可以打在第一行,这样既可以调试,也不用花时间等待启动。