如何在Spring启动应用程序中将日志配置为仅采用JSON格式?

时间:2017-08-08 05:55:07

标签: spring-boot logstash logback

在我的春季启动应用程序中,以下配置用于通过logback-spring.xml添加JSON日志:

<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <fieldNames>
                <timestamp>time</`timestamp>
                <message>msg</message>
                <thread>thread</thread>
                <logger>logger</logger>
                <version>[ignore]</version>
                <levelValue>[ignore]</levelValue>
            </fieldNames>
        </encoder>
    </appender>
    <root level="all">
        <appender-ref ref="consoleAppender" />
    </root>
</configuration>

但是,在运行应用程序时,会跟踪两个版本的日志(非JSON和JSON):

2017-08-08 07:31:49.718  INFO 6849 --- [           main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 10000 (http/1.1)
{"time":"2017-08-08T07:31:49.718+02:00","msg":"Jetty started on port(s) 10000 (http/1.1)","logger":"org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer","thread":"main","level":"INFO","HOSTNAME":"ilya-ThinkPad-X1-Carbon-4th","caller_class_name":"org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainer","caller_method_name":"start","caller_file_name":"JettyEmbeddedServletContainer.java","caller_line_number":144}

是否可以配置logback / logstash编码器,以便日志中只有JSON版本可用,并且会跳过非JSON?

1 个答案:

答案 0 :(得分:3)

噢,愚蠢的我 - 应该从logback配置中删除该行添加基本日志记录:

<include resource="org/springframework/boot/logging/logback/base.xml"/>