Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding labels to Cloud Logging logs using Spring Cloud GCP #3553

Open
Alos opened this issue Feb 12, 2025 · 1 comment
Open

Adding labels to Cloud Logging logs using Spring Cloud GCP #3553

Alos opened this issue Feb 12, 2025 · 1 comment
Labels
priority: p3 type: enhancement New feature or request

Comments

@Alos
Copy link
Contributor

Alos commented Feb 12, 2025

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.

@meltsufin
Copy link
Member

Currently, this can be done with a custom JsonLoggingEventEnhancer.

package com.example;

import ch.qos.logback.classic.spi.ILoggingEvent;
import com.google.cloud.spring.logging.JsonLoggingEventEnhancer;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Marker;

public class CustomJsonLoggingEventEnhancer implements JsonLoggingEventEnhancer {

  @Override
  public void enhanceJsonLogEntry(Map<String, Object> jsonMap, ILoggingEvent event) {
    if (!event.getMarkerList().isEmpty()) {
      // convert the list of markers to a map where the keys are marker1, marker2, ...
      Map<String, String> markers = new HashMap<>();
      for (int i = 0; i < event.getMarkerList().size(); i++) {
        Marker marker = event.getMarkerList().get(i);
        markers.put("marker" + (i + 1), marker.getName());
      }

      jsonMap.put("labels", markers);
    }
  }
}

logback-spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="CONSOLE_JSON" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
      <layout class="com.google.cloud.spring.logging.StackdriverJsonLayout">
      <loggingEventEnhancer>com.example.CustomJsonLoggingEventEnhancer</loggingEventEnhancer>
      </layout>
    </encoder>
  </appender>

  <root level="INFO">
    <appender-ref ref="CONSOLE_JSON" />
  </root>
</configuration>

In application code:

    Marker myMarker = MarkerFactory.getMarker("myMarker");
    String message = "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"}}

This should be compatible with structured JSON logging on GKE: https://cloud.google.com/logging/docs/structured-logging


I think it makes sense to make it supported by default in StackdriverJsonLayout. So, I would leave this open as a feature request.

@meltsufin meltsufin added the type: enhancement New feature or request label Feb 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants