log4j Custom Appender

Source: Internet
Author: User

1. Overview

emphasizing the development of reusable components today, in addition to developing a reusable log operations class from start to finish, Apache provides us with a powerful log operation package -log4j.

log4j is an Apache open source project, and by using log4j, we can control the destination of log information delivery to the console, files,GUI components, even the socket server, NT Event Recorder,UNIX Syslog Daemon, etc. we can also control the output format of each log, and by defining the level of each log information, we can control the log generation process more carefully. Most interesting of all, these can be configured flexibly with a single configuration file, without the need to modify the code of the application.

2.log4j Extension2.1. main components
    1. Logger(Logger): Controls which logging statements are enabled or disabled. You can specify the following levels for the logger: all , DEBUG , INFO , WARN , ERROR, FATA, or OFF .

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

    3. Appender: Sends formatted output to the destination.

2.2. Built-in Appender

l Consoleappender (console)

l Fileappender (file)

l Dailyrollingfileappender (Generate a log file every day)

l Rollingfileappender (creates a new file when the file size reaches the specified size)

l Writerappender (send log information in stream format to any specified location)

2.3. Extension steps
  1. Extended Appenderskeleton abstract class.

  2. Specify if your appender requires layout.

  3. If some properties must be active 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 that you want to use The ErrorHandler object. The system defaults to Onlyonceerrorhandler, which sends out the first error message and ignores any remaining errors , and the error message is output to system.err.

  6. Code that writes the Append () method. This method is responsible for attaching the logging event and is responsible for invoking the error handler when the error occurs.

3. A simple example3.1. Download Resources
    1. :http://www.apache.org/dyn/closer.cgi/logging/log4j

    2. Load the jar package into the Class Path:


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4C/E9/wKiom1RHWMOBKb-FAABvU3hGggc897.jpg "title=" ss.png "alt=" Wkiom1rhwmobkb-faabvu3hgggc897.jpg "/>

3.2.Java Code

Package Com.aeai.appender;

Import Org.apache.log4j.AppenderSkeleton;

Import org.apache.log4j.spi.LoggingEvent;

Public class Logui extends appenderskeleton{

private int maxSize;

@Override

protected void append (loggingevent event) {

System. out . println ("percent"+ event.getmessage () +"percent maxSize:" + maxSize );

}

@Override

public void Close () {}

@Override

public boolean requireslayout () {

return false;

}

public int getmaxsize () {

return maxSize;

}

public void setmaxsize (int maxSize) {

this. maxSize = maxSize;

}

}

3.3.log4j configuration file


# Set Root Logger level to error

log4j.rootlogger= INFO, Customer, File

###### Customer appender Definition #######

log4j.appender.customer= Com.aeai.appender.logUI

log4j.appender.customer.maxsize= -

###### File appender Definition #######

log4j.appender.file= Org.apache.log4j.RollingFileAppender

log4j.appender.file.file= Logs/log.log

log4j.appender.file.append= true

log4j.appender.file.maxfilesize= 500KB

log4j.appender.file.layout= Org.apache.log4j.PatternLayout

log4j.appender.file.layout.conversionpattern= %d{yyyy-mm-ddHH:mm:ss, SSS} %-5p %m%n   

3.4. Testing

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4C/E9/wKiom1RHWdvh0Hv6AAFa_kSSuRA840.jpg "title=" ff.png "alt=" Wkiom1rhwdvh0hv6aafa_kssura840.jpg "/>


This article is from the "aeaiportal" blog, make sure to keep this source http://aeaiportal.blog.51cto.com/9300497/1566718

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.