Some detailed references about Apache log4j

xiaoxiao2021-03-06  39

Log4j's simple configuration! LOG4J consists of three important components: the priority of log information, output destination of log information, and output format of log information. The priority of the log information is from high to Error, Warn, INFO, DEBUG, and is used to specify the importance of this log information. The output destination of log information specifies the log to print to the console or in the file; The format controls the display of log information.

1, define the configuration file

In fact, you can also use the profile at all, but configure the log4j environment in your code. However, using the profile will make your application more flexible.

Log4j supports two profile formats, one is a Java feature file (key = value), one is a file in XML format. Let's introduce the method of using the log4j configuration file:

Configure root Logger, whose syntax is:

Log4j.rootlogger = [level], appendername, appendername, ... where Level is the priority of log records, divided into OFF, Fatal, Error, Warn, INFO, DEBUG, ALL, or your defined level. 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.

Configure the log information output Destination Appender, its syntax is

log4j.appender.appenderName = fully.qualified.name.of.appender.classlog4j.appender.appenderName.option1 = value1 ... log4j.appender.appenderName.optionN = valueN wherein, appender Log4j provided are the following:

Org.apache.log4j.consoleappender org.apache.log4j.fileappender (file) org.apache.log4j.dailyRollingFileAppender (a log file is generated every day) org.apache.log4j.rollingFileAppender (file size arrives at the specified size) Generate a new file) Org.apache.log4j.writerappender (layout) of the log information (layout) in the format (layout) of the log information in the flow format to any specified place, whose syntax is:

log4j.appender.appenderName.layout = fully.qualified.name.of.layout.classlog4j.appender.appenderName.layout.option1 = value1 ... log4j.appender.appenderName.layout.optionN = valueN which, layout Log4j provided are the following Species:

Org.apache.log4j.htmlLayout (layout in HTML form) org.apache.log4j.patternLayout (can be flexibly specified) org.apache.log4j.simplelayout (including level and information string with log information) Org.apache .log4j.ttccLayout (time, thread, category, etc.) 2, using log4j under the code, how to use log4j in program code.

i, get the logger using log4j, the first step is to get the log recorder, which will be responsible for control log information. Its syntax is: public static logger getlogger (String name), obtains a logger by the specified name, and 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 ());

II, reading a configuration file After obtaining the log logger, the second step will configure the log4j environment, its syntax: 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.

III, inserting 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); IV, Code Example Note: Blue - File Name Red - Place of Log4j

Hello.java

import org.apache.log4j *;. public class Hello {static Logger logger = Logger.getLogger (Hello.class); public static void main (String argv []) {PropertyConfigurator.configure ( "Hello.properties"); logger. Debug ("Hello World."); Logger.info ("What a beatiful day."); Try {// If there is an exception throw new nullpointRexception ();} catch (exception e) {logger.warn ("NullPointersErexception ", e);}} Log4j.properties

log4j.rootLogger = debug, stdout, Rlog4j.appender.stdout = org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout = org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number.log4j.appender .stdout.Layout.conversionPattern =% 5P [% T] (% F:% L) -% m% n # log4j.log4j.r = org.apache.log4j.rollingfileAppenderlog4j.rapnder.r.file = example.loglog4j. appender.R.MaxFileSize = 100KB # Keep one backup filelog4j.appender.R.MaxBackupIndex = 1log4j.appender.R.layout = org.apache.log4j.PatternLayoutlog4j.appender.R.layout.ConversionPattern =% p% t% c - % M% NLOG4J.XML

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

New Post(0)