In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about how to understand the meaning of the code in the Log4j configuration file. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
Through Log4j, you can easily deal with the logs in the project-controlling the output destination of log information (console, file, GUI component or NT event logger), controlling the output format of each log (HTMLLayout layout format, PatternLayout layout format, SimpleLayout layout format or TTCCLayout), controlling the level of output log information (DEBUG level, INFO level, WARN level, ERROR level or FATAL level), and so on. Let's start with a simple example to introduce how to use Log4j in detail:
The structure of the project is shown below:
The code in the TestLog4j.java file:
Package com.ghj.test;import org.apache.log4j.Logger;public class TestLog4j {private static final Logger logger = Logger.getLogger (TestLog4j.class); public static void main (String [] args) {logger.debug ("This log comes from the DEBUG level!"); logger.info ("This log comes from the INFO level!"); logger.warn ("This log comes from the WARN level!") Logger.error ("This log comes from the ERROR level!");}}
The code in the log4j.properties file:
Log4j.rootLogger=DEBUG,Console,DailyRollingFile# outputs the log information to the console log4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.Encoding=UTF-8log4j.appender.Console.layout=org.apache.log4j.SimpleLayout# and outputs the log information to the index.html file in the root directory of the operating system disk D log4j.appender.DailyRollingFile=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.DailyRollingFile.Encoding=UTF-8log4j.appender.DailyRollingFile.layout=org.apache.log4j.HTMLLayoutlog4j.appender.DailyRollingFile.File=D\:\\ index.html
[download the above source code project at 0 points]
From the above example, we know that the syntax for configuring log4j is as follows:
Log4j.rootLogger = level, appenderName1, appenderName2,... AppenderNameN# log information output destination 1log4j.appender.appenderName1 = fully qualified name of appender class log4j.appender.appenderName1.option1 = value1... log4j.appender.appenderName1.optionN = valueN# log information output destination 2log4j.appender.appenderName2 = fully qualified name of appender class log4j.appender.appenderName2.option1 = value1... log4j.appender.appenderName2.optionN = valueN. # Log information output destination Nlog4j.appender.appenderNameN = end of appender class Fully qualified name log4j.appender.appenderNameN.option1 = value1... log4j.appender.appenderNameN.optionN = valueN
The following syntax is explained one by one:
Log4j.rootLogger = [level], appenderName1, appenderName2,..., appenderNameN
1. Level: used to specify the output level of log log. The output level of Log4j log has five levels, which are DEBUG, INFO, WARN, ERROR and FATAL from small to large. The output level of the log log may not be specified in the configuration file, but it should be noted that in this case, the system will output information with a log information level equal to or higher than the DEBUG level to the specified log destination-in a word, the default log priority for Log4j is the DEBUG level.
Note: only if the log level of the log information is equal to or higher than the configured log level, the log information will be output to the specified log output destination. For example, if the log level configured by the above configuration file is DEBUG, then the log information with log level of DEBUG or INFO or WARN or ERROR or FATAL will be output to the specified log output destination, but if the log level in the configuration file is set to INFO Then the log information at the log level of INFO or WARN or ERROR or FATAL can be output to the specified log output destination, while the log information at the DEBUG level will not be output to the log destination.
2. AppenderName: output the destination place name of the log information. The name of the destination can be arbitrary, but it is best to see the meaning of the name; in addition, you can specify multiple destination places at the same time on the right side of the equal sign. For example, the above example specifies two log log destinations-Console (to output logs to the MyEclipse console) and DailyRollingFile (to output logs to the index.html file in the root directory of operating system D).
2. Log4j.appender.appenderName = the fully qualified name of the appender class
1. AppenderName has already said above. Here we talk about "the fully qualified name of the appender class". The qualified name is actually the implementation class of the log log destination. What are the implementation classes of the log4j log output destination?
A, org.apache.log4j.ConsoleAppender (output log information to the console)
B, org.apache.log4j.FileAppender (output log information to file)
C, org.apache.log4j.DailyRollingFileAppender (output log information to a file, which produces one per day)
D, org.apache.log4j.RollingFileAppender (outputs log information to a file that produces a new file when the size exceeds the specified size)
E, org.apache.log4j.WriterAppender (send log information to any specified place in stream format)
F, org.apache.log4j.net.SMTPAppender (send log information to the specified mailbox by mail)
3. Log4j.appender.appenderName.option = value
Option: here is the option to configure a log output destination, which varies according to the specified "fully qualified name of appender class", and the corresponding value of each option varies according to different option. Here is a description of "fully qualified name of appender class" and its corresponding option option:
A, org.apache.log4j.ConsoleAppender: [download sample code at 0: 00]
B, org.apache.log4j.FileAppender: [download sample code at 0: 00]
C, org.apache.log4j.DailyRollingFileAppender: [download sample code at 0: 00]
D, org.apache.log4j.RollingFileAppender: [download sample code at 0: 00]
E, org.apache.log4j.WriterAppender: [download sample code at 0: 00]
F, org.apache.log4j.jdbc.JDBCAppender: [download sample code at 0: 00]
The above is the editor for you to share how to understand the meaning of the code in the Log4j configuration file, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.