log4j configuration

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

  1. org.apache.log4j.ConsoleAppender
    to console
  2. org.apache.log4j.FileAppender
    to file
  3. org.apache.log4j.DailyRollingFileAppender
    create a file everyday
  4. org.apache.log4j.RollingFileAppender
    to some size make a new file
  5. 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

  1. org.apache.log4j.HTMLLayout
  2. org.apache.log4j.PatternLayout
    customized layout
  3. org.apache.log4j.SimpleLayout
    contain log level, string so on
  4. org.apache.log4j.TTCCLayout
    contain time/ thread/ class info so on
  5. 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#OFF,systemOut,logFile,logDailyFile,logRollingFile,logMail,logDB,ALL
log4j.rootLogger =ALL,systemOut,logFile,logDailyFile,logRollingFile,logMail,logDB

#输出到控制台
log4j.appender.systemOut = org.apache.log4j.ConsoleAppender
log4j.appender.systemOut.layout = org.apache.log4j.PatternLayout
log4j.appender.systemOut.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
#log4j.appender.systemOut.layout.ConversionPattern = %d{yyyy-MM-dd HH\:mm\:ss} %5p %c{1}\: %m%n
#log4j.appender.systemOut.layout.ConversionPattern = %d{yyyy-MM-dd HH\:mm\:ss,SSS} %5p [%t] (%F\:%L) - %m%n
log4j.appender.systemOut.Threshold = DEBUG
log4j.appender.systemOut.ImmediateFlush = TRUE
log4j.appender.systemOut.Target = System.out

#输出到文件
log4j.appender.logFile = org.apache.log4j.FileAppender
log4j.appender.logFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logFile.Threshold = DEBUG
log4j.appender.logFile.ImmediateFlush = TRUE
log4j.appender.logFile.Append = TRUE
log4j.appender.logFile.File = ../Struts2/WebRoot/log/File/log4j_Struts.log
log4j.appender.logFile.Encoding = UTF-8

#按DatePattern输出到文件
log4j.appender.logDailyFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.logDailyFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logDailyFile.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logDailyFile.Threshold = DEBUG
log4j.appender.logDailyFile.ImmediateFlush = TRUE
log4j.appender.logDailyFile.Append = TRUE
log4j.appender.logDailyFile.File = ../Struts2/WebRoot/log/DailyFile/log4j_Struts
log4j.appender.logDailyFile.DatePattern = '.'yyyy-MM-dd-HH-mm'.log'
log4j.appender.logDailyFile.Encoding = UTF-8

#设定文件大小输出到文件
log4j.appender.logRollingFile = org.apache.log4j.RollingFileAppender
log4j.appender.logRollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logRollingFile.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logRollingFile.Threshold = DEBUG
log4j.appender.logRollingFile.ImmediateFlush = TRUE
log4j.appender.logRollingFile.Append = TRUE
log4j.appender.logRollingFile.File = ../Struts2/WebRoot/log/RollingFile/log4j_Struts.log
log4j.appender.logRollingFile.MaxFileSize = 1MB
log4j.appender.logRollingFile.MaxBackupIndex = 10
log4j.appender.logRollingFile.Encoding = UTF-8

#用Email发送日志
log4j.appender.logMail = org.apache.log4j.net.SMTPAppender
log4j.appender.logMail.layout = org.apache.log4j.HTMLLayout
log4j.appender.logMail.layout.LocationInfo = TRUE
log4j.appender.logMail.layout.Title = Struts2 Mail LogFile
log4j.appender.logMail.Threshold = DEBUG
log4j.appender.logMail.SMTPDebug = FALSE
log4j.appender.logMail.SMTPHost = SMTP.163.com
log4j.appender.logMail.From = xxx@163.com
log4j.appender.logMail.To = xxx@gmail.com
#log4j.appender.logMail.Cc = xxx@gmail.com
#log4j.appender.logMail.Bcc = xxx@gmail.com
log4j.appender.logMail.SMTPUsername = leon
log4j.appender.logMail.SMTPPassword = 123456
log4j.appender.logMail.Subject = Log4j Log Messages
#log4j.appender.logMail.BufferSize = 1024
#log4j.appender.logMail.SMTPAuth = TRUE

#将日志登录到MySQL数据库
log4j.appender.logDB = org.apache.log4j.jdbc.JDBCAppender
log4j.appender.logDB.layout = org.apache.log4j.PatternLayout
log4j.appender.logDB.Driver = com.mysql.jdbc.Driver
log4j.appender.logDB.URL = jdbc:mysql://127.0.0.1:3306/xxx
log4j.appender.logDB.User = root
log4j.appender.logDB.Password = 123456
log4j.appender.logDB.Sql = INSERT INTOT_log4j(project_name,create_date,level,category,file_name,thread_name,line,all_category,message)values('Struts2','%d{yyyy-MM-ddHH:mm:ss}','%p','%c','%F','%t','%L','%l','%m')

log4j.properties