You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using Spring Cloud GCP and running in a GKE cluster. Is there a way to support Slf4j's markers to mark a log with a specific label? Or any way to populate the label field in Cloud Logging? I only need SOME of my logs to have a special label.
The text was updated successfully, but these errors were encountered:
Currently, this can be done with a custom JsonLoggingEventEnhancer.
packagecom.example;
importch.qos.logback.classic.spi.ILoggingEvent;
importcom.google.cloud.spring.logging.JsonLoggingEventEnhancer;
importjava.util.HashMap;
importjava.util.Map;
importorg.slf4j.Marker;
publicclassCustomJsonLoggingEventEnhancerimplementsJsonLoggingEventEnhancer {
@OverridepublicvoidenhanceJsonLogEntry(Map<String, Object> jsonMap, ILoggingEventevent) {
if (!event.getMarkerList().isEmpty()) {
// convert the list of markers to a map where the keys are marker1, marker2, ...Map<String, String> markers = newHashMap<>();
for (inti = 0; i < event.getMarkerList().size(); i++) {
Markermarker = event.getMarkerList().get(i);
markers.put("marker" + (i + 1), marker.getName());
}
jsonMap.put("labels", markers);
}
}
}
MarkermyMarker = MarkerFactory.getMarker("myMarker");
Stringmessage = "This is a message with a marker.";
LOGGER.info(myMarker, message);
It produces structured logs that look like this:
{"timestampSeconds":1739413691,"timestampNanos":769040000,"severity":"INFO","thread":"http-nio-8080-exec-1","logger":"com.example.ExampleController","message":"This is a message with a marker.","context":"default","labels":{"marker1":"myMarker"}}
When using Spring Cloud GCP and running in a GKE cluster. Is there a way to support Slf4j's markers to mark a log with a specific label? Or any way to populate the label field in Cloud Logging? I only need SOME of my logs to have a special label.
The text was updated successfully, but these errors were encountered: