log4j 如何使用logbook-spring-boot-starter以自定义模式输出日志

r7knjye2  于 2022-11-23  发布在  Spring
关注(0)|答案(1)|浏览(292)

我有一个spring应用程序,它使用logbook logger进行请求/响应日志记录
pom.xml

<dependency>
   <groupId>org.zalando</groupId>
   <artifactId>logbook-spring-boot-starter</artifactId>
   <version>2.14.0</version>
</dependency>

application.yml

logging:
  level:
    org.zalando.logbook: TRACE

logback正在记录传入和传出响应,如下所示

2022-11-09T09:00:51,084Z {"origin":"remote","type":"request","correlation":"c6d7a74....}

我怎样才能使它以这种方式记录?

2022-11-09T09:00:51.036210Z - info: {"origin":"remote","type":"request","correlation":"c6d7a74...}

尝试使用logback.xml中的模式,但似乎没有任何效果

<configuration>
  <appender name="STDOUT" class="org.zalando.logbook.json.JsonHttpLogFormatte">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  <root level="info">
    <appender-ref ref="STDOUT"/>
  </root>
</configuration>
fv2wmkja

fv2wmkja1#

我通过添加appender.console.layout.pattern = %d{ISO8601} - info: %msg%n这一行来修改resources/log4j2.properties,从而解决了此问题
文件如下所示

name = requestlogger

# Console appender configuration
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{ISO8601} - info: %msg%n

# Root logger level
rootLogger.level = info
# Root logger referring to console appender
rootLogger.appenderRef.stdout.ref = consoleLogger

相关问题