level
log4j.rootLogger = [level],appenderName,appenderName2,…
level show log’s privilege, OFF,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,ALL
log4j recommand 4 level, from low to high : DEBUG, INFO, WARN, ERROR
output
appenderName point logs’ output target
log4j.appender.xxx
log4j.appender.T.xxx
appender have these
- org.apache.log4j.ConsoleAppender
to console - org.apache.log4j.FileAppender
to file - org.apache.log4j.DailyRollingFileAppender
create a file everyday - org.apache.log4j.RollingFileAppender
to some size make a new file - org.apache.log4j.WriterAppender
stream to everywhere
ConsoleAppender
- -Threshold = DEBUG:log lowest output level
- -ImmediateFlush = TRUE:default is
true
, all log will output immediately - -Target = System.err:default is
System.out
, out put to console(err is red,out is black)
FileAppender
- -Threshold = INFO:log lowest output level
- -ImmediateFlush = TRUE:default is
true
, all log will output immediately - -File = log/log4j.log:output file path
- -Append = FALSE:default
true
, add message to file,false
override file - -Encoding = UTF-8:appoint file encoding
DailyRollingFileAppender
- -Threshold = WARN:log lowest output level
- -ImmediateFlush = TRUE:default is
true
, all log will output immediately - -File = log/log4j.log:output file path
- -Append = FALSE:default
true
, add message to file,false
override file - -DatePattern=’.’yyyy-ww:every week
- ‘.’yyyy-MM:month
- ‘.’yyyy-ww:week
- ‘.’yyyy-MM-dd:day
- ‘.’yyyy-MM-dd-a:twice a day
- ‘.’yyyy-MM-dd-HH:hour
- ‘.’yyyy-MM-dd-HH-mm:minute
- -Encoding = UTF-8:appoint file encoding
RollingFileAppender
- -Threshold = ERROR:log lowest output level
- -ImmediateFlush = TRUE:default is
true
, all log will output immediately - -File = log/log4j.log:output file path
- -Append = FALSE:default
true
, add message to file,false
override file - -MaxFileSize = 100KB:suffix can be
KB
MB
GB
when file met the threshold,
new file will be created like log4j.log.1 log4j.log.2 - -MaxBackupIndex = 2:appoint rolling files’ up limit
- -Encoding = UTF-8:appoint file encoding
layout
- org.apache.log4j.HTMLLayout
- org.apache.log4j.PatternLayout
customized layout - org.apache.log4j.SimpleLayout
contain log level, string so on - org.apache.log4j.TTCCLayout
contain time/ thread/ class info so on - org.apache.log4j.xml.XMLLayout
HTMLLayout
- -LocationInfo = TRUE:default
false
, output java file and row num - -Title=Struts Log Message:默认值 Log4J Log Messages
PatternLayout
- -ConversionPattern = %m%n:formatting message [reference to Format]
XMLLayout
- -LocationInfo = TRUE:default
false
, output java file and row num
Format
similar with c style
| a | b |
| —- | —- |
| %m | message in code |
| %p | level:DEBUG,INFO,WARN,ERROR,FATAL |
| %r | Millisecond from code to file |
| %c | class full name |
| %t | Thread |
| %n | Windows \r\n
Unix \n
Max \r
|
| %d | ISO8601 in default or %d{yyyy-MM-dd HH\:mm\:ss,SSS} like: 2017-09-18 22:10:28,921 |
| %l | detail in event like: Testlog.main(TestLog.java:10)|
| %F | file name |
| %L | code row num |
| %x | 输出和当前线程相关联的NDC(嵌套诊断环境),像java servlets多客户多线程的应用中 |
| %% | “%” |
| %5c | min width right alignment in default |
| %-5c | min width -
left alignment |
| %.5c | max width split left char |
| %20.30c | less than 20 add blank, more than 30 split from left |
Demo
1 | #OFF,systemOut,logFile,logDailyFile,logRollingFile,logMail,logDB,ALL |