Spring Boot Logger

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/Andy2019/article/details/84780287

Spring Boot Logger

Spring Boot 默认的日志级别是INFO级别

package vip.fkandy.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class UserDao {
	private Logger log = LoggerFactory.getLogger(UserDao.class);
	public void log() {
		log.debug("UserDao debug log");
		log.info("UserDao info log");
		log.warn("UserDao warn log");
		log.error("UserDao error log");
	}
}

package vip.fkandy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import vip.fkandy.dao.UserDao;
import vip.fkandy.service.UserService;

@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
		context.getBean(UserDao.class).log();
		context.close();
	}
}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.5.RELEASE)

2018-12-03 21:27:52.012  INFO 11748 --- [           main] vip.fkandy.Application                   : Starting Application on DESKTOP-C87OJFS with PID 11748 (started by chenjianfei in D:\Spring Boot 实战与原理分析\Spring Boot 实战与原理分析\Spring Boot 实战与原理分析\springboot-10-logger)
2018-12-03 21:27:52.018  INFO 11748 --- [           main] vip.fkandy.Application                   : No active profile set, falling back to default profiles: default
2018-12-03 21:27:52.072  INFO 11748 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5038d0b5: startup date [Mon Dec 03 21:27:52 CST 2018]; root of context hierarchy
2018-12-03 21:27:52.669  INFO 11748 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-12-03 21:27:52.689  INFO 11748 --- [           main] vip.fkandy.Application                   : Started Application in 1.038 seconds (JVM running for 1.87)
2018-12-03 21:27:52.691  INFO 11748 --- [           main] vip.fkandy.dao.UserDao                   : UserDao info log
2018-12-03 21:27:52.691  WARN 11748 --- [           main] vip.fkandy.dao.UserDao                   : UserDao warn log
2018-12-03 21:27:52.691 ERROR 11748 --- [           main] vip.fkandy.dao.UserDao                   : UserDao error log
2018-12-03 21:27:52.691  INFO 11748 --- [           main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5038d0b5: startup date [Mon Dec 03 21:27:52 CST 2018]; root of context hierarchy
2018-12-03 21:27:52.694  INFO 11748 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

一、修改默认日志级别

#表示所有的类的日志级别都是debug
logging.level.root=debug
#表示vip.fkandy.servic包下的日志级别是debug
logging.level.vip.fkandy.service=debug
#表示文件日志的路径和名称
logging.file=./my.log
#控制台日志输出格式
logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} [%p][%c][%M][%L]-> %m%n
#文件日志输出格式
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%p][%c][%M][%L]-> %m%n
#只能打印出Spring启动类加载的debug信息,自己写的debug日志不能输出
debug=true
 
  • 可以通过logging.level.*=debug 来设置,*可以是包,也可以是某个类

  • 日志级别(7种) TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF

  • Spring Boot默认使用Logback作为日志控件,只需要在classpath目录下加入logback.xml或logback-spring.xml文件,就会生效

  • 日志文件输出大小为10M之后就会分割存储

二、使用其他日志组件步骤

  1. 排除默认的日志组件:spring-boot-starter-logging
  2. 加入新的日志依赖
  3. 加入相应的配置文件到classpath目录下

三、日志相关源码包

​ org.springframework.boot.logging.LoggingSystemProperties

猜你喜欢

转载自blog.csdn.net/Andy2019/article/details/84780287
今日推荐