log4j发邮件

首先需要有

activation.jar、mail.jar(发邮件)、
commons-logging-1.1.1.jar、log4j-1.2.17.jar(common-logging与log4j的优势结合)

四个jar包。

 

package com.horizon.log;
import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

public class TestLog {

    private static Log log = LogFactory.getLog(TestLog.class);

    public void test() {
        log.debug("This is the debug message.");
        log.info("This is the info message.");
        log.warn("This is the warn message.");
        log.error("This is the error message.");
        log.fatal("This is the fatal message.");
    }

    public static void main(String[] args) {
        TestLog testLog = new TestLog();
        testLog.test();
    }

}

 

配置文件log4j.properties:

log4j.rootLogger = info,stdout,RF,MAIL

#There is 3 appender,you can use any of them according to you requirement.

#Set the variable
pattern=Happened Time[%d]    Log Type[%p](%F:%L) - %m%n

#appender1[name=stdout]   log to console
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern =${pattern}

#appender2[name=RF]   log to file
log4j.appender.RF = org.apache.log4j.RollingFileAppender
log4j.appender.RF.File = d:/logs/pfingoopenapi2.log
log4j.appender.RF.MaxFileSize = 10000KB
log4j.appender.RF.MaxBackupIndex = 1

log4j.appender.RF.layout=org.apache.log4j.PatternLayout
log4j.appender.RF.layout.ConversionPattern=${pattern}


#appender3[name=MAIL]  log to email
log4j.appender.MAIL =org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold=ERROR
log4j.appender.MAIL.BufferSize=512
log4j.appender.MAIL.SMTPHost=smtp.163.com
log4j.appender.MAIL.to=tv***@163.com
log4j.appender.MAIL.from=tv***@163.com
#custom by myself[using auth]
log4j.appender.MAIL.SMTPUsername=tv***
#custom by myself[using auth]
log4j.appender.MAIL.SMTPPassword=your mail password
log4j.appender.MAIL.Subject=Log4J Message
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=${pattern}

 

 

posted @ 2013-01-21 23:57  horizon~~~  阅读(229)  评论(0编辑  收藏  举报