The initial desire to invoke an external interface to record audit information while executing a business logic has not been able to find a more elegant way, and it has been discussed that log4j's custom appender may be possible to implement this function. Then I learned about this part of log4j.
Apache log4j Architecture
Apache log4j is currently the most used log framework in Java EE and J2SE development (it is used in almost all projects) because of its excellent performance, flexible configuration, and rich functionality, and when the business has special requirements, you can use custom components instead of the components already in the framework to meet the requirements.
log4j Component Introduction
The log4j consists of three components:
- Logger: Responsible for client code invocation, execution of debug (object msg), info (object msg), Warn (object msg), error (Object msg), and other methods.
- Appender: Responsible for the output of the log, LOG4J has implemented a variety of different targets of the output mode, you can output the log to the file, to the console output log, to the socket output log.
- Layout: Responsible for the formatting of log information.
< Span class= "Apple-converted-space" >
The
Logger hierarchy describes &NBSP;
Logger is specified by Logger name, such as x.y for two tiers, X and y layers, X is the parent of Y, and x.y is at the y level &NBSP;
Log4j.additivity.* = false: Indicates that the current logger does not need to hit the Appender specified by the parent level, only the current Appender;&NBSP;
Default true: Indicates that the current logger will print the log to the current Appender and all parent levels specified by Appender
Layout has a variety of &NBSP;
the most commonly used and most flexible output formats are: Org.apache.log4j.patternlayout&NBSP;
can be configured in combination with the following: &NBSP;
- %c The full name of the logger namespace, if added with the {< layer, which represents the namespace of the specified number of layers from the most inner layer.
- %c The full name of the class that called logger, including the package path.
- %d Logging time,{< date format, using the date format defined by ISO8601. The
- %f invokes the source file name of logger. The
- %l where the log event occurred, including the name of the class, the thread that occurred, and the number of rows in the code. The
- %l calls the logger code line%m output message. The
- %m invokes the method name of logger.
- %n The line break under the current platform. The
- %p the priority of the log.
- %r The number of milliseconds that have elapsed since the start of the program to record the log. The
- %t the name of the thread that generated the log event.
- %x Output The logs in the Order of NDC (Nested Diagnostic Context, thread stack).
- %x Output the log by MDC (Mapped Diagnostic Context, thread map table). Typically used for multiple clients to connect to the same server, which makes it easier for the server to differentiate between the logs that client access leaves behind. The
- %% displays a percent semicolon. )
execution order and relationship
The order in which the individual components are called when the log4j output log is called:
- 1, log information into the Logger.
- 2, the log information is encapsulated into a Loggingevent object and passed into the Appender.
- 3, in the Appender call filter to filter the log information, call Layout to format the log information, and then output.
Diagram: &NBSP
Implement custom log4j Appender &NBSP;
Understand the structure of the log4j of the implementation of the custom log4j Appender will be solved &NBSP;
Inherit log4j common base class: Appenderskeleton&NBSP;
Print Log core method: Abstract protected void Append (Loggingevent event); &NBSP;
Initialize load resource: public void Activateoptions (), default implementation is empty &NBSP;
Frees resources: public void Close () &NBSP;
Whether you need to output text in the format: public boolean requireslayout () &NBSP;
normally we just need to override the Append method. Then you can use the in log4j,
Demo code: &NBSP;
Java code
- Import Org.apache.log4j.AppenderSkeleton;
- Import org.apache.log4j.spi.LoggingEvent;
- Public class Helloappender extends Appenderskeleton {
- private String account;
- @Override
- protected void Append (Loggingevent event) {
- System.out.println ("Hello," + Account + ":" + event.getmessage ());
- }
- @Override
- public Void Close () {
- //TODO auto-generated method stub
- }
- @Override
- Public Boolean requireslayout () {
- //TODO auto-generated method stub
- return false;
- }
- Public String Getaccount () {
- return account;
- }
- public void Setaccount (String account) {
- this.account = account;
- }
- }
Java code
- Public static void Main (string[] args) {
- Log log = Logfactory.getlog ("Hellolog");
- Log.info ("I am ready.");
- }
Referencing the log4j.properties configuration&NBSP;
Log4j.logger.hellolog=info, hello &NBSP;
Log4j.appender.hello=helloappender &NBSP;
Log4j.appender.hello.account=world
Execute main function, output
Hello, world:i am ready.
/span>
Log4j Custom Appender Introduction