Stackdriver Logging is the managed logging service provided by Google Cloud Platform. This module provides support for associating a web request trace ID ...
11. StackdriverLoggingSupportPrev Next11. StackdriverLoggingSupportMavencoordinates,usingSpringCloudGCPBOM:
org.springframework.cloud
spring-cloud-gcp-starter-logging
Gradlecoordinates:dependencies{
compilegroup:'org.springframework.cloud',name:'spring-cloud-gcp-starter-logging'
}StackdriverLoggingisthemanagedloggingserviceprovidedbyGoogleCloud
Platform.ThismoduleprovidessupportforassociatingawebrequesttraceIDwiththecorrespondinglogentries.
ItdoessobyretrievingtheX-B3-TraceIdvaluefromtheMappedDiagnosticContext(MDC),whichissetbySpringCloudSleuth.
IfSpringCloudSleuthisn’tused,theconfiguredTraceIdExtractorextractsthedesiredheadervalueandsetsitasthelogentry’straceID.
Thisallowsgroupingoflogmessagesbyrequest,forexample,intheGoogleCloudConsoleLogsviewer.NoteDuetothewayloggingissetup,theGCPprojectIDandcredentialsdefinedinapplication.propertiesareignored.
Instead,youshouldsettheGOOGLE_CLOUD_PROJECTandGOOGLE_APPLICATION_CREDENTIALSenvironmentvariablestotheprojectIDandcredentialsprivatekeylocation,respectively.
Youcandothiseasilyifyou’reusingtheGoogleCloudSDK,usingthegcloudconfigsetproject[YOUR_PROJECT_ID]andgcloudauthapplication-defaultlogincommands,respectively.Asampleapplicationisavailable.11.1 WebMVCInterceptorForuseinWebMVC-basedapplications,TraceIdLoggingWebMvcInterceptorisprovidedthatextractstherequesttraceIDfromanHTTPrequestusingaTraceIdExtractorandstoresitinathread-local,whichcanthenbeusedinaloggingappendertoaddthetraceIDmetadatatologmessages.WarningIfSpringCloudGCPTraceisenabled,theloggingmoduledisablesitselfanddelegateslogcorrelationtoSpringCloudSleuth.LoggingWebMvcConfigurerconfigurationclassisalsoprovidedtohelpregistertheTraceIdLoggingWebMvcInterceptor
inSpringMVCapplications.ApplicationshostedontheGoogleCloudPlatformincludetraceIDsunderthex-cloud-trace-contextheader,whichwillbeincludedinlogentries.
However,ifSleuthisusedthetraceIDwillbepickedupfromtheMDC.11.2 LogbackSupportCurrently,onlyLogbackissupportedandthereare2possibilitiestologtoStackdriverviathislibrarywithLogback:
viadirectAPIcallsandthroughJSON-formattedconsolelogs.11.2.1 LogviaAPIAStackdriverappenderisavailableusingorg/springframework/cloud/gcp/autoconfigure/logging/logback-appender.xml.
ThisappenderbuildsaStackdriverLogginglogentryfromaJULorLogbacklogentry,addsatraceIDtoitandsendsittoStackdriverLogging.STACKDRIVER_LOG_NAMEandSTACKDRIVER_LOG_FLUSH_LEVELenvironmentvariablescanbeusedtocustomizetheSTACKDRIVERappender.11.2.2 LogviaConsoleForLogback,aorg/springframework/cloud/gcp/autoconfigure/logging/logback-json-appender.xmlfileismadeavailableforimporttomakeiteasiertoconfiguretheJSONLogbackappender.Yourconfigurationmaythenlooksomethinglikethis:
IfyourapplicationisrunningonGoogleContainerEngine,GoogleComputeEngineorGoogleAppEngineFlexible,yourconsoleloggingisautomaticallysavedtoGoogleStackdriverLogging.
Therefore,youcanjustincludeorg/springframework/cloud/gcp/autoconfigure/logging/logback-json-appender.xmlinyourloggingconfiguration,whichlogsJSONentriestotheconsole.
Thetraceidwillbesetcorrectly.YourLogbackconfigurationmaythenlooksomethinglikethis:
Ifyouwanttohavemorecontroloverthelogoutput,youcanalsoconfiguretheConsoleAppenderyourself.
Thefollowingpropertiesareavailable:PropertyDefaultValueDescriptionprojectIdIfnotset,defaultvalueisdeterminedinthefollowingorder:
SPRING_CLOUD_GCP_LOGGING_PROJECT_IDEnvironmentalVariable.ValueofDefaultGcpProjectIdProvider.getProjectId()ThisisusedtogeneratefullyqualifiedStackdriverTraceIDformat:projects/[PROJECT-ID]/traces/[TRACE-ID].
ThisformatisrequiredtocorrelatetracebetweenStackdriverTraceandStackdriverLogging.
IfprojectIdisnotsetandcannotbedetermined,thenit’lllogtraceIdwithoutthefullyqualifiedformat.includeTraceIdtrueShouldthetraceIdbeincludedincludeSpanIdtrueShouldthespanIdbeincludedincludeLeveltrueShouldtheseveritybeincludedincludeThreadNametrueShouldthethreadnamebeincludedincludeMDCtrueShouldallMDCpropertiesbeincluded.TheMDCpropertiesX-B3-TraceId,X-B3-SpanIdandX-Span-ExportprovidedbySpringSleuthwillgetexcludedastheygethandledseparatelyincludeLoggerNametrueShouldthenameoftheloggerbeincludedincludeFormattedMessagetrueShouldtheformattedlogmessagebeincluded.includeExceptionInMessagetrueShouldthestacktracebeappendedtotheformattedlogmessage.ThissettingisonlyevaluatedifincludeFormattedMessageistrueincludeContextNametrueShouldtheloggingcontextbeincludedincludeMessagefalseShouldthelogmessagewithblankplaceholdersbeincludedincludeExceptionfalseShouldthestacktracebeincludedasaownfieldThisisanexampleofsuchanLogbackconfiguration:
${projectId}
Prev Next10. SpringCloudSleuth Home 12. CloudFoundry