首页 技术 正文
技术 2022年11月21日
0 收藏 859 点赞 3,753 浏览 3543 个字

背景

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

SpringBoot中日志配置

日志将记录到logs文件夹中

SpringBoot中日志配置

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

logging.path=${logPath}
已经不再凑效,时间原因没去深追。
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731