Easily configure log4j to implement error log email notification

Source: Internet
Author: User
Tags log4j
Reprint: http://www.blogjava.net/sxyx2008/

Requirements Description: Any program will have bugs, although the project after repeated testing, has been running the line, but will inevitably encounter a variety of errors, here easily configure log4j to implement error message email notification.
Two files: Web.xml log4j.properties
Xml

Add the following code to the Web.xml

<!--setting Context parameters-->
    <context-param>
        <!--log4j configuration file location-->
        <param-name> Log4jconfiglocation</param-name>
        <param-value>/web-inf/classes/log4j.properties</ param-value>
    </context-param>
    <context-param>
        <param-name> log4jrefreshinterval</param-name>
        <param-value>6000</param-value>
    </context-param >
    <!--log4j listener-->
    <listener>
        <listener-class> Org.springframework.web.util.log4jconfiglistener</listener-class>
    </listener>

Log4j.properties

# # ROOT Log4j.rootlogger=info,console,mail # # CONSOLE Log4j.appender.console=org.apache.log4j.consoleappender Log4j.appender.threshold=info Log4j.appender.console.target=system.out log4j.appender.console.layout= Org.apache.log4j.PatternLayout #log4j. Appender.console.layout.conversionpattern=[framework]%d-%c-%-4r [%t]%-5p%c %x-%m%n log4j.appender.console.layout.conversionpattern=[\u8c03\u8bd5\u4fe1\u606f]%-5p%c-%m%n # # File Log4j.appe NDEr. A_default=org.apache.log4j.rollingfileappender Log4j.appender.A_default. Threshold=info Log4j.appender.A_default. File=e\:/logs/log4j.log Log4j.appender.A_default. MAXFILESIZE=4000KB Log4j.appender.A_default. maxbackupindex=10 Log4j.appender.a_default.layout=org.apache.log4j.patternlayout log4j.appender.A_ default.layout.conversionpattern=[\u8c03\u8bd5\u4fe1\u606f]%-5p%c%x-%m%n # # MAIL log4j.appender.mail=
Org.apache.log4j.net.SMTPAppender # Log error level Log4j.appender.mail.threshold=error # cache file size, send email when log reaches 512K Log4j.appender.MAIL.BufferSiZE=10 # Send mail to the server log4j.appender.mail.smtphost=smtp.163.com # message header log4j.appender.mail.subject=\u6d4b\u8bd5log4j\
U8f93\u51fa\u9519\u8bef\u4fe1\u606f\u5230\u65e5\u5fd7 # User Name log4j.appender.mail.smtpusername= your username # password
log4j.appender.mail.smtppassword= your password # sender address Log4j.appender.mail.from=xxxxx@163.com # The recipient of the log message log4j.appender.mail.to=xxx@qq.com # log Patternlayout log4j.appender.mail.layout=com.quartz.demo.loger4jhtmllayout #
    
    
 Format of log Log4j.appender.mail.layout.conversionpattern=[errormessage]%d-%c-%-4r [%t]%-5p%c%x-%m%n

Note: You have configured your own custom layout in the above log4j.properties file, because log4j is the default plain text method. This is not convenient for us to view in the mail, so cover it's layout, custom belongs to our own layout
Custom class Com.quartz.demo.Loger4JHTMLLayOut Complete this feature

Package Com.quartz.demo;

Import Org.apache.log4j.HTMLLayout;

public class Loger4jhtmllayout extends htmllayout{
    
    @Override public
    String getContentType () {return
        text /html;charset=utf-8 "; 
    }
}

That is, simply set contenttype as text/html
Effect Chart:

Log4j.properties

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.