Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure and use log4j

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to configure and use log4j, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Log4j has three main components, which are:

* Logger (recorder): responsible for filtering log information according to the set priority, and then forwarding it to the memory

* Appender (memory): responsible for receiving and processing log information forwarded by the recorder, usually outputting to the screen or storing to a disk file

* Layout (scheduler): responsible for formatting log information

The relationship between the above three:

Multiple Appender can be attached to a Logger (log messages are forwarded to multiple devices at the same time)

An Appender specifies a Layout for formatting

1. Define the configuration file log4j.properties

1.

Log4j. Logger name = [level], memory name 1, memory name 2, …

The order of priority from high to low is OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL

Log4j recommends using only four levels, with priority from high to low: ERROR, WARN, INFO, DEBUG

Only log information with a priority higher than or equal to the set level is forwarded, for example, if it is defined as INFO, then log information with DEBUG level in the program will not be output.

Log4j.rootLogger is the parent of all loggers (the original name rootCategory is obsolete) and can be used to set the default priority for all loggers.

Recorders can be inherited, such as log4j.myLogger.childLogger=,file

The recorder is obtained by name and is automatically created on the first access (always present)

If it is agreed that each class will only output logs to its logger with the same name, the log can be configured for each specific class in the configuration file

two。

Log4j.appender. Memory name = memory class name

Currently, there are several kinds of memory classes implemented by log4j:

Org.apache.log4j.ConsoleAppender (console)

Org.apache.log4j.FileAppender (file)

Org.apache.log4j.DailyRollingFileAppender (generates a log file every day)

Org.apache.log4j.RollingFileAppender (generates a new file when the file size reaches the specified size)

Org.apache.log4j.WriterAppender (sends log information to any specified place in stream format)

3.

Log4j.appender. Repository name .layout = scheduler class name

Currently, there are several types of scheduler classes implemented by log4j:

Org.apache.log4j.PatternLayout (log4j's standard scheduler, which can flexibly specify layout modes)

Org.apache.log4j.HTMLLayout (layout in HTML form)

Org.apache.log4j.SimpleLayout (a simple string containing level and log information)

Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc., when the log was generated)

4.

Set the layout options (mainly to set the conversion format)

Log4j.appender. Memory name. Layout. Option name = value

Log4j's standard scheduler is usually used:

Log4j.appender. Memory name. Layout=org.apache.log4j.PatternLayout

Log4j.appender. Memory name .layout.ConversionPattern = formatting parameter

Log4j formats log information with formatting parameters similar to those of the printf function in C language. The parameters are as follows:

% m outputs the information specified in the code

% p output priority, i.e. DEBUG,INFO,WARN,ERROR,FATAL

% r outputs the number of milliseconds elapsed from the scheduler construction to that point in time

% c outputs the category to which it belongs, which is usually the full name of the class

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

% n outputs a carriage return newline character, Windows platform is "/ rzone", Unix platform is "/ n"

D outputs the date or time of the point in time, the default format is ISO8601, or you can specify the format later. For example:% d {yyy MMM dd HH:mm:ss,SSS}, output class

Like: 08 December 03 12 purl 04purl 52531

% l outputs where the log event occurs, including the class name, the thread that occurred, and the number of lines in the code. Such as: Testlog4.main (TestLog4.java:10)

% F output file name

% L output line number

Second, call log4j in the code

1. Import log4j's jar package (log4j-1.2.15.jar) into the project.

two。 Write the configuration file log4j.properties, put it in the src directory, and copy it to the bin directory automatically when the system is running.

1. 2. Import org.apache.log4j.Logger; 3. 4.private static final Logger log = Logger.getLogger (class name. Class); 5. 6. Log.debug (Object message); 7. Log.info (Object message); 8. Log.warn (Object message); 9. Log.error (Object message); 10. 11. Import org.apache.log4j.Logger;. Private static final Logger log = Logger.getLogger (class name. Class); Log.debug (Object message); log.info (Object message); log.warn (Object message); log.error (Object message)

Third, the example of log4j.properties

1.

< span style ="font-family: courier new,courier;" >

2.# Set root logger priority to INFO and its only appender to CONSOLE. 3.log4j.rootLogger = INFO, CONSOLE 4.log4j.rootLogger = INFO, CONSOLE, LOGFILE 5.6.# Set the enterprise logger category to FATAL and its only appender to CONSOLE. 7.log4j.logger.org.apache.axis.enterprise = FATAL, CONSOLE 8.9.# CONSOLE is set to be a ConsoleAppender using a PatternLayout. 10.log4j.appender.CONSOLE = org .apache.log4j.ConsoleAppender 11.log4j.appender.CONSOLE.Threshold = INFO 12.log4j.appender.CONSOLE.layout = org .apache.log4j.PatternLayout 13.14.# Pattern to output the caller's file name and line number. 15.log4j.appender.CONSOLE.layout.ConversionPattern =% 5p [% t] (% FRV% L) -% m% n 16. 17.# LOGFILE is set to be a File appender using a PatternLayout. 18.log4j.appender.LOGFILE = org .apache.log4j.FileAppender 19.log4j.appender.LOGFILE.File = axis .log 20.log4j.appender.LOGFILE.Append = true 21.log4j.appender.LOGFILE.Threshold = INFO 22.log4j.appender.LOGFILE.layout = org .apache.log4j.PatternLayout 23.log4j.appender.LOGFILE.layout.ConversionPattern =%-4r [% t]%-5p% c% x -% m% n 24.

# Set root logger priority to INFO and its only appender to CONSOLE.

Log4j.rootLogger=INFO, CONSOLE

Log4j.rootLogger=INFO, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.

Log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.

Log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

Log4j.appender.CONSOLE.Threshold=INFO

Log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.

Log4j.appender.CONSOLE.layout.ConversionPattern=%5p [% t] (% FRV% L) -% m% n

# LOGFILE is set to be a File appender using a PatternLayout.

Log4j.appender.LOGFILE=org.apache.log4j.FileAppender

Log4j.appender.LOGFILE.File=axis.log

Log4j.appender.LOGFILE.Append=true

Log4j.appender.LOGFILE.Threshold=INFO

Log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout

Log4j.appender.LOGFILE.layout.ConversionPattern=%-4r [% t]%-5p% c% x -% m% n

IV. Common failures

Log4j:WARN Please initialize the log4j system properly.

Solution:

[eclipse environment] copy the file log4j.properties to the src directory, and the runtime will automatically copy it to the bin directory. You may need Refresh before.

Example:

Log4j.rootLogger=INFO, cons MyAppenderlog4j.appender.syslog.encoding=UTF-8log4j.appender.cons=org.apache.log4j.ConsoleAppenderlog4j.appender.cons.layout=org.apache.log4j.SimpleLayout# logger log4j.appender.MyAppender=org.apache.log4j.RollingFileAppenderlog4j.appender.MyAppender.File=/system.log#log4j.appender.MyAppender.encoding=UTF-8log4j.appender.MyAppender.MaxFileSize = 2048KBlog4j.appender.MyAppender.MaxBackupIndex = 20log4j.appender.MyAppender.layout=org.apache.log4j.PatternLayout#log4j.appender.MyAppender.layout.ConversionPattern=%d (% FGV% L)%-5p- ->% m%nlog4j.appender.MyAppender.layout.ConversionPattern=%d {yyyy-MM-dd HH:mm:ss}% p% t% c -% m%n# Information logger log4j.additivity.MsgLogger = falselog4j.logger.MsgLogger=INFO MsgAppenderlog4j.appender.MsgAppender=org.apache.log4j.RollingFileAppender#log4j.appender.MsgAppender.encoding=UTF-8log4j.appender.MsgAppender.File=/msg.loglog4j.appender.MsgAppender.MaxFileSize = 2048KBlog4j.appender.MsgAppender.MaxBackupIndex = 20log4j.appender.MsgAppender.layout=org.apache.log4j.PatternLayoutlog4j.appender.MsgAppender.layout.ConversionPattern=%d {yyyy-MM-dd HH:mm:ss}% m%n# numeric logger log4j.additivity.CollectValueLogger = falselog4j.logger.CollectValueLogger=INFO CollectValueAppenderlog4j.appender.CollectValueAppender=org.apache.log4j.RollingFileAppender#log4j.appender.CollectValueAppender.encoding=UTF-8log4j.appender.CollectValueAppender.File=/value.loglog4j.appender.CollectValueAppender.MaxFileSize = 20480KBlog4j.appender.CollectValueAppender.MaxBackupIndex = 20log4j.appender.CollectValueAppender.layout=org.apache.log4j.PatternLayoutlog4j.appender.CollectValueAppender.layout.ConversionPattern=%m%n Thank you for reading this article carefully I hope the article "how to configure and use log4j" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report