使用Lombok记录Spring Boot

Lombok项目是从应用程序中删除样板代码的非常方便的工具。Lombok还可以用于在Spring Boot应用程序中配置日志记录,从而删除用于获取logger实例的样板代码。

1.在Spring Boot应用程序中导入lombok

在使用它之前,我们必须在Spring Boot 应用程序中导入lombok。使用前,不要忘记将Lombok安装到Eclipse中

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.company</groupId>
	<artifactId>SpringBootEhcache</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>SpringBootEhcache</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.6.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<skipTests>true</skipTests>
	</properties>

	<dependencies>
	
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

2. @ Log,@ Log4j2,@ Slf4j –Lombok注解

相反,我们可以开始在用lombok的@Log注解注解的java类中编写日志语句。Lombok支持以下用于Spring Boot的日志注解–

  • @CommonsLog –使用以下语句创建记录器:
    private static final org.apache.commons.logging.Log log = 
        org.apache.commons.logging.LogFactory.getLog(LogExample.class);
  • @Log –使用以下语句创建记录器:
    private static final java.util.logging.Logger log = 
        java.util.logging.Logger.getLogger(LogExample.class.getName());
  • @Log4j2 –使用以下语句创建记录器:
    private static final org.apache.logging.log4j.Logger log = 
        org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
  • @Slf4j –使用以下语句创建记录器:
    Creates private static final org.slf4j.Logger log = 
        org.slf4j.LoggerFactory.getLogger(LogExample.class);

此日志注解的示例用法是:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
public class Application 
{
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
		
		log.info("Simple log statement with inputs {}, {} and {}", 1, 2, 3);
	}
}

默认情况下,Spring Boot logback用作日志记录提供程序。

3.在Lombok中使用Log4j2日志记录

要使用log4j2,请从类路径中排除logback,并包括log4j2。

<dependencies>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
		<exclusions>
			<exclusion>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-logging</artifactId>
			</exclusion>
		</exclusions>
	</dependency>

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-log4j2</artifactId>
	</dependency>

	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
	</dependency>

</dependencies>

现在,您可以使用@Slf4j推荐)或@Log4j2在类中将log4j2用作基础日志记录实现。

import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
public class Application 
{
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
		
		log.info("Simple log statement with inputs {}, {} and {}", 1, 2, 3);
	}
}
2019-07-28 15:29:41.267  INFO 7660 --- [main] com.how2codex.demo.Application       
	: Simple log statement with inputs 1, 2 and 3

请把有关使用lombok进行Spring Boot日志记录的问题交给我。

学习愉快!

参考文献:

Lombok测井注解

saigon has written 1440 articles

One thought on “使用Lombok记录Spring Boot

Leave a Reply