方式一:LoggerFactory 获取Logger对象
在需要打日志的类中定义。
@SpringBootApplication
public class SpringBoot303LoggingApplication {
private static Logger log = LoggerFactory.getLogger(SpringBoot303LoggingApplication.class);
public static void main(String[] args) {
SpringApplication.run(SpringBoot303LoggingApplication.class, args);
log.info("启动完成");
}
}
方式二:引入 lombok
使用
@Slf4j
注解可以自动生成一个log
对象,省去了手动获取Logger
对象的步骤,简化了代码编写。
导入依赖:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>compile</scope>
</dependency>
添加注接 @Slf4j
:
@Slf4j
@SpringBootApplication
public class SpringBoot303LoggingApplication {
// private static Logger log = LoggerFactory.getLogger(SpringBoot303LoggingApplication.class);
public static void main(String[] args) {
SpringApplication.run(SpringBoot303LoggingApplication.class, args);
log.info("启动完成");
}
}