2019独角兽企业重金招聘Python工程师标准>>>
最简单的日志配置
在application.yml中添加debug模式日志:
debug: true
启动项目后发现会打印很多日志。springboot默认是debug=false
默认日志配置是在org.springframework.boot.context.logging.LoggingApplicationListener
类中的。
static {
MultiValueMap loggers = new LinkedMultiValueMap();
loggers.add("web", "org.springframework.core.codec");
loggers.add("web", "org.springframework.http");
loggers.add("web", "org.springframework.web");
loggers.add("sql", "org.springframework.jdbc.core");
loggers.add("sql", "org.hibernate.SQL");
DEFAULT_GROUP_LOGGERS = Collections.unmodifiableMap(loggers);
loggers = new LinkedMultiValueMap();
loggers.add(LogLevel.DEBUG, "sql");
loggers.add(LogLevel.DEBUG, "web");
loggers.add(LogLevel.DEBUG, "org.springframework.boot");
loggers.add(LogLevel.TRACE, "org.springframework");
loggers.add(LogLevel.TRACE, "org.apache.tomcat");
loggers.add(LogLevel.TRACE, "org.apache.catalina");
loggers.add(LogLevel.TRACE, "org.eclipse.jetty");
loggers.add(LogLevel.TRACE, "org.hibernate.tool.hbm2ddl");
LOG_LEVEL_LOGGERS = Collections.unmodifiableMap(loggers);
EVENT_TYPES = new Class[]{ApplicationStartingEvent.class, ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class, ContextClosedEvent.class, ApplicationFailedEvent.class};
SOURCE_TYPES = new Class[]{SpringApplication.class, ApplicationContext.class};
shutdownHookRegistered = new AtomicBoolean(false);
}
使用logback配置日志
在src/main/resources
下新建文件 /logs/logback.xml
,内容(控制台带颜色渲染):
logback
INFO
true
--------------> %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger) - %cyan(%msg%n)
UTF-8
${log.path}/main.log
${log.path}/main.log%d{yyyy-MM-dd HH:mm}.log
30
1GB
--------------> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
UTF-8
application.yml中添加日志配置:
logging:
config: classpath:log/logback.xml
写一个测试接口:
@SpringBootApplication
@RestController
@Slf4j
public class LogApp {
public static void main(String[] args) {
SpringApplication.run(LogApp.class, args);
}
@GetMapping("/")
public String home(){
log.info("访问了主页接口");
return "hello world!";
}
}
启动项目后,访问 localhost:8080 ,查看控制台和文件
源码地址
https://gitee.com/yimingkeji/springboot/tree/master/log