1. 程式人生 > >log4j 郵件傳送

log4j 郵件傳送

菜鳥Mark一下自己用log4j 大神繞行,小弟不才

1、下載 log4J jar包
2、MAVEN 下載 log4J jar包

log4j
log4j
1.2.17

3、src 目錄建立log4j.properties檔案

4、配置log4j.properties 如下

[%p][%-d{yyyy-MM-dd HH:mm:ss}]%m%n

log等級輸出的資訊配置 INFO,console,info,warn,error

log4j.rootLogger=INFO, stdout, dailyFile,MAIL

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%p][%-d{yyyy-MM-dd HH:mm:ss}]%m%n

log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyFile.Encoding=UTF-8
log4j.appender.dailyFile.Threshold=DEBUG
log4j.appender.dailyFile.ImmediateFlush=true
log4j.appender.dailyFile.Append=true

log輸出路徑配置

log4j.appender.dailyFile.File=D:/logs/daily.log
log4j.appender.dailyFile.DatePattern=’.’yyyy-MM-dd
log4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyFile.layout.ConversionPattern=[%p][%-d{yyyy-MM-dd HH:mm:ss}]%m%n

LOG4J 配置 Email如下

log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold=ERROR

配置快取滿多大發送郵件下面配置的是10K,根據需要配置

log4j.appender.MAIL.BufferSize=10

配置伺服器代理

log4j.appender.MAIL.SMTPHost=smtp.163.com
log4j.appender.MAIL.Subject=ErrorMessage
[email protected]
[email protected]


[email protected]
log4j.appender.MAIL.SMTPPassword=123456
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=[ErrorMessage] %d - %c -%-4r [%t] %-5p %c %x - %m%n