Java is really not difficult to Log4j log

Log4j log:

What is Log4j?
log4j is a common logging framework, which is used to record the execution process or exceptions of the application. It is recorded in the log file. The operation and exception information of the application can be known through the log file.

Why log?
We know that the program will produce a lot of information during its operation. For example, when it runs, what is the result of the operation? In order to better understand the operation of the program, you can view it through the log. The log can be output on the console or to the specified file, which will be introduced to you in the following article.

Log4J is an open source project of Apache Company for log processing. Download address:

After downloading, we can get a package with the suffix jre.

Detailed steps:

  • 1, Then open IDEA, you can create a new project, and then create a new lib package in the project Put this package in.

Then create a class in src:

Follow the steps in the figure:

Add jre's package here:

  • 2, Create log object:
    After completing the above operations, you can create objects in the class:
    Note: the Logger package selected here is a package from apache. Be sure not to select it wrong here!
import org.apache.log4j.Logger;

public class logTest {

    public static void main(String[] args) {

        //Import objects:
        Logger log = Logger.getLogger(logTest.class);
        log.error("For recording error Level information"); //Log critical errors
        log.warn("For recording warn Level information");   //Log warning"For recording info Level information");   //Record information
        log.debug("For recording debug Level information"); //Record debug

Then we need to create a configuration file:
Create a new file. The file suffix must be properties

Then create a new file. The name can be set to: resources, and the format can be changed to the following figure:

Then put the newly created configuration file into this file:

We need to log The following three most important information are configured in properties:

  1. Configure what level of logging your program will record to the log file
  2. Specify the destination of log output, whether to record the log to the program's console (transient state) or in a file on disk (persistent storage)
  3. Specify the output format of log information output to the console or file, or in what format to record these log information.

The template set is as follows:
Copy directly to log Properties:

# 1. Set the output level info. You can record info and higher levels in the log file, but lower levels such as debug will not be recorded in the log file
# stdout is the destination of the set log record (the name can be given arbitrarily, which should be corresponding at that time)

#2. Set the destination of logging (ConsoleAppender records to the console)

#3. Set the format or style of the record (System.err is red and System.out is black)
# Format records
#PatternLayout is the layout according to our custom rules (% d% l% m% n is the specified rule layout)
log4j.appender.stdout.layout.ConversionPattern=%d %l %m %n

We can complete the above configuration without looking at the configuration information. Let's run the above code first:

If this happens, the description is correct.
Let's rewrite a piece of code to see the function of this log:

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.out.println("Please enter divisor:");
            int a = input.nextInt();
            logger.debug("bug:Enter divisor"+a);
  "info:Enter divisor"+a);
            System.out.println("Please enter the divisor:");
            int b = input.nextInt();
            logger.debug("bug:Enter divisor"+b);
  "info:Enter divisor"+b);
            int c = a/b;

            //Record the results in a log file
            System.out.println("The result is:"+c);

        } catch (Exception e) {
        }finally {
            System.out.println("The program is over!!");


This is a division operation. Run it first to see the effect:

We found that the log is recorded for every step we perform in the program. Because the output to the console is set in the configuration file, the log information is directly displayed on the console. If you need to output to the specified file, you need to configure as follows:

Then run the program of division:

The log information is not displayed on the console because it has been set to output to the specified file:
According to the set path, we can see:

Such records include time information, program name information and what happened in the first line of the program.
Of course, there are many output formats, which can be set separately as needed!

About Log4j log, I'll stop here. After completing these steps, a simple log recording is completed. The following log output levels also need to be paid attention to.

Output level of log: Highest level, used to turn off all logging 
2.fatal Each application will exit with a serious error 
3.error It refers to sending error events without affecting the operation of the system 
4.warn Indicates a potential error condition General users record the running process of the program 
6.debug Information record generally used for debugging 
7.all The lowest level, which is used to open all logging

Keywords: Java Back-end Programmer intellij-idea

Added by Warptweet on Fri, 25 Feb 2022 14:59:00 +0200