Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to understand Log4j in Spring log

Shulou Source: shulou.com Published: 2022-06-02 01:47:24 09月15日 Update

This article will explain in detail how to understand the Log4j in the Spring log. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Introduction to Slf4j

The full name of    slf4j is Simple Loging Facade For Java, that is, it is only a unified interface to provide log output for Java programs, and it is not a specific log implementation, just like JDBC, it is just a rule. Therefore, slf4j alone does not work, and must be matched with other specific logging implementations, such as java.util.logging.Logger and log4j included in apache's org.apache.log4j.Logger,JDK.

The pom.xml configuration is as follows: org.slf4j slf4j-log4j12 1.7.25

Mainly increased org.slf4j:slf4j-log4j12 dependency

Create a log4j.properties profile:

Create a property profile named log4j.properties in the src/main/resources directory

Log4j.rootLogger=INFO, console, filelog4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%d% p [% c] -% m%nlog4j.appender.file=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.file.File=logs/log.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.A3.MaxFileSize=1024KBlog4j.appender.A3.MaxBackupIndex=10log4j.appender.file.layout.ConversionPattern=%d% p [% c] -% m% n

Log configuration instructions:

Log4j.rootLogger: root log, configured with log level of INFO, and predefined two appenders named console and file

Log4j.appender.console:console appender, log output is located in the console

Log4j.appender.console.layout:console appendage, using matcher layout mode

Log4j.appender.console.layout.ConversionPattern:console append, log output format is: date log level [class name]-message newline character

Log4j.appender.file:file appendage, which generates a log file every day

Log4j.appender.file.File:file appender, log file output location logs/log.log

Log4j.appender.file.layout:file appendage, using matcher layout mode

Log4j.appender.A3.MaxFileSize: log file maximum

Log4j.appender.A3.MaxBackupIndex: maximum number of files recorded

Log4j.appender.file.layout.ConversionPattern:file append, log output format is: date log level [class name]-message newline character

Test log output

Create a test class and test the log output as follows:

Import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class MyTest {public static final Logger logger = LoggerFactory.getLogger (MyTest.class); public static void main (String [] args) {logger.info ("slf4j for info"); logger.debug ("slf4j for debug"); logger.error ("slf4j for error"); logger.warn ("slf4j for warn"); String message = "Hello SLF4J" Logger.info ("slf4j message is: {}", message); / / {} placeholder}

The console displays as follows:

2018-06-07 05 slf4j for info2018 15 ERROR 42914 [com.hjj.hello.spring.MyTest]-slf4j for info2018-06-07 05 slf4j for info2018 15 ERROR [com.hjj.hello.spring.MyTest]-slf4j for error2018-06-07 05 INFO 15 ERROR [com.hjj.hello.spring.MyTest]-slf4j for warn2018-06-07 05 15 INFO [com.hjj.hello.spring.MyTest]-slf4j message is: Hello SLF4J

Logs/log.log directories and files will also be added to the root directory of the project.

Attached: placeholder ({}) description

   uses {} placeholders when logging, so there is no string concatenation, reducing the number of useless String objects and saving memory. And remember, before producing the string of the final log information, this method checks to see if a specific log level is turned on, which not only reduces memory consumption but also reduces the time CPU takes to process string concatenation commands in advance.

On how to understand the Spring log Log4j to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Log attach output file configuration level character string test location memory content layout newline character console article scheme date more format Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MariaDB Shulou Tech Info MySQL NVidia