Log4j 与 Logback的ConversionPattern对比

为了能将log4j的配置无缝转到logback,需要了解其中ConversionPattern的差异,以下是对比表格,内容来自:

  1. log4j官网
  2. logback官网

其中可能需要转换的地方主要有两块:

  1. %l
  2. %x
Log4jLog4j DescriptionLogbackLogback Description
cUsed to output the category of the logging event.c{length} / lo{length} / logger{length}Outputs the name of the logger at the origin of the logging event.
CUsed to output the fully qualified class name of the caller issuing the logging requestC{length} class{length}Outputs the fully-qualified class name of the caller issuing the logging request.
dUsed to output the date of the logging event.d{pattern} / date{pattern} / d{pattern, timezone} / date{pattern, timezone}Used to output the date of the logging event.
FUsed to output the file name where the logging request was issued.F / fileOutputs the file name of the Java source file where the logging request was issued.
lUsed to output location information of the caller which generated the logging event.caller{depth} / caller{depth, evaluator-1, ... evaluator-n}Outputs location information of the caller which generated the logging event.
LUsed to output the line number from where the logging request was issued.L / lineOutputs the line number from where the logging request was issued.
mUsed to output the application supplied message associated with the logging event.m / msg / messageOutputs the application-supplied message associated with the logging event.
MUsed to output the method name where the logging request was issued.M / methodOutputs the method name where the logging request was issued.
nOutputs the platform dependent line separator character or characters.nOutputs the platform dependent line separator character or characters.
pUsed to output the priority of the logging event.p / le / levelOutputs the level of the logging event.
rUsed to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event.r / relativeOutputs the number of milliseconds elapsed since the start of the application until the creation of the logging event.
tUsed to output the name of the thread that generated the logging event.t / threadOutputs the name of the thread that generated the logging event.
xUsed to output the NDC (nested diagnostic context) associated with the thread that generated the logging event.  
XUsed to output the MDC (mapped diagnostic context) associated with the thread that generated the logging event. The X conversion character must be followed by the key for the map placed between braces, as in %X{clientNumber} where clientNumber is the key. The value in the MDC corresponding to the key will be output.X{key:-defaultVal} / mdc{key:-defaultVal}Outputs the MDC (mapped diagnostic context) associated with the thread that generated the logging event.
%The sequence %% outputs a single percent sign.  

转载于:https://www.cnblogs.com/diecui1202/p/3531505.html

### 回答1: 在 Spring Boot 中集成 log4j 很简单,您需要做的是在项目的 classpath 下添加 log4j 的配置文件(通常命名为 log4j.properties 或 log4j.xml),然后在配置文件中配置 log4j 的各项参数。 如果您使用的是 Maven 构建工具,可以在 pom.xml 文件中添加 log4j 的依赖: ``` <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> ``` 如果您使用的是 Gradle 构建工具,可以在 build.gradle 文件中添加 log4j 的依赖: ``` compile group: 'log4j', name: 'log4j', version: '1.2.17' ``` 然后,在 classpath 下新建 log4j 配置文件,并配置相应的参数。例如,在 log4j.properties 中可以这样配置: ``` log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n ``` 更多关于 log4j 配置的信息,可以参考 log4j 官方文档。 ### 回答2: 使用Spring Boot集成log4j非常简单,只需按照以下步骤操作: 1. 在pom.xml文件中添加log4j依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency> ``` 2. 创建一个log4j.properties或log4j.xml配置文件,并将其放置在src/main/resources目录下。例如,创建log4j.properties文件: ```properties log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n ``` 这是一个简单的日志配置文件,将日志输出到控制台,并包含日期、日志级别、类名、行号和日志消息。 3. 在Spring Boot的启动类上使用@EnableAutoConfiguration注解启用自动配置,并使用@ImportResource注解导入log4j配置文件: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ImportResource; @EnableAutoConfiguration @ImportResource("classpath:log4j.properties") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这样,Spring Boot就会自动读取log4j配置并使用它来记录日志了。 注意:如果使用log4j2而不是log4j,只需将依赖和配置文件中的名称改为log4j2即可。另外,Spring Boot也支持使用slf4j或logback等其他日志框架,集成方式类似。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值