springboot方法日志切片_SpringBoot中日志配置

背景

由于一些框架中还使用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文件夹

1c89f37e70efa37b4c1217a63286f286.png

日志将记录到logs文件夹中

a8d1938a9d230c8db36d8862088a8d31.png

spring框架中已经不建议再使用log4j-1.x.jar日志框架,笔者该篇文章使用的是2.0.2.RELEASE,笔者曾经尝试更新到2.1.6,以上日志配置

logging.path=${logPath}

已经不再凑效,时间原因没去深追。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值