Use log4j to join the log in the program

xiaoxiao2021-03-06  38

Learn the log4j online, the following is some simple learning note Apache's open source project log4j is a powerful log component, providing fast and effective logging, developers can add very much to their own programs. Practical log function. In Apache website: http://jakarta.apache.org/log4j You can download the latest version of the LOG4J for free, decompressed, copied the Log4j-1.x.x.jar files that packaged into the ClassPath-related directory.

First, quickly experience OK, now you can experience the functionality of log4j, follow these steps to quickly join the log functionality in the program. See the simple example below: Step 1: Import Log4j Use of IMPORT Related class second step: Define A log logger logger's object Step 3: Load log4j configuration file, or use default Configuration of Environment Step 4: You can now want to output a log in your program, using the following methods provided by the Logger class, insert different priority logs. Debug (Object Message); Info (Object Message); Warn (Object Message); Error (Object Message);

Datual! Is it very simple? It can fully replace the statement such as system.out.println ("xxxxxxxxxxxx" previously used to debug the program, and log4j can flexibly control the level of log output by custom profile. ###################################################################

/ ** Import Log4j Related Class * / Import Org.Apache.log4j.logger; import org.apache.log4j.basicconfigurator;

/ ** *

Title: log4j example *

Description: Quick use log4j *

Copyright: Copyright (C) 2004 * @Author Robin * @version 1.0 * / public class hellolog4j {

// Define the logging logger, all logs are listed by it, static logger logger = logger.getlogger (Hellolog4j.class.getClass ());

Public static void main (string [] args) {// uses the default environment to configure log4j baricconfigurator.configure (); // to configure log4j //propertyconfigurator.configure ("d: //testlog4j.properties) ;

// Insert the log information logger.info ("HelloLog4j start"); try {hellog4j hellolog4j1 = new hellolog4j (); int count = hellolog4j1.count (1, -32); logger.debug ("count of count:" count);} catch (exception e) {logger.Error ("error" E.GetMessage ());} logger.info ("HelloG4J execution");} / ** * Seeks two intensive sum * * / Public int count (int A, int b) {int Add = a b; if (add <0) {logger.warn ("two numbers less than zero");} return add;}} ### ####################### two, configuring log4j We can define profiles by custom definition To dynamically control the output of the log. Log4j supports two configuration file formats, one is a file in XML format, one is a Java feature file (key = value). You can load the configuration file in the following ways: 1, load the configuration file written by using the Java key-value-paid properties file. PropertyConfigurator.configure (String ConfigfileName) 2, loaded into the configuration file Domconfigurator.configure (String FileName).

Look at a simple Java property profile testlog4j.properties :##################################################################################################################################################################################################################################################################################### #### Specify root logger, and log output level, greater than or equal to this level of log will be output (Debug

# Also specify the log level of the output of a package # log4j.logger.com.Study.Hellolog4j = info ########################################################################################################################################################################################################################################################### ###############

1. Configure the root Logger, its syntax is: log4j.rootlogger = [level], appendername, appendername2level: The level of the log information, specifies the importance of this log information. Divided into all

When using org.apache.log4j.patternLayout from defining information format, you can use log4j.Appender.a1.Layout.conversionPattern =% D {YYYY-MM-DD HH: MM: SS}% P -% M% N to format The information% C outputs the full name of the class belong, written as% c {num}, and the NUM class name is output, such as "com.sun.aaa.classb",% C {2} will make the log output output range : AAA.CLASSB% D Output Log Time Format The format is specifiable format as% D {HH: mm: SS} and other% L output log event occurs, including category name, thread, number of rows in the code% N Removal% m Output code specifying information, such as INFO ("Message"), outputs the priority of the Message% P output log, that is, Fatal, Error and other% R output from start to display the time of the log information (millisecond number) )% T outputs thread names that generate this log event more detailed parameter information can be found in org.apache.log4j.patternlayout API Doc document About performance: Joining logs always bring some performance losses, of course, most cases can be ignored. 1. When a level log is shielded, for example, the configuration level is INFO, then the DEBUG level log statement in the program will be disabled, such as: logger.debug ("Entry Number:" i "IS" String.Valueof (entry [i])); in the above statement, although the message in the debug statement does not output into the log, the operation of the string structure in the message will still be executed, when the operation is more complicated, sometimes it will bring Some performance losses can be changed to the Word: if (Logger.DebugNabled () {Logger.debug ("Entry Number:" i "IS" String.Valueof (entry [i]));} Once the debug level It is forbidden to output, the string operation behind log information is not executed. 2. Since the logger logger, support the named hierarchy, there may be some performance when searching in the sub-hierarchy, and there may be some performance. loss.

In short, the log4j's design goal has been fully considering the speed priority. After many rewritments, performance issues are almost all completely not a problem. It is a very good tool component that uses the benefits of it, which is much better than the lost point performance loss. It can be used in bold use.

转载请注明原文地址:https://www.9cbs.com/read-66308.html

New Post(0)