SpringBoot中日志配置


背景

由於一些框架中還使用log4j-1.x系列陳舊的日志框架,調試過程中有一些錯誤信息不能在控制台顯示,增加了調試成本。以下配置方法

將幫助你獲得log4j-1.x日志在控制台顯示。

解決方法:

使用logback充當門面模式,由他來適配底層日志框架。

logback-spring.xml(一定要用着名稱)

<?xml version="1.0" encoding="UTF-8"?>
<configuration  scan="true" scanPeriod="60 seconds" debug="false">
    <contextName>default</contextName>
     <springProperty scope="context" name="logLevel" source="logging.level.root"/>
     <springProperty scope="context" name="logPath" source="logging.path"/>
     <springProperty scope="context" name="applicationName" source="spring.application.name"/>
    <!--輸出到控制台-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
             <level>${logLevel}</level>
         </filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <!--輸出到文件-->
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logPath}/${applicationName}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${logPath}/${applicationName}/${applicationName}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <!-- keep 30 days' worth of history capped at 3GB total size -->
            <maxHistory>30</maxHistory>
            <maxFileSize>100MB</maxFileSize>
            <totalSizeCap>30GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="${logLevel}">
        <appender-ref ref="console" />
        <appender-ref ref="file" />
    </root>
</configuration>

application.yml/application.properties添加以下屬性(不太喜歡yaml格式,因為臭名昭著的空格問題,常常讓程序不能正常運行)

application.yml

spring:
  application:
    name: aiPlatform
logging:
  level:
    root: debug
  path: C:\\xxx
  xatu.zsl: debug
  org.springfromework.web: debug

 

application.properties

spring.application.name=aiPlatform
logging.level.root=debug
logging.path: C:\\xxx

日志路徑的配置

1、操作系統環境變量

當然logging.path也可以動態設定,比如取操作系統環境變量

logging.path: ${TEMP}
#相當於:System.getenv("TEMP")

2、JVM屬性

經過筆者測試,你也可以設置JVM屬性,作為日志路徑,內部的表達式怎么評估出值,筆者沒去深追代碼

1、首先在啟動類設置屬性

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@EnableAsync
@EnableCaching
@CacheConfig
@EnableAutoConfiguration
@SpringBootApplication(exclude = {
        xxxConfig.class
})
@ComponentScan
public class WebApplication{

    public static void main(String[] args) throws IOException {

        ApplicationHome home = new ApplicationHome(WebApplication.class);
        // returns the folder where the jar is. This is what I wanted.
        File rootFolder = home.getDir();
       //jar文件同級目錄下識別logs目錄絕對地址
        Path logPath = rootFolder.toPath().resolve("logs").normalize().toAbsolutePath(); //jar文件父級目錄logs文件夾路徑
       //Path logPath = rootFolder.toPath().getParent().resolve("logs").normalize().toAbsolutePath();

        if(!Files.exists(logPath))
        {
            Files.createDirectories(logPath);
        }
        System.setProperty("logPath", logPath.toString());

        SpringApplication.run(WebApplication.class, args);
    }
}

 

 

application.yml日志配置路徑如下:

logging:
  level:
    root: debug
  path: ${logPath}

將springboot打包成jar,程序會在同級目錄新建logs文件夾,如圖,web-0.3.jar同級目錄的logs文件夾

 日志將記錄到logs文件夾中

 

 

 spring框架中已經不建議再使用log4j-1.x.jar日志框架,筆者該篇文章使用的是2.0.2.RELEASE,筆者曾經嘗試更新到2.1.6,以上日志配置

logging.path=${logPath}
已經不再湊效,時間原因沒去深追。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM