0

我正在使用 spring-boot 1.4.0。我想在我的应用程序中使用 logstash-logback-encoder 以便以 json 格式打印我的日志。我没有收到任何错误,但日志仍然以纯文本而不是 json 打印。请找到以下配置并请指导我解决这个问题。

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>net.logstash.logback</groupId>
        <artifactId>logstash-logback-encoder</artifactId>
        <version>3.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>logs/${PROJECT_ID}.json</File>
    <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
    <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
        <maxIndex>1</maxIndex>
        <FileNamePattern>logs/${PROJECT_ID}.json.%i</FileNamePattern>
    </rollingPolicy>
    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
        <MaxFileSize>1MB</MaxFileSize>
    </triggeringPolicy>
</appender>
<logger name="sample.app" additivity="false" level="DEBUG">
    <appender-ref ref="file"/>
</logger>
<root level="WARN">
    <appender-ref ref="file"/>
</root>

家庭控制器.java

@RequestMapping("/home")
@ResponseBody
public String home(){
    System.out.println("Entered Home!!!");
    logger.debug("Requesting for path Home!!!");
    logger.error("Requesting for path Home error!!!");
    logger.info("Requesting for path Home info!!!");
    logger.trace("Requesting for path Home trace!!!");
    return "Hello World!";
}

配置类

@SpringBootApplication
public class SampleProjectApplication {

public static void main(String[] args) {
    SpringApplication.run(SampleProjectApplication.class, args);
}
}

我想以 json 格式打印记录器。应该感谢您的帮助。

4

0 回答 0