背景
由于一些框架中还使用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}
已经不再凑效,时间原因没去深追。