slfj+logback

1.pom.xml編程

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>


<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
</dependency>

2.logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--debug="true" : 打印logback內部狀態(默認當logback運行出錯時纔會打印內部狀態 ),配置該屬性後打印條件以下(同時知足):
一、找到配置文件 二、配置文件是一個格式正確的xml文件 也可編程實現打印內部狀態,例如: LoggerContext lc = (LoggerContext)
LoggerFactory.getILoggerFactory(); StatusPrinter.print(lc); -->
<!-- scan="true" : 自動掃描該配置文件,如有修改則從新加載該配置文件 -->
<!-- scanPeriod="30 seconds" : 配置自動掃面時間間隔(單位能夠是:milliseconds, seconds, minutes
or hours,默認爲:milliseconds), 默認爲1分鐘,scan="true"時該配置纔會生效 -->
<configuration debug="false" scan="true" scanPeriod="30 seconds" packagingData="true">
<!-- 設置 logger context 名稱,一旦設置不可改變,默認爲default -->
<contextName>myAppName</contextName>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are by default assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 當前活動日誌文件名 -->
<file>./my_log.log</file>
<!-- 文件滾動策略根據%d{patter}中的「patter」而定,此處爲天天產生一個文件 -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- 歸檔文件名「.zip或.gz結尾」,表示歸檔文件自動壓縮 -->
<FileNamePattern>./my_log%d{yyyyMMdd}.log.zip</FileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>

<!--rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>renhai%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>

<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>20MB</maxFileSize>
</triggeringPolicy-->

<!-- <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
rollover daily
<fileNamePattern>mylog-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
each file should be at most 30MB, keep 60 days worth of history, but at most 20GB
<maxFileSize>30MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy> -->


<encoder>
<pattern>%d{HH:mm:ss.SSS}[%-5level][%thread]%logger{36} - %msg%n</pattern>
<!-- <pattern>%d{HH:mm:ss.SSS}[%-5level][%thread] - %msg%n</pattern> -->
</encoder>
</appender>

<!-- 日誌級別若沒顯示定義,則繼承最近的父logger(該logger需顯示定義level,直到rootLogger)的日誌級別-->
<!-- logger的appender默認具備累加性(默認日誌輸出到當前logger的appender和全部祖先logger的appender中),可經過配置 「additivity」屬性修改默認行爲-->
<logger name="com.yinz" level="debug" additivity="false" >
<appender-ref ref="FILE"/>
</logger>

<!-- 至多隻能配置一個root -->
<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
3.
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Slf4j
public class Test2 {
static Logger log1 = LoggerFactory.getLogger(Test2.class);

public static void main(String[] args) {

System.out.println(log1.isInfoEnabled());
log1.info("你好啊{}", "01");
log.info("cc");

}}
以上會打印到控制檯和文件麗
本站公眾號
   歡迎關注本站公眾號,獲取更多信息