SpringBoot------自定義Logback日誌

幫助文檔:html

https://docs.spring.io/spring-boot/docs/2.1.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-feature-logging

log4j.properties轉換logback.xmljava

https://logback.qos.ch/translator/
https://logback.qos.ch/manual/index.html

轉換前代碼:mysql

### 設置###
log4j.rootLogger = debug,stdout,D,E

### 輸出信息到控制擡 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 輸出DEBUG 級別以上的日誌到=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 輸出ERROR 級別以上的日誌到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

 

1.添加pom.xml的依賴web

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>top.ytheng</groupId>
  <artifactId>springboot-demo</artifactId>
  <version>0.0.1</version>
  <packaging>jar</packaging>
  
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        
        <!-- 熱部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
              <scope>true</scope>
        </dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
            <scope>runtime</scope>
        </dependency>
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>
    </dependencies>

    <build>
        <!-- 打包的名稱 -->
        <finalName>myspringboot</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2.添加lockback-spring.xml配置文件spring

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>
    
    <!-- 日誌記錄器,日期滾動記錄 -->
    <appender name="fileInfoApp" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 正在記錄的日誌文件的路徑及文件名 -->
        <!-- <file>${LOG_PATH}/warn/log_warn.log</file> -->
        <!-- 日誌記錄器的滾動策略,按日期,按大小記錄 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 歸檔的日誌文件的路徑,例現在天是2013-12-21日誌,當前寫的日誌文件路徑爲file節點指定,能夠將此文件與file指定文件路徑設置爲不一樣路徑,從而將當前日誌文件或歸檔日誌文件置不一樣的目錄。
            而2013-12-21的日誌文件在由fileNamePattern指定。%d{yyyy-MM-dd}指定日期格式,%i指定索引 -->
            <fileNamePattern>app_log/log/log-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <!-- 表示只保留最近30天的日誌,以防止日誌填滿整個磁盤空間。-->
            <maxHistory>30</maxHistory>
            <!--用來指定日誌文件的上限大小,例如設置爲1GB的話,那麼到了這個值,就會刪除舊的日誌。-->
            <totalSizeCap>1GB</totalSizeCap>
            <!-- 除按日誌記錄以外,還配置了日誌文件不能超過2M,若超過2M,日誌文件會以索引0開始,命名日誌文件,例如log-error-2013-12-21.0.log -->
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>2MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
       </rollingPolicy>
       <!-- 追加方式記錄日誌 -->
       <append>true</append>
       <!-- 日誌文件的格式 -->
       <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
           <pattern>===%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger Line:%-3L - %msg%n</pattern>
           <charset>utf-8</charset>
       </encoder>
       <!-- 此日誌文件只記錄war級別的 -->
       <filter class="ch.qos.logback.classic.filter.LevelFilter">
              <!-- 只保留error日誌 -->
              <!-- level:debug,info,warn,error -->
           <level>ERROR</level>
           <onMatch>ACCEPT</onMatch>
           <onMismatch>DENY</onMismatch>
       </filter>
    </appender>
    
    <!-- root節點要放到appender以後 -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="fileInfoApp" />
    </root>
</configuration>

3.添加控制器sql

package top.ytheng.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/log")
public class LogController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @GetMapping("/test")
    public Object test() {
        
        logger.debug("this is debug level");
        logger.info("this is info level");
        logger.warn("this is warn level");
        logger.error("this is error level");
        return "success";
    }
}

4.添加啓動類apache

package top.ytheng.demo;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication //等於下面3個
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

5.右鍵項目Rum As啓動,訪問地址api

http://localhost:8080/api/v1/log/test

如圖:springboot

 

另附:mybatis

 轉載:

https://www.cnblogs.com/xm-02/p/9109563.html
相關文章
相關標籤/搜索