Explain log4j Simple configuration and use of properties

This paper introduces log4j The simple configuration and use of properties are shared with you as follows:

Simple log4j Properties configuration example

Java code part

The log4j jar package is required

Log4j supports two formats of configuration files: XML and properties; Rely on Commons logging package

1. Configure root logger

log4j. rootLogger=[level],appenderName,......

Level can be divided into off, fatal, error, warn, info, debug and all. If off is configured, no information will be printed. The priority is: error > warn > info > debug, which is case insensitive

Subclasses can be added, for example:

log4j. category. org. nutz=INFO,nutz

log4j. category. com. gtz=DEBUG,gtz

2. Configure the appender for log information output

There are 4 output forms:

org. apache. log4j. Consoleappender

org. apache. log4j. Fileappender (file)

org. apache. log4j. Daily rollingfileappender (one log file is generated every day)

org. apache. log4j. Rollingfileappender (a new file is generated when the file size reaches the specified size)

You can use log4j appender. appenderName. Maxfilesize = 100kb set file size

You can also use log4j appender. appenderName. Maxbackupindex = 1 is set to save a backup file.

org. apache. log4j. Writerappender (send log information in stream format to any specified place)

3. Configure the format (layout) of log information

Layout is responsible for formatting the output of the appender. The syntax is:

Among them, log4j provides the following layouts:

org. apache. log4j. Htmllayout (layout in HTML table form)

org. apache. log4j. Patternlayout (layout mode can be specified flexibly)

org. apache. log4j. Simplelayout (level and information string containing log information)

org. apache. log4j. Ttcclayout (including log generation time, thread, category, etc.)

4. Format log information

log4j. appender. appenderName. layout. ConversionPattern=%d [%t] %-5p %c - %m%n

%M output the message specified in the code

%P output priority, i.e. debug, info, warn, error, fatal

%R input the number of milliseconds from application startup to output the log information

%C output the category to which it belongs, usually the full name of the class

%T outputs the name of the thread that generated the log event

%N outputs a carriage return line feed, which is "RN" for Windows platform and "n" for UNIX platform

%D output the date or time of the log time point. The default format is iso8601. You can also specify the format after it, such as:% d {yyyy MMM DD HH: mm: SS, SSS}. The output is similar to 22:10:28921 on October 18, 2002

%L output the location of the log event, including the category name, the thread that occurred, and the number of lines in the code.

Logger class: complete logging and set log information level

Appender class: determines the log destination, including terminal, DB and hard disk

Layout class: determines the style of log output, such as the current thread, line number, and time

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>