在application.yml中添加debug模式日誌:java
debug: true
啓動項目後發現會打印不少日誌。springboot默認是debug=falsegit
默認日誌配置是在org.springframework.boot.context.logging.LoggingApplicationListener
類中的。web
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); }
在src/main/resources
下新建文件 /logs/logback.xml
,內容(控制檯帶顏色渲染):spring
<?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中添加日誌配置:sql
logging: config: classpath:log/logback.xml
寫一個測試接口:apache
@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 ,查看控制檯和文件tomcat
https://gitee.com/yimingkeji/springboot/tree/master/logspringboot