Class StackdriverJsonLayout

java.lang.Object
ch.qos.logback.core.spi.ContextAwareBase
ch.qos.logback.core.LayoutBase<E>
ch.qos.logback.contrib.json.JsonLayoutBase<ch.qos.logback.classic.spi.ILoggingEvent>
ch.qos.logback.contrib.json.classic.JsonLayout
com.google.cloud.spring.logging.StackdriverJsonLayout
All Implemented Interfaces:
ch.qos.logback.core.Layout<ch.qos.logback.classic.spi.ILoggingEvent>, ch.qos.logback.core.spi.ContextAware, ch.qos.logback.core.spi.LifeCycle

public class StackdriverJsonLayout extends ch.qos.logback.contrib.json.classic.JsonLayout
This class provides a JSON layout for a Logback appender compatible to the Stackdriver log format.

Reference: https://cloud.google.com/logging/docs/agent/configuration#process-payload

  • Field Summary

    Fields inherited from class ch.qos.logback.contrib.json.classic.JsonLayout

    CONTEXT_ATTR_NAME, EXCEPTION_ATTR_NAME, FORMATTED_MESSAGE_ATTR_NAME, includeContextName, includeException, includeFormattedMessage, includeLevel, includeLoggerName, includeMDC, includeMessage, includeThreadName, LEVEL_ATTR_NAME, LOGGER_ATTR_NAME, MDC_ATTR_NAME, MESSAGE_ATTR_NAME, THREAD_ATTR_NAME, TIMESTAMP_ATTR_NAME

    Fields inherited from class ch.qos.logback.contrib.json.JsonLayoutBase

    appendLineSeparator, CONTENT_TYPE, includeTimestamp, jsonFormatter, timestampFormat, timestampFormatTimezoneId

    Fields inherited from class ch.qos.logback.core.LayoutBase

    started

    Fields inherited from class ch.qos.logback.core.spi.ContextAwareBase

    context
  • Constructor Summary

    Constructors
    Constructor
    Description
    creates a layout for a Logback appender compatible to the Stackdriver log format.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addLoggingEventEnhancer(String enhancerClassName)
    Add additional logging enhancers that implement JsonLoggingEventEnhancer.
    protected String
     
    Get the project id.
    Get the MDC field name for span id.
    Get the MDC filed name for trace id.
    boolean
    check if there is an included exception in the message.
    boolean
    check if the span id is included.
    boolean
    check if the trace id is included.
    void
    set custom json data to include in log output.
    void
    setIncludeExceptionInMessage(boolean includeExceptionInMessage)
    set whether the exception is included in the message.
    void
    setIncludeSpanId(boolean includeSpanId)
    set whether the span id is included.
    void
    setIncludeTraceId(boolean includeTraceId)
    set whether the trace id is included.
    void
    setProjectId(String projectId)
    set the project id.
    void
    set the service context for stackdriver.
    void
    setSpanIdMdcField(String spanIdMdcField)
    Set the MDC field name for span id.
    void
    setTraceIdMdcField(String traceIdMdcField)
    Set the MDC filed name for trace id.
    void
     
    protected Map<String,Object>
    toJsonMap(ch.qos.logback.classic.spi.ILoggingEvent event)
    Convert a logging event into a Map.

    Methods inherited from class ch.qos.logback.contrib.json.classic.JsonLayout

    addCustomDataToJsonMap, addThrowableInfo, getThrowableProxyConverter, isIncludeContextName, isIncludeException, isIncludeFormattedMessage, isIncludeLevel, isIncludeLoggerName, isIncludeMDC, isIncludeMessage, isIncludeThreadName, setIncludeContextName, setIncludeException, setIncludeFormattedMessage, setIncludeLevel, setIncludeLoggerName, setIncludeMDC, setIncludeMessage, setIncludeThreadName, setThrowableProxyConverter, stop

    Methods inherited from class ch.qos.logback.contrib.json.JsonLayoutBase

    add, addMap, addTimestamp, createDateFormat, doLayout, format, formatTimestamp, getContentType, getJsonFormatter, getTimestampFormat, getTimestampFormatTimezoneId, isAppendLineSeparator, isIncludeTimestamp, setAppendLineSeparator, setIncludeTimestamp, setJsonFormatter, setTimestampFormat, setTimestampFormatTimezoneId

    Methods inherited from class ch.qos.logback.core.LayoutBase

    getContext, getFileFooter, getFileHeader, getPresentationFooter, getPresentationHeader, isStarted, setContext, setFileFooter, setFileHeader, setPresentationFooter, setPresentationHeader

    Methods inherited from class ch.qos.logback.core.spi.ContextAwareBase

    addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn, getDeclaredOrigin, getStatusManager

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface ch.qos.logback.core.spi.ContextAware

    addError, addError, addInfo, addInfo, addStatus, addWarn, addWarn
  • Constructor Details

    • StackdriverJsonLayout

      public StackdriverJsonLayout()
      creates a layout for a Logback appender compatible to the Stackdriver log format.
  • Method Details

    • getProjectId

      public String getProjectId()
      Get the project id.
      Returns:
      the Google Cloud project id relevant for logging the traceId
    • setProjectId

      public void setProjectId(String projectId)
      set the project id.
      Parameters:
      projectId - the Google Cloud project id relevant for logging the traceId
    • getTraceIdMdcField

      public String getTraceIdMdcField()
      Get the MDC filed name for trace id.
      Returns:
      the MDC field name for retrieving a trace id
      Since:
      2.0.5
    • setTraceIdMdcField

      public void setTraceIdMdcField(String traceIdMdcField)
      Set the MDC filed name for trace id.
      Parameters:
      traceIdMdcField - the MDC field name for retrieving a trace id
      Since:
      2.0.5
    • getSpanIdMdcField

      public String getSpanIdMdcField()
      Get the MDC field name for span id.
      Returns:
      the MDC field name for retrieving a span id
      Since:
      2.0.5
    • setSpanIdMdcField

      public void setSpanIdMdcField(String spanIdMdcField)
      Set the MDC field name for span id.
      Parameters:
      spanIdMdcField - the MDC field name for retrieving a span id
      Since:
      2.0.5
    • isIncludeTraceId

      public boolean isIncludeTraceId()
      check if the trace id is included.
      Returns:
      true if the traceId should be included into the JSON
    • setIncludeTraceId

      public void setIncludeTraceId(boolean includeTraceId)
      set whether the trace id is included.
      Parameters:
      includeTraceId - true if the traceId should be included into the JSON
    • isIncludeSpanId

      public boolean isIncludeSpanId()
      check if the span id is included.
      Returns:
      true if the spanId should be included into the JSON
    • setIncludeSpanId

      public void setIncludeSpanId(boolean includeSpanId)
      set whether the span id is included.
      Parameters:
      includeSpanId - true if the spanId should be included into the JSON
    • isIncludeExceptionInMessage

      public boolean isIncludeExceptionInMessage()
      check if there is an included exception in the message.
      Returns:
      true if the exception should be added to the message
    • setIncludeExceptionInMessage

      public void setIncludeExceptionInMessage(boolean includeExceptionInMessage)
      set whether the exception is included in the message.
      Parameters:
      includeExceptionInMessage - true if the exception should be added to the message
    • setServiceContext

      public void setServiceContext(StackdriverErrorReportingServiceContext serviceContext)
      set the service context for stackdriver.
      Parameters:
      serviceContext - the service context
      Since:
      1.2
    • setCustomJson

      public void setCustomJson(String json)
      set custom json data to include in log output.
      Parameters:
      json - json string
      Since:
      1.2
    • addLoggingEventEnhancer

      public void addLoggingEventEnhancer(String enhancerClassName)
      Add additional logging enhancers that implement JsonLoggingEventEnhancer.
      Parameters:
      enhancerClassName - class name of the layout enhancer
    • start

      public void start()
      Specified by:
      start in interface ch.qos.logback.core.spi.LifeCycle
      Overrides:
      start in class ch.qos.logback.contrib.json.classic.JsonLayout
    • toJsonMap

      protected Map<String,Object> toJsonMap(ch.qos.logback.classic.spi.ILoggingEvent event)
      Convert a logging event into a Map.
      Overrides:
      toJsonMap in class ch.qos.logback.contrib.json.classic.JsonLayout
      Parameters:
      event - the logging event
      Returns:
      the map which should get rendered as JSON
    • formatTraceId

      protected String formatTraceId(String traceId)