Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
package com.lmoraesdev.payment.config.logging;

public record Log5w1h(
String traceId,
String spanId,
String where,
String why,
String when,
Object who,
Object what,
String how) {}
public record Log5w1h(String where, String why, String who, String what, String how) {}
26 changes: 12 additions & 14 deletions src/main/java/com/lmoraesdev/payment/config/logging/Logger5w1h.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.lmoraesdev.payment.config.logging;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LoggingEventBuilder;

public class Logger5w1h {

private static final ObjectMapper MAPPER = new ObjectMapper();

private final Logger logger;

private Logger5w1h(Class<?> clazz) {
Expand All @@ -19,26 +17,26 @@ public static Logger5w1h of(Class<?> clazz) {
}

public void info(Log5w1h data) {
logger.info(toJson(data));
withFields(logger.atInfo(), data).log();
}

public void debug(Log5w1h data) {
logger.debug(toJson(data));
withFields(logger.atDebug(), data).log();
}

public void warn(Log5w1h data) {
logger.warn(toJson(data));
withFields(logger.atWarn(), data).log();
}

public void error(Log5w1h data, Throwable exception) {
logger.error(toJson(data), exception);
public void error(Log5w1h data, Throwable cause) {
withFields(logger.atError(), data).setCause(cause).log();
}

private String toJson(Log5w1h data) {
try {
return MAPPER.writeValueAsString(data);
} catch (Exception exception) {
return data.toString();
}
private LoggingEventBuilder withFields(LoggingEventBuilder builder, Log5w1h data) {
return builder.addKeyValue("where", data.where())
.addKeyValue("why", data.why())
.addKeyValue("who", data.who())
.addKeyValue("what", data.what())
.addKeyValue("how", data.how());
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package com.lmoraesdev.payment.config.logging;

import java.time.LocalDateTime;
import org.slf4j.MDC;

public class Logger5w1hBuilder {

private final Logger5w1h logger;

private String where;
private String why;
private String when;
private Object who;
private Object what;
private String who;
private String what;
private String how;

private Logger5w1hBuilder(Class<?> clazz) {
this.logger = Logger5w1h.of(clazz);
this.when = LocalDateTime.now().toString();
}

public static Logger5w1hBuilder create(Class<?> clazz) {
Expand All @@ -33,17 +28,12 @@ public Logger5w1hBuilder why(String value) {
return this;
}

public Logger5w1hBuilder when(String value) {
this.when = value;
return this;
}

public Logger5w1hBuilder who(Object value) {
public Logger5w1hBuilder who(String value) {
this.who = value;
return this;
}

public Logger5w1hBuilder what(Object value) {
public Logger5w1hBuilder what(String value) {
this.what = value;
return this;
}
Expand All @@ -65,13 +55,11 @@ public void warn() {
logger.warn(build());
}

public void error(Throwable e) {
logger.error(build(), e);
public void error(Throwable cause) {
logger.error(build(), cause);
}

private Log5w1h build() {
// traceId is injected automatically from Micrometer Tracing MDC —
// no caller needs to pass it explicitly.
return new Log5w1h(MDC.get("traceId"), MDC.get("spanId"), where, why, when, who, what, how);
return new Log5w1h(where, why, who, what, how);
}
}
5 changes: 3 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ logging:
level:
root: INFO
com.lmoraesdev.payment: DEBUG
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %X{traceId} %logger{36} - %msg%n"
structured:
format:
console: ecs
Loading