Java learning - -log4j configuration file

zhaozj2021-02-16  95

During writing the program, it is found that the output is a very big thing. If you want to output it to the screen with system.out.println (""); if you want to output it in the file, you will usually develop a class to do it. . Such a bad place is that the amount of code is too large. On the one hand, it is not convenient to modify the code if you want to shield the output terminal, or there is a lot of debug code when you have to endure it. Full screen word, I don't know where it is what I want, it is really a big problem.

However, log4j solves this problem. Let's take a look at the configuration file. Log4j is mainly a configuration file. When writing the program, you generally declare such a logger, then write the log when you need it. By modifying the configuration file, you can determine where the log information is output (console, file, ...), whether it is output. In this way, the detailed log information can be printed in the system development phase to track the system operation, and the log output can be turned off after the system is stable, so that the garbage code is reduced while being able to track the system operation (System.Out.println. ...)Wait). Using log4j, you need a unified log mechanism throughout the system to facilitate system planning.

Log4j's use itself is simple. However, it is reasonable to plan a system's unified log mechanism that requires a comprehensive consideration. And you can output it into multiple files, mainly configured. Then you can control the output places of the log.

Profile detailed explanation First look at an example of a configuration file:

Among them, Level is the priority of logging, divided into OFF, Fatal, Error, Warn, Info, Debug, All, or level you defined. Log4J is recommended to use only four levels, priority from high to low, Error, Warn, Info, Debug. By the level defined here, you can control the switch to the corresponding level of log information in the application. For example, the INFO level is defined here, and all the DEBUG level log information in the application will not be printed. Appendername is where the specified log information is output to which place. You can specify multiple output destinations at the same time. Log4j.rootlogger = debug <- This is root log, this must be configured, if there is no specified in the code, all use this configuration

# Record the DAO layer log to DAOLOG, ALLOG log4j.logger.dao = debug, A2, A4 <- here is the second log, using Logger.getLogger ("DAO") in the code to specify it.

# Record the logical layer log to businesslog, alwork log4j.logger.businessLog = Debug, A3, A4 <---- this is the third day

# A1 - Print to the screen <----- The following is the output configuration, you can specify the screen, file, database, socket port, etc. Log4j.Appender.a1 = org.apache.log4j.consoleAppenderlog4j.Appender.a1 . retog4j.patternLayoutLog4j.PatternLayoutLog4j.Appender.a1.Layout.conversionPattern =% - 5P [% T]% 37C% 3X -% M% N

# A2 - Print to Document DAOLOG - Specially served for DAO Layer Log4j.Appender.a2 = org.apache.log4j.dailyRollingFileAppenderlog4j.Appender.a2.a2.datePattern = '.' YYYY-mm -ddlog4j.appender.a2.Layout = org.apache.log4j.patternLayoutLog4j.Appender.a2.Layout.conversionPattern = [% - 5P]% D {YYYY-MM-DD HH: MM: SS, SSS} Method:% L % N% M% N # A3 - Print to File Businesslog - Specialized Logic Processing Layer Service LOG Information Log4j.Appender.a3 = Org.Apache.log4j.dailyRollingFileAppenderlog4j.Appender.a3.a3.a3.Appender.a3.a3 .Datepattern = '.' YYYY-mm-ddlog4j.appender.a3.Layout = org.apache.log4j.patternlayoutlog4j.Appender.a3.Layout.conversionPattern = [% - 5P]% d {YYYY-MM-DD HH: mm : SS, SSS} Method:% L% N% M% N

# A4 - Print to File AllLog - Record all log information log4j.Appender.a4 = org.apache.log4j.dailyrollingFileAppenderlog4j.Appender.a4.file = allLoglog4j.Appender.a4.datepattern = '.' YYYY-mm- DDLog4j.Appender.a4.Layout = org.apache.log4j.patternLayoutLog4j.Appender.a4.Layout.conversionPattern = [% - 5P]% d {YYYY-MM-DD HH: mm: SS, SSS} Method:% l% N% M% N

Configuration is actually relatively simple, and if you want to change, you can modify this configuration, don't modify the code, convenient. Save a lot of typing time, and repeated system.out time.

In the log4j, it is very detailed about the format.

Used in the program

Get recorder

Using log4j, the first step is to obtain a logging device, which will be responsible for control log information. Its syntax is:

Public Static Logger getlogger (String name),

Get the recorder by the specified name, if necessary, create a new recorder for this name. Name generally takes the name of this class, such as:

Static logger logger = logger.getlogger (ServerWithlog4j.class.getname ());

Read configuration file

After the log recorder is obtained, the second step will configure the log4j environment, whose syntax is: BasicConfigurator.configure (): Automatically quickly uses the default Log4j environment. PropertyConfigurator.configure (String configFileName): Read the configuration file written by using the characteristic file using Java. Domconfigurator.configure (String FileName): Read the configuration file in the XML form.

Insert record information (format log information)

When the top two necessary steps are performed, you can easily use different priority logging statements into any place you want to log log, whose syntax is as follows:

Logger.debug (Object Message); Logger.info (Object Message); Logger.Warn (Object Message); Logger.Error (Object Message);

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

New Post(0)