Customizing the log4j Appender

Source: Internet
Author: User
Custom log4j the Appender   Appender The life cycle   1.       Appender instance does not exist, perhaps the framework has not yet been configured well. The   2.       framework instantiates a new appender. This occurs when a Appender declaration is made in the Config class profiling configuration script. The Configurator class calls Class.newinstance (Yourcustomappender.class), which is equivalent to dynamically invoking the new Yourcustomappender (). The framework does this in order to avoid being hard-coded into any particular appender name; the framework is generic and applies to any appender. e.g. Log4j.appender.file=org.apache.log4j.rollingfileappender   3.       Framework judgment Whether the Appender needs layout. If the appender does not require layout, the Configurator does not attempt to load layout information from the configuration script. In the Appender interface, there is a public boolean requireslayout () method, and if return is true, the layout is required to set the layout information e.g in the configuration script. Log4j.appender.file.layout=org.apache.log4j.patternlayout   4.       log4j The Configurator invokes the setter method. This method is called by the framework after all properties have been set. At this point, each field corresponding to Rollingfileappender needs to have a setter method, which is also set e.g in the configuration script. Log4j.appender.file.file=<project>.log Corresponding in Rollingfileappender, there is a public void Setfile (string file) {    string val = File.trim ();   &n Bsp FileName = val;  Other properties, such as MaxFileSize, also call the Activateoptions () method in the same   5.       Configurator. This method is called by the framework after all properties have been set. The Optionhandler interface defines the Activateoptions () method, after all properties have been set, to perform the necessary actions in the method, such as opening the file e.g if (filename!= null) {       try {    setfile (fileName, Fileappend, Bufferedio, buffersize);     & nbsp }       catch (Java.io.IOException e) {    errorhandler.error ("Setfile (" + Filename+ "," +fileappend+ ") call failed.",                e, errorcode.file_open_failure);      }    } else {     //loglog.error ("File Option not set for Appender ["+name+"]. ");       loglog.warn ("File option not set for Appender [" +name+]. ");       Loglog.warn ("Are you using Fileappender instead of Consoleappender?"); }   6.&NBSP;&Nbsp;     Appender is ready. The framework can now invoke the Append () method to process logging requests. This method is called by the Appenderskeleton.doappend () method. This method is the most critical method in Appender, append () can define the output of the log, simplest: protected void Append (Loggingevent event) {   //if Layout is required     System.out.println (This.getlayout (). Format (event)); }   7.       Finally, close Appender. When the framework is about to delete your custom Appender instance, it calls your Appender close () method. Close () is a cleanup method that means you need to release all the resources that you have allocated. It is a required method and does not accept any parameters. It must set the Closed field to true and alert the frame when someone tries to use the closed appender. public void Close () {        if (this.closed)              return;         this.closed = true;    } Note: You need to invoke Logger.getroot (). Removeallappender () before the main program exits, so that you can invoke the Appender close () method.   to write a custom Appender 1. Extend Appenderskeleton abstract class. 2. Specify whether your appender requires layout. 3. If some properties must be activated at the same time, they should be done within the activateoptions () method. 4. Implement the Close () method. It must set the value of the Closed field to true. Remember to release all resources. 5. Optionally specify the default ErrorHandler object to use. The system defaults to Onlyonceerrorhandler, it sends out the first error message and ignores all remaining errors, and the error message is output to System.err. 6. Write code for the Append () method. This method is responsible for attaching logging events and for calling the error handler when an error occurs.

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.