1

everyone!

I am facing a weird behavior using Log4j2 and JsonLayout.

When I log any object the result on the console is a Json. However, there is a LocalDate attribute in my object and when it is serialized the result is not what I expected.

Here is my example class with a LocalDate attribute:

@JsonProperty("nome_qualquer")
private String nome;

@JsonProperty("data_qualquer")
private LocalDate dataTeste;

public TesteModel(String nome, LocalDate dataTeste) {
    this.nome = nome;
    this.dataTeste = dataTeste;
}

public LocalDate getDataTeste() {
    return dataTeste;
}

public String getNome() {
    return nome;
}

@Override
public String toString() {
    return "TesteModel{" +
            "nome='" + nome + '\'' +
            ", dataTeste=" + dataTeste +
            '}';
}

Here is my log4j2.xml file:

<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <JsonLayout complete="false" compact="false" objectMessageAsJsonObject="true"/>
    </Console>
</Appenders>

When I log the object I expect this:

{ "thread" : "http-nio-8085-exec-1", "level" : "INFO",
"loggerName" : "br.com.edwi.log4j2localdatelog.controller.LogController", "message" : { "nome_qualquer" : "testando qualquer coisa", "data_qualquer" "2020-03-18", "endOfBatch" : false, "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"instant" : { "epochSecond" : 1579153464, "nanoOfSecond" : 414000000 }, "threadId" : 23, "threadPriority" : 5 }

But I receive this:

{ "thread" : "http-nio-8085-exec-1", "level" : "INFO",
"loggerName" : "br.com.edwi.log4j2localdatelog.controller.LogController", "message" : { "nome_qualquer" : "testando qualquer coisa", "data_qualquer" : { "year" : 2020, "month" : "MARCH", "dayOfWeek" : "WEDNESDAY", "era" : "CE", "dayOfYear" : 78, "leapYear" : true, "chronology" : { "calendarType" : "iso8601", "id" : "ISO" }, "monthValue" : 3, "dayOfMonth" : 18 } }, "endOfBatch" : false, "loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger", "instant" : { "epochSecond" : 1579153464, "nanoOfSecond" : 414000000 }, "threadId" : 23, "threadPriority" : 5 }

What is happening with the LocalDate field? Why is it not serialized with the ISO format?

I am using the Spring Boot and my pom.xml is below:

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jsr310</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Best regards!

0 Answers0