Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-06-02 12:48:27 11月03日 Update

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!

Tags: Logs environment configuration levels differences frameworks articles tests projects development appearance performance ways patterns features programs Baba code values interests Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker macOS Shulou Tech Info Xiaomi vpn