Location>code7788 >text

[Java/Logging] Logging framework to print the execution of the application logging code

Popularity:865 ℃/2024-10-16 16:21:05

0 Introduction

  • I often assumed that application logging code at the INFO log level would not be executed (e.g., Experiment 1'sprintTestLog(Functions). But today's problem on the line confirms that this line of thinking is wrong.

1 Validation experiments

  • version information
  • jdk : 1.8
  • log component
  • : 1.7.25
  • : 2.20.0
<!-- log [start] -->
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>${}</version>
</dependency>
<dependency>
	<groupId>.log4j</groupId>
	<artifactId>log4j-api</artifactId>
	<version>${}</version>
</dependency>
<dependency>
	<groupId>.log4j</groupId>
	<artifactId>log4j-core</artifactId>
	<version>${}</version>
</dependency>
<dependency>
	<groupId>.log4j</groupId>
	<artifactId>log4j-slf4j-impl</artifactId>
	<version>${}</version>
</dependency>
<dependency>
	<groupId>.log4j</groupId>
	<artifactId>log4j-jul</artifactId>
	<!--<version>2.13.3</version>-->
	<version>${}</version>
	<scope>compile</scope>
</dependency>
<!-- log [end] -->

Experiment 1: Logging framework to print the execution of the application logging code

## Level of the log (customized configuration item)
##=ALL,TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF
=DEBUG

# ------------------- [1.1] Define global configuration for RootLogger, etc. (not to be changed) ------------------- #
## rootLogger, root logger, parent of all loggers
## Specify the level of the root logger | All < Trace < Debug < Info < Warn < Error < Fatal < OFF
=${}

... // Omitted

Application code:LogTest

  • LogTest
package ;

import .slf4j.Slf4j;

@Slf4j
public class LogTest {
    public static String printTestLog(){
        return "HelloWorld";//Key Lines of Code
    }

    public static void main(String[] args) {
        ( "log:{}", printTestLog() );
    }
}

Results

  • =INFOhour
Key Lines of Code : Executed

Log Output: Empty
  • =DEBUGhour
Key Lines of Code : Executed

Log Output.
[20XX/10/16 16:01:28.585] [TID: N/A] [DEBUG] [main] [:12 main] log:HelloWorld

reach a verdict

  • Regardless of the logging level used by the application logging code logger to print logs, the program in the line of code will be executed, but the final output is determined by the logging framework based on the logging level of the class to which the logger is configured (appender).

X References

  • not have