log4j Custom Appender

Source: Internet
Author: User

The LOG4J environment consists of three main components:

Logger (Logger): controls which logging statements are enabled or disabled. The following levels can be set for the logger:

All, DEBUG, INFO, WARN, ERROR, FATA, OFF.

layout: formats logging requests According to the user's wishes.

Appender: sends formatted output to the destination. Log4j brought in also remember the Appender as follows:

    1. Consoleappender
    2. Fileappender
    3. Smtpappender
    4. Jdbcappender
    5. Jmsappender
    6. Nteventappender
    7. Syslogappender

You can also create a custom Appender

Gorgeous split-rich ***************************************** ***************************************

Create a custom Appender

1. Inherit the Appenderskeleton class and implement three methods ( you can also inherit existing Appender, override some methods, and implement the required logic)
 Public classSimpleappenderextendsAppenderskeleton {//Configure required properties in Log4j.xml, where you can injectPrivateString Developer; @Overrideprotected voidAppend (loggingevent loggingevent) {
Custom Output what System.out.println ("=========" + developer + "test===========" +loggingevent.getmessage ()); } @Override Public BooleanRequireslayout () {
Whether layout is required, or false, even if Log4j.xml is configured with layout does not take effect. return false; } @Override Public voidClose () {
Release resource this.closed = true; } PublicString Getdeveloper () {returndeveloper; } Public voidSetdeveloper (String developer) { This. Developer =developer; }
2. Configure Log4j.xml
 <!--  -->  <  appender  name  = "Simpleappender"   class  = "Com.cn.simple.logger.SimpleAppender"  >  <  param 
     name  = "developer"   = "Lqw"  ></ " Span style= "COLOR: #800000" >param  >   </ appender  >  
3. Implementation
        Logger.info ("[Springdemo]--------Info");        Logger.warn ("[Springdemo]--------warn reminder");        Logger.error ("[Springdemo]--------Error Exception");    
4. Output results
=========lqwtest===========[springdemo]--------info =========lqwtest===========[springdemo]-------- warn alert =========lqwtest===========[springdemo]--------Error exception

Reference Documentation:

Http://www.360doc.com/content/16/0121/16/14567867_529569233.shtml

Log output to frame reference:

http://zhangjunhd.blog.51cto.com/113473/48895/

log4j Custom Appender

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.