猿问

登录不工作?未生成 JSON

我有这些 Maven 依赖文件:


<dependency>

    <groupId>org.slf4j</groupId>

    <artifactId>slf4j-api</artifactId>

    <version>1.7.25</version>

</dependency>

<dependency>

    <groupId>ch.qos.logback</groupId>

    <artifactId>logback-classic</artifactId>

    <version>1.2.3</version>

</dependency>

<dependency>

    <groupId>net.logstash.logback</groupId>

    <artifactId>logstash-logback-encoder</artifactId>

    <version>6.1</version>

</dependency>

logback.xml 放置在 resources 文件夹中。


logback.xml 是(由于 Stackoverflow 格式,类名分为两行):


<?xml version="1.0" encoding="UTF-8"?>

<configuration>

  <conversionRule conversionWord="stacktrace"

    converterClass="net.logstash.logback.stacktrace.

    ShortenedThrowableConverter" />

  <appender name="JSON" class="ch.qos.logback.core.ConsoleAppender">

    <encoder 

      class="net.logstash.logback.encoder.

      LoggingEventCompositeJsonEncoder">

      <providers>

        <pattern>

          <pattern>

            {

            "timestamp": "%date{yyyy-MM-dd HH:mm:ss.SSS}",

            "level": "%level",

            "msg": "%msg",

            "stacktrace": "%stacktrace"

            }

          </pattern>

         </pattern>

     </providers>

   </encoder>

 </appender>

 <root level="info">

   <appender-ref ref="JSON"/>

 </root>

</configuration>

日志是这样的:


14:34:23,782 INFO  [x.y.Logger] (default task-15) Hello world!

日志不以JSON格式输出。为什么?


我认为 Logback 正在运行。应用程序启动时Logback如何启动?我没有任何代码行调用它。我读到 Logback 扫描 logback.xml,但我怎么知道它正在运行?


Logback 在新的 Spring boot 应用程序中运行良好,但此应用程序是旧的 JEE 应用程序。


问题是什么?


牧羊人nacy
浏览 115回答 2
2回答

当年话下

尝试这个编码器配置:<encoder class="net.logstash.logback.encoder.LogstashEncoder">&nbsp; <jsonGeneratorDecorator class="net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator"/>&nbsp; &nbsp; <timestampPattern>yyyy-MM-dd HH:mm:ss.SSS</timestampPattern>&nbsp; &nbsp; <providers>&nbsp; &nbsp; &nbsp; &nbsp; <timestamp/>&nbsp; &nbsp; &nbsp; &nbsp; <logLevel/>&nbsp; &nbsp; &nbsp; &nbsp; <message/>&nbsp; &nbsp; &nbsp; &nbsp; <stacktrace/>&nbsp; &nbsp; </providers></encoder>

手掌心

我必须添加EAR META-INF 中的 jboss-deployment-struct.xml。该文件的内容是:<?xml version="1.0" encoding="UTF-8"?>&nbsp;&nbsp;<jboss-deployment-structure>&nbsp;&nbsp;&nbsp; <deployment>&nbsp;&nbsp;&nbsp; &nbsp; <exclusions>&nbsp; &nbsp; &nbsp; &nbsp; <module name="org.slf4j" />&nbsp; &nbsp; &nbsp; &nbsp; <module name="org.slf4j.impl" />&nbsp; &nbsp; </exclusions>&nbsp; </deployment>&nbsp;&nbsp;</jboss-deployment-structure>
随时随地看视频慕课网APP

相关分类

Java
我要回答