spring boot使用slf4j輸出日誌
阿新 • • 發佈:2019-02-03
的配置,log也不例外,
我們先看看pom.xml中的配置吧!如果之前有新增
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
這裡可以不同添加了,因為裡面已經包含
- <dependency>
- <groupId>org.springframework.boot
- <artifactId>spring-boot-starter-logging</artifactId>
- </dependency>
沒有的就加上吧!二選一皆可!
程式碼:
public class ArticleController extends BaseController { private static Logger log = LoggerFactory.getLogger(ArticleController.class); @Autowired private RedisConfig redisConfig; @Autowired private ArticleService articleService; @RequestMapping("/articleList") @ResponseBody public BaseModel<List<ArticleVO>> articleList(){ BaseModel<List<ArticleVO>> res = new BaseModel<>(); JedisCluster jedisCluster = redisConfig.getJedisCluster(); String redis=redisConfig.getJedisCluster().get("redisKey"); log.info("redis:"+redis); }
接下來我們先看看application.properties方式的配置吧,
- #日誌相關配置
- logging.level.root=INFO
- logging.level.org.springframework.web=DEBUG
- logging.level.org.hibernate=ERROR
- logging.path=/home/_xyy_/output/SpringBootDemo/logs
- #logging.file=demo.log
logging.org.springFramework.web具體服務的日誌級別
logging.path:日誌的輸出路徑
logging.file:輸出的日誌檔案
注(大坑):
1:logging.path和logging.file不可以同時配置,同時配置也只有logging.file起效
2:配置logging.path將會在指定資料夾下面生成spring.log檔案,檔名字無法控制
3:配置logging.file,如果只是檔名如:demo.log只會在專案的根目錄下生成指定檔名的日誌檔案,,如果想控制日誌路徑,可以選擇完整路徑
如:E:\\demo\\demo.log
接下來看看自定義配置檔案,這個就要方便很多了,還是喜歡自定義配置檔案的方式
在src/main/resources下面新建檔案logback.xml這個也是spring boot預設的配置檔名,
如果需要自定義檔名,如:logback-test.xml需要在application.properties新增配置
logging.config=classpath:logback-test.xmlspring boot預設載入的相關配置檔案,詳見jar包;spring-boot-1.*.*.RELEASE.jar下面
org/springframework/boot/logging/logback/
詳細檔案:
base.xml //基礎包,引用了下面所有的配置檔案
console-appender.xml //控制檯輸出配置
defaults.xml //預設的日誌檔案配置
file-appender.xml //檔案輸出配置
logback.xml詳細程式碼:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <configuration>
- <includeresource="org/springframework/boot/logging/logback/defaults.xml"/>
- <appendername="CONSOLE"class="ch.qos.logback.core.ConsoleAppender">
- <encoder>
- <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</pattern>
- <charset>UTF-8</charset>
- </encoder>
- </appender>
- <appendername="FILE"class="ch.qos.logback.core.rolling.RollingFileAppender">
- <file>/home/_xyy_/output/SpringBootDemo/logs/stdout.log</file>
- <rollingPolicyclass="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
- <!-- daily rollover 儲存歷史記錄到這個資料夾一日起為字尾 -->
- <fileNamePattern>/home/_xyy_/output/SpringBootDemo/logs/stdout.log.%d{yyyy-MM-dd}.log</fileNamePattern>
- <!-- keep 30 days' worth of history -->
- <maxHistory>30</maxHistory>
- </rollingPolicy>
- <triggeringPolicyclass="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
- <maxFileSize>10MB</maxFileSize>
- </triggeringPolicy>
- <encoder>
- <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n</pattern>
- <charset>UTF-8</charset><!-- 此處設定字符集 -->
- </encoder>
- </appender>
- <rootlevel="INFO">
- <appender-refref="CONSOLE"></appender-ref>
- <appender-refref="FILE"></appender-ref>
- </root>
- </configuration>