Package com.google.cloud.spring.logging
Class TraceIdLoggingEnhancer
java.lang.Object
com.google.cloud.spring.logging.TraceIdLoggingEnhancer
- All Implemented Interfaces:
com.google.cloud.logging.logback.LoggingEventEnhancer,com.google.cloud.logging.LoggingEnhancer
public class TraceIdLoggingEnhancer
extends Object
implements com.google.cloud.logging.LoggingEnhancer, com.google.cloud.logging.logback.LoggingEventEnhancer
Adds the trace ID and span ID to the logging entry, in its correct format to be displayed in the
Logs viewer.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidenhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder) Set the trace and span ID fields of the log entry to the current one.voidenhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder, ch.qos.logback.classic.spi.ILoggingEvent e) See:enhanceLogEntry(LogEntry.Builder)This method adds support forAsyncAppenderwhen retrieving MDC properties.static StringGet the current trace id.static voidSets or removes the current trace ID.
-
Constructor Details
-
TraceIdLoggingEnhancer
public TraceIdLoggingEnhancer()
-
-
Method Details
-
setCurrentTraceId
Sets or removes the current trace ID.- Parameters:
id- current ID or null to unset
-
getCurrentTraceId
Get the current trace id.- Returns:
- the trace ID stored through
setCurrentTraceId(String)
-
enhanceLogEntry
public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder) Set the trace and span ID fields of the log entry to the current one.The current trace ID is either the trace ID stored in the Mapped Diagnostic Context (MDC) under the "X-B3-TraceId" key or, if none set, the current trace ID set by
setCurrentTraceId(String). The current span ID is retrieved from the MDC under the "X-B3-SpanId" key, if set.The trace ID is set in the log entry in the "projects/[GCP_PROJECT_ID]/traces/[TRACE_ID]" format, in order to be associated to traces by the Google Cloud Console.
If an application is running on Google App Engine, the trace ID is also stored in the "appengine.googleapis.com/trace_id" field, in order to enable log correlation on the logs viewer.
- Specified by:
enhanceLogEntryin interfacecom.google.cloud.logging.LoggingEnhancer- Parameters:
builder- log entry builder
-
enhanceLogEntry
public void enhanceLogEntry(com.google.cloud.logging.LogEntry.Builder builder, ch.qos.logback.classic.spi.ILoggingEvent e) See:enhanceLogEntry(LogEntry.Builder)This method adds support forAsyncAppenderwhen retrieving MDC properties.- Specified by:
enhanceLogEntryin interfacecom.google.cloud.logging.logback.LoggingEventEnhancer- Parameters:
builder- log entry buildere- logging event
-