1.2 日誌框架
阿新 • • 發佈:2018-11-05
1.日誌框架應該具有的能力
2.
logback 的一些用法
@Slf4j 可以不用每個類上面都寫 private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);
package com.imooc; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner; /** * Created by Administrator on 2018/11/5. */ @RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class LoggerTest { // private final Logger logger = LoggerFactory.getLogger(LoggerTest.class); @Test public void test1() { String name= "姓名"; int age = 2; log.info("username:{},age:{}", name, age); log.info("info"); log.debug("debug"); log.error("error"); } }
2.1 第一種寫法 只能簡單的配置輸出格式和路徑
#logging: # pattern: ## console: "%d - %msg%n" ## path: D:\JavaInstall\selllog\ # file: D:\JavaInstall\selllog\log1.log # level: # com.imooc.LoggerTest: debug
2.2 第二種格式 新建一個配置檔案 (一般用這個寫法)
logback-spring.xml
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <appender name="comsoleLog" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> %d -- %msg%n </pattern> </layout> </appender> <!--只輸出info日誌--> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> <!--<onMatch>DENY</onMatch>--> <!--<onMismatch>ACCEPT</onMismatch>--> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <!--滾動策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--路徑--> <fileNamePattern> D:\JavaInstall\selllog\info.%d.log </fileNamePattern> </rollingPolicy> </appender> <!--只輸出error級別的日誌--> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <!--滾動策略--> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--路徑--> <fileNamePattern> D:\JavaInstall\selllog\error.%d.log </fileNamePattern> </rollingPolicy> </appender> <root level="info"> <appender-ref ref="comsoleLog"></appender-ref> <appender-ref ref="fileInfoLog"></appender-ref> <appender-ref ref="fileErrorLog"></appender-ref> </root> </configuration>