Lombok在Spring Boot中的日志记录

avatar
作者
筋斗云
阅读量:0

在Spring Boot中,可以使用logback或者log4j等日志框架来记录日志。在Lombok中,可以使用@Slf4j注解来自动生成日志记录器,示例代码如下:

import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;  @RestController @Slf4j public class LoggingController {      @GetMapping("/log")     public String logMessage() {         log.debug("This is a debug message");         log.info("This is an info message");         log.warn("This is a warning message");         log.error("This is an error message");          return "Logging messages";     } } 

在上面的示例中,通过@Slf4j注解自动生成了一个日志记录器log,然后可以使用log.debug()、log.info()、log.warn()和log.error()方法来记录不同级别的日志信息。

另外,可以在application.properties文件中配置日志级别和输出格式,例如:

logging.level.root=INFO logging.pattern.console=%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n 

通过以上配置,可以设置根日志级别为INFO,以及定义控制台输出格式为时间、线程、日志级别、类名和消息。这样可以更灵活地控制日志记录的行为。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!