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 > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use Log4j logs in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Why use a journal?
We know that the program will produce a lot of information in the process of running, such as when it is run, and what is the result of running? In order for us to know more about the running of the program, we can view it through the log, which can be output in the console or to the specified file, which will be introduced in detail in the following article.
Download:
Log4J is an open source project of Apache for log processing. Download address:
Https://logging.apache.org/log4j/2.x/download.html
When the download is complete, we can get a package with the suffix jre.
Detailed steps: first, open IDEA
You can create a new project, then create a new lib package within the project, and put the log4j.jar package in it.
Then create a class within src:
Follow the steps in the figure:
Just add the jre package here:
Second, create log objects
When you have done this, you can create an object in the class:
Note: the Logger package selected here is to come from apache, so don't make a mistake here!
Import org.apache.log4j.Logger;public class logTest {public static void main (String [] args) {/ / Import object: Logger log = Logger.getLogger (logTest.class); log.error ("used to record error-level information"); / / record critical error log.warn ("used to record warn-level information") / / record warning log.info ("used to record information at info level"); / / record information log.debug ("used to record information at debug level"); / / record debug}}
Then we need to create a configuration file:
Create a new file file. The file suffix must be properties
Then create a new file, whose name can be set to: resources, and change the format as shown below:
Then put the newly created configuration file into this file:
We need to configure the following three most important information within log.properties:
Configure what level of logging your program will log to the log file
Specify the destination of the log output, whether to log to the program's console (transient state) or in a file on disk (persistent save)
Specify the output format of the log information output to the console or file, or in what format the log information is recorded.
The template is set as follows:
You can copy it directly into log.properties:
# 1. If you set the output level info, you can log info and higher to the log file, but the lower debug level will not be recorded in the log file.
# stdout is the destination of the set logging (the name can be given at random)
Log4j.rootLogger=info,stdout
# 2. Set the destination for logging (ConsoleAppender is recorded to the console)
Log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# 3. Format or style the record (System.err is red, System.out is black)
Log4j.appender.stdout.Target=System.err
# format the record
# PatternLayout is laid out according to our custom rules (d l m n is the specified rule layout)
Log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
Log4j.appender.stdout.layout.ConversionPattern=%d l m n
We can complete the above configuration without looking at this configuration information, and let's run the above code first:
If this happens, it is correct. Let's rewrite a piece of code to see what this log does:
Import org.apache.log4j.Logger;import java.util.Scanner;public class test2 {public static void main (String [] args) {Logger logger = Logger.getLogger (test2.class); Scanner input = new Scanner (System.in); try {System.out.println ("Please enter divisor:"); int a = input.nextInt (); logger.debug ("bug: input divisor" + a) Logger.info ("info: input divisor" + a); System.out.println ("Please enter the divisor:"); int b = input.nextInt (); logger.debug ("bug: input divisor" + b); logger.info ("info: input divisor" + b); int c = an and b / / record the results in the log file logger.debug ("bug: result" + c); logger.info ("info: result" + c); System.out.println ("result is" + c);} catch (Exception e) {e.printStackTrace (); System.out.println (e.getMessage ()) } finally {System.out.println ("Program is over!") ;}
This is a division operation. Run it first, depending on the effect:
We found that every step we performed in the program was logged. Because the configuration file is set to output to the console, the log information is displayed directly in the console. If you need to output to the specified file, you need to configure as follows:
Then run the division program:
The log information is not displayed in the console because it has been set to output to the specified file: according to the set path, we can see:
Such records have time information, program name information, and information about what happened in the first line of the program. Of course, there are many more output formats, which can be set separately according to your needs.
This is the end of the upperclassman Hsiao Ying about the Log4j log. After completing these steps, a simple log recording is completed, and the level of the log output below also needs to be noted.
Output level of the log:
1.off: highest level, used to turn off all logging
2.fatal points out that each serious error time will cause the application to exit.
3.error means that although an error event is sent, it still does not affect the operation of the system.
4.warn indicates that a potential error situation will occur
5.info general user records the running process of the program
Information recording commonly used by 6.debug for debugging
Minimum level of 7.all, used to open all logging
Thank you for reading! This is the end of the article on "how to use Log4j logs in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.