In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to use Log4j2 simply, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Log is a function often used by the system, we can rely on the log to view the output when debugging, and judge the running status of the program by looking at the log when the program is running. In the Java world, there is a very famous logging class library-Log4j. Now there is a new version of Log4j, which is Log4j2. I won't say much about the benefits of the new version. Let's start using it.
Introduction of Log4j2
To use Log4j2, the first step is to import its jar package first. If it's a normal project, download the jar package from the log4j2 website, and then add log4j-api-2.7.jar and log4j-core-2.7.jar to your project's classpath.
If you use Maven or Gradle, query the mvn repository to find the two packages and add them to the project dependency. In this way, the preparatory work is completed.
Then open the project, add the following two sentences, create a Logger and debug some information. Note that the full names of the Logger and LogManager classes are org.apache.logging.log4j.LogManager and org.apache.logging.log4j.Logger, respectively. Don't be confused with the log class under java.util.logging that comes with Java.
Logger logger = LogManager.getLogger (); logger.debug ("5555555")
Then run the project and you can see the corresponding output.
Log level
If you follow the previous steps, you will find that there is no output, only one sentence: ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. . This means that because the configuration file is not found, only error messages are output to the console using the default log level. This leads to a log-level problem.
Logs are divided into several levels, arranged in order from light to heavy: trace, debug, info, warn, error, fatal. The corresponding Logger class also has several corresponding methods for outputting the corresponding log information. If we define a level, logs below that level will not be output. Since there is no configuration file, the log level is error by default, as in the previous output. In this way, logs lower than error will not be output. So let's go back to just now, change the debug method to the error method, and run the project again. This time there is a log output.
22 _ _ 21 _ _ 58.600 [Test worker] ERROR yitian.bean.BeanTest-5555555 profile
The use of Log4j2 has been briefly demonstrated earlier. Let's talk about the configuration file of Log4j2. Log4j2 can be configured either with a configuration file or programmatically with code. Here is a brief description of the configuration file. Log4j2 supports a variety of configuration files, including XML, JSON, YAML, and perperties files, and of course the most commonly used is the XML file. Just put the configuration file under the classpath, or if you use Maven or Gradle, under the resources folder.
If there is no configuration file above, Log4j2 will use a default configuration, which is equivalent to the following configuration file.
The configuration file has two main places. the first is the Appender node, where there will be a lot of Appender, that is, the destination of our log output, which can be the console, a file, or even a dedicated remote log server. There is only one console by default. The second important node is the Loggers node, under which there can be many Logger, each Logger can record different information, and some configurations can be shared between Logger. A Root Logger is defined above. If no Logger name is specified or the specified Logger does not exist, Root will be used, and the configuration under Root Logger will be inherited by other Logger by default, unless they define their own configuration.
Then we look back at the Appenders node, where there is a paragraph:. What is defined here is the output format of the log. Let's briefly talk about the commonly used output formats.
% d {HH:mm:ss.SSS} output time, accuracy is millisecond.
T outputs the name of the current thread.
%-5level output log level,-5 indicates left alignment and fixed width of 5 characters, if not filled with spaces.
% logger outputs the Logger name, but there is no name if it is Root.
% msg log information, that is, the information we passed in.
% n wrap.
% F the file name where the output is located.
% L outputs the line number.
The name of the method where the% M output is located.
L outputs the location of the statement, including file name, class name, method name, line number.
Custom configuration Custom Logger
With all that said, now we can start customizing the configuration file. First of all, let's add a new Logger to record all the information. The name of this Logger is TRACE_ALL. This Logger has two properties, level specifies the record level and additivity specifies transitivity. If transitivity is specified as true, if we now use TRACE_ALL to record a log at the debug level, because the debug level is also within the recording scope of Root, the log will be recorded twice. Finally, we specify to use the console Appender above.
The configuration file should now look like this.
The program code goes like this. Specify which Logger to use when calling getLogger.
Logger logger = LogManager.getLogger ("TRACE_ALL"); logger.info ("5555555"); Custom Appender
In addition to using Console as the log output destination, we can define other outputs, such as files. Let's create a new file, Appender. Create a new File node under the Appenders node, and the name and fileName attributes specify the Appender name and the destination file name, respectively. Then we specify TRACE_ALL Logger to output the log to both the console and the file.
Run the program again, and we will find that this time there is an extra file under the corresponding path, and the content of this file is our output.
The above content is how to use Log4j2 simply. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, 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.