Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Log4j2 in SpringBoot2 to print logs in different environments

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to configure Log4j2 in SpringBoot2 to achieve different environment log printing, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. Brief introduction of Log4j2 log

Log printing is the most direct way to understand the operation of the Web project, so it is necessary to build a good environment in the project development.

1. Characteristics of Log4j2

1) Core features

Compared with other log systems, log4j2 loses data less; disruptor technology, in a multithreaded environment, has high performance; concurrency reduces the occurrence of deadlocks.

2) performance testing

2. The appearance mode of log printing

Each logging framework has its own separate API. To use the corresponding framework, it is necessary to use its corresponding API to increase the coupling between the application code and the logging framework.

"Alibaba Java Development Manual", one of the specifications makes a "mandatory" requirement:

SLF4J coach

Java simple log facade (Simple Logging Facade for Java, abbreviated SLF4J) is a set of interface program that wraps the Logging framework and is implemented in appearance mode.

Configure log printing 1. Project structure

2. Log configuration in different environments

In the most direct way, different environments load different log configurations.

1) configuration of development environment

Logging: config: classpath:log4j2-boot-dev.xml

2) configuration of production environment

Logging: config: classpath:log4j2-boot-pro.xml3 、 Configuration file for Log4j2 3. Test log printing 1. Simple test program import org.slf4j.Logger Import org.slf4j.LoggerFactory;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class Log4j2Controller {private static final Logger LOGGER = LoggerFactory.getLogger (Log4j2Controller.class); / * log level * OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL * / @ RequestMapping ("/ printLog") public String printLog () {LOGGER.error ("ERROR level log") LOGGER.warn ("WARN level log"); LOGGER.info ("INFO level log"); LOGGER.debug ("DEBUG level log"); LOGGER.trace ("TRACE level log"); return "success";}} 2, test effect diagram

Thank you for reading this article carefully. I hope the article "how to configure Log4j2 in SpringBoot2 to print logs in different environments" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report