zoukankan      html  css  js  c++  java
  • 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}
  • 相关阅读:
    bzoj 1588: [HNOI2002]营业额统计 treap
    Codeforces Round #135 (Div. 2) E. Parking Lot 线段数区间合并
    cdoj 851 方老师与素数 bfs
    hdu 5150 Sum Sum Sum 水
    Codeforces Round #376 (Div. 2) F. Video Cards 数学,前缀和
    POJ 1984 Navigation Nightmare 带全并查集
    POJ 1655 Balancing Act 树的重心
    POJ 3140 Contestants Division 树形DP
    HDU 3586 Information Disturbing 树形DP+二分
    HDU 1561 The more, The Better 树形DP
  • 原文地址:https://www.cnblogs.com/tv151579/p/2870623.html
Copyright © 2011-2022 走看看