Java:apache log4j Concise tutorial (i)

Source: Internet
Author: User

The official introduction of Apache log4j is "log4j are a reliable, fast and flexible logging Framework (APIS) written in Java, which are distributed u NDEr the Apache software License. Log4j is highly configurable through external configuration files at runtime. It views the logging process in terms of levels of priorities and offers mechanisms to direct logging information to a GRE At variety of destinations, such as a database, file, console, UNIX Syslog, etc. ", translated is: Log4j is based on Apache software license release A reliable, fast, and extensible logging framework. The log4j is highly configurable and can be configured through external files during the run. Log4j can filter logs based on different levels of priority, and provides a mechanism to write log records directly to the console, files, databases, UNIX system logs, and so on.

  The above official introduction, not only to the definition of log4j, but also to log4j characteristics of the performance of the elaboration. Log4j mainly consists of logger (recorder), Appender (output source), layout (layout mode) three parts, wherein logger is used to capture log information, layout is used to format the log information, Appender used to define the output source of the log. The specific architecture can participate in:

  LOG4J is installed in the same way as adding a common. Jar class library file, you can download the relevant package to the official website (http://logging.apache.org/log4j/2.x/), and the difference is log4j need to add the above mentioned configuration file.

So how should the configuration file be written?

log4j configuration files, all called "Log4j.properties", save parameters in the form of key-value, or you can write configuration parameters in the form of an XML document. in General, Log4j.properties needs to include the filtering level, log directory, log file name, output source, the detailed implementation class of the output source, layout mode, and the corresponding formatting parameters and other configuration information, details as follows:

Filter Level: All,trace,debug,info,warn,error,fatal

Output Source: File,db,console, The corresponding detailed implementation class is Org.apache.log4j.fileappender,org.apache.log4j.jdbcappender,org.apache.log4j.consoleappender.

Layout mode: Patternlayout,htmllayout, where the corresponding format parameter of patternlayout (%[]) is the following table:

C Outputs the fully qualified name of the class that issued the logging request, such as org.example.demo.Test;
D Outputs the date on which the logging event occurred, such as%D{HH:MM:SS};
M Outputs the information of the application associated with the logging event;
M Output the name of the method that issued the logging request;
N Output line breaks associated with the system platform;
P The filtering level of the output log;
X Outputs the IP of the client that issued the logging request, such as X{clientip};

  A specific example using the Key-value form is as follows:

 # Define the root logger and the directory of log file.logdir  = E:/templog4j.rootlogger  = DEBUG, file# Define the FILE Appenderlog4j.appender.FILE  = Org.apache.log4j.FileAppenderlog4j.appender.FILE.File  = ${logdir}/< Span style= "color: #000000;" >log.txt# Define The layout   the file Appenderlog4j.appender.FILE.layout  = Org.apache.log4j.PatternLayoutlog4j.appender.FILE.layout.conversionPattern  =%m%n 

  The log4j.properties configuration file is divided into three parts, the first part The log file is configured to save directory LogDir, while the logging filtering level is set to debug, the output source is file; The second part configures the output source's detailed implementation class Org.apache.log4j.FileAppender, the log document name Log.txt; The third part specifies the layout mode of the log document . Patternlayout , and detailed formatting parameters%m%n.

An example of the above configuration file is written in XML as follows:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE log4j:configuration SYSTEM "Log4j.dtd"><log4j:configuration><Appendername= "FILE"class= "Org.apache.log4j.FileAppender">   <paramname= "File"value= "E:temp/log.txt"/>   <paramname= "Immediateflush"value= "true"/>   <paramname= "threshold"value= "Debug"/>   <paramname= "Append"value= "false"/>      <Layoutclass= "Org.apache.log4j.PatternLayout">      <paramname= "Conversionpattern"value= "%m%n"/>   </Layout></Appender><Loggername= "Log4j.rootlogger"additivity= "false">   < Levelvalue= "DEBUG"/>   <Appender-refref= "FILE"/></Logger></log4j:configuration>

After configuring Log4j.properties, is it possible to write a Java program to run?

In fact, there is no end to the configuration of log4j here, and you need to determine where the log4j.properties file should be placed. The official introduction says that Log4j.properties should be placed in the directory of path or classpath, but if you do this, you will easily create a naming conflict if you encounter each Java project that requires a custom profile problem. The appropriate approach is to associate the log4j.properties with a specific Java project and place the log4j.properties in the SRC package. Or a more recommended approach: Create a new config directory under the Java Project root > Place log4j.properties in the Config directory > right-click the config directory > select build Path > click Use as Source Folder.

Now it's time to write a Java class and run it, with an example like this:

1  PackageOrg.warnier.zhang.demo;2 3 ImportOrg.apache.log4j.Logger;4 5  Public classTest {6     /**7 * acquisition of recorders;8      */9     StaticLogger Logger = Logger.getlogger (Test.class);Ten      One      Public Static voidMain (string[] args) { ALogger.debug ("Debug > Hello world!"); -Logger.info ("Info > Hello world!"); -     } the  -}

Here, the Apache log4j Concise tutorial (a) is over, and the next one will explain how to output logs to the MySQL database. Hopefully the tutorial will help a troubled reader. Of course, due to the author's limited level, if there are errors, readers are welcome to exchange messages.

Java:apache log4j Concise tutorial (i)

Related Article

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.