最简单的日志配置
在application.yml中添加debug模式日志:
debug: true
启动项目后发现会打印很多日志。springboot默认是debug=false
默认日志配置是在org.springframework.boot.context.logging.LoggingApplicationListener
类中的。
static {
MultiValueMap<String, String> 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
,内容(控制台带颜色渲染):
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<contextName>logback</contextName>
<!--日志文件的存储地址-->
<property name="log.path" value="${user.home}/logs/springboot/log" />
<!--输出到控制台 名字随便写-->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<withJansi>true</withJansi>
<encoder>
<!--<pattern>%d %p (%file:%line\)- %m%n</pattern>-->
<!--格式化输出:%d:表示日期 %thread:表示线程名 %-5level:级别从左显示5个字符宽度 %msg:日志消息 %n:是换行符-->
<pattern>--------------> %red(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger) - %cyan(%msg%n)</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!--输出到文件-->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/main.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--按没分钟保存日志 修改格式可以按小时、按天、月来保存-->
<fileNamePattern>${log.path}/main.log%d{yyyy-MM-dd HH:mm}.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<!--格式化输出:%d:表示日期 %thread:表示线程名 %-5level:级别从左显示5个字符宽度 %msg:日志消息 %n:是换行符-->
<pattern>--------------> %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<root level="info">
<appender-ref ref="stdout" />
<appender-ref ref="file" />
</root>
</configuration>
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