背景
由于一些框架中还使用log4j-1.x系列陈旧的日志框架,调试过程中有一些错误信息不能在控制台显示,增加了调试成本。以下配置方法
将帮助你获得log4j-1.x日志在控制台显示。
解决方法:
使用logback充当门面模式,由他来适配底层日志框架。
logback-spring.xml(一定要用着名称)
default
${logLevel}
%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n
${logPath}/${applicationName}.log
${logPath}/${applicationName}/${applicationName}.%d{yyyy-MM-dd}.%i.log
30
100MB
30GB
%d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n
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、首先在启动类设置属性
importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.EnableAutoConfiguration;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.system.ApplicationHome;importorg.springframework.cache.annotation.CacheConfig;importorg.springframework.cache.annotation.EnableCaching;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.scheduling.annotation.EnableAsync;importjava.io.File;importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Path;
@EnableAsync
@EnableCaching
@CacheConfig
@EnableAutoConfiguration
@SpringBootApplication(exclude={
xxxConfig.class})
@ComponentScanpublic classWebApplication{public static void main(String[] args) throwsIOException {
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}
已经不再凑效,时间原因没去深追。