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
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 .
layout: Formats logging requests According to the user's wishes.
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
Extended Appenderskeleton abstract class.
Specify if your appender requires layout.
If some properties must be active at the same time, they should be done within the activateoptions () method.
implement the Close () method. It must set the value of the Closed field to true . Remember to release all resources.
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.
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
:http://www.apache.org/dyn/closer.cgi/logging/log4j
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