0

I am using spring-boot 1.4.0. I want to use logstash-logback-encoder in my application in order to print my logs in json format.I am not getting any error but logs are still printed in plain text instead of json.Please find the below configuration and kindly guide me to resolve this issue.

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>

HomeController.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!";
}

Configuration class

@SpringBootApplication
public class SampleProjectApplication {

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

I want to print the logger in json format.Your help should be appreciated.

VelNaga
  • 3,593
  • 6
  • 48
  • 82
  • You haven't configured anything for the appender so how should it be outputting json... You only added a dependency you also have to add configuration for it. – M. Deinum Sep 08 '16 at 11:13
  • How to do it? Could you please fine me. I don't find any useful links in Google all explains about log back.xml not log back-spring.xml – VelNaga Sep 08 '16 at 11:29
  • `logback.xml` == `logback-spring.xml`... – M. Deinum Sep 08 '16 at 11:31
  • Ok.Configuration in a sense what you are mentioning just adding encoder is enough?could you please share a configuration link? – VelNaga Sep 08 '16 at 11:40
  • @M.Deinum Now i am able to log message in json format but it is printing in a separate file, Is it possible to print in the same log file instead of separate json file? – VelNaga Sep 08 '16 at 12:22
  • That is what you configured it to do. Not sure if you can mix those in a single file. – M. Deinum Sep 08 '16 at 12:48

0 Answers0