Stackdriver Logging - Google Cloud
文章推薦指數: 80 %
The Stackdriver Logging service collects and stores logs from applications and services on the Google Cloud Platform, giving you fine-grained, ... Index» File:OVERVIEW StackdriverLogging TheStackdriverLoggingservicecollectsandstoreslogsfromapplicationsand servicesontheGoogleCloudPlatform,givingyoufine-grained,programmatic controloveryourprojects'logs.YoucanusetheStackdriverLoggingAPIto: Readandfilterlogentries ExportyourlogentriestoCloudStorage, BigQuery,orCloudPub/Sub Createlogs-basedmetricsforusein CloudMonitoring Writelogentries ForgeneralinformationaboutStackdriverLogging,readStackdriverLogging Documentation. Thegoalofgoogle-cloudistoprovideanAPIthatiscomfortabletoRubyists. YourauthenticationcredentialsaredetectedautomaticallyinGoogleCloud Platform(GCP),includingGoogleComputeEngine(GCE),GoogleKubernetesEngine (GKE),GoogleAppEngine(GAE),GoogleCloudFunctions(GCF)andCloudRun.In otherenvironmentsyoucanconfigureauthenticationeasily,eitherdirectlyin yourcodeorviaenvironmentvariables.Readmoreabouttheoptionsfor connectingintheAuthenticationGuide. Listinglogentries StackdriverLogginggatherslogentriesfrommanyservices,includingGoogleApp EngineandGoogleComputeEngine.(SeetheListofLog Types.)Inaddition,you canwriteyourownlogentriestotheservice. Project#entriesreturnsthe Entryrecordsbelongingtoyourproject: require"google/cloud/logging" logging=Google::Cloud::Logging.new entries=logging.entries entries.eachdo|e| puts"[#{e.timestamp}]#{e.log_name}#{e.payload.inspect}" end Youcannarrowtheresultstoasinglelogusinganadvancedlogs filter.Alogisa namedcollectionofentries.LogscanbeproducedbyGoogleCloudPlatform services,bythird-partyservices,orbyyourapplications.Forexample,thelog compute.googleapis.com/activity_logisproducedbyGoogleComputeEngine.Logs aresimplyreferencedbynameingoogle-cloud.ThereisnoLogtypein google-cloudorLogresourceintheStackdriverLoggingAPI. require"google/cloud/logging" logging=Google::Cloud::Logging.new entries=logging.entriesfilter:"logName:syslog" entries.eachdo|e| puts"[#{e.timestamp}]#{e.payload.inspect}" end Youcanalsoorderthelogentriesbytimestamp. require"google/cloud/logging" logging=Google::Cloud::Logging.new entries=logging.entriesorder:"timestampdesc" entries.eachdo|e| puts"[#{e.timestamp}]#{e.log_name}" end Exportinglogentries StackdriverLoggingletsyouexportlogentriestodestinationsincludingGoogle CloudStoragebuckets(forlongtermlogstorage),GoogleBigQuerydatasets(for loganalysis),andGooglePub/Sub(forstreamingtootherapplications). Creatingsinks ASinkisanobjectthatletsyoutospecifya setoflogentriestoexport. Inadditiontothenameofthesinkandtheexportdestination, Project#create_sinkacceptsan advancedlogs filtertonarrow thecollection. Beforecreatingthesink,[email protected] permissiontowritelogstothedestination.SeeExportingLogs (V2). require"google/cloud/storage" require"google/cloud/logging" storage=Google::Cloud::Storage.new bucket=storage.create_bucket"my-logs-bucket" #GrantownerpermissiontoStackdriverLoggingservice email="[email protected]" bucket.acl.add_owner"group-#{email}" logging=Google::Cloud::Logging.new sink=logging.create_sink"my-sink", "storage.googleapis.com/#{bucket.id}" Whenyoucreateasink,onlynewlogentriesareexported.StackdriverLogging doesnotsendpreviously-ingestedlogentriestothesink'sdestination. Listingsinks Youcanalsolistthesinksbelongingtoyourprojectwith Project#sinks. require"google/cloud/logging" logging=Google::Cloud::Logging.new sinks=logging.sinks sinks.eachdo|s| puts"#{s.name}:#{s.filter}->#{s.destination}" end Creatinglogs-basedmetrics YoucanuselogentriesinyourprojectasthebasisforGoogleCloud Monitoringmetrics.Thesemetricscan thenbeusedtoproduceCloudMonitoringreportsandalerts. Creatingmetrics Ametricisameasuredvaluethatcanbeusedtoassessasystem.Use Project#create_metricto configureaMetricbasedonacollectionof logentriesmatchinganadvancedlogs filter. require"google/cloud/logging" logging=Google::Cloud::Logging.new metric=logging.create_metric"errors","severity>=ERROR" Listingmetrics Youcanalsolistthemetricsbelongingtoyourprojectwith Project#metrics. require"google/cloud/logging" logging=Google::Cloud::Logging.new metrics=logging.metrics metrics.eachdo|m| puts"#{m.name}:#{m.filter}" end Writinglogentries AnGoogle::Cloud::Logging::Entryiscomposedofmetadataandapayload.The payloadistraditionallyamessagestring,butinStackdriverLoggingitcan alsobeaJSONorprotocolbufferobject.Asinglelogcanhaveentrieswith differentpayloadtypes.Inadditiontothepayload,yourargument(s)to Project#write_entriesmustalso containalognameandaresource. require"google/cloud/logging" logging=Google::Cloud::Logging.new entry=logging.entry entry.payload="Jobstarted." entry.log_name="my_app_log" entry.resource.type="gae_app" entry.resource.labels[:module_id]="1" entry.resource.labels[:version_id]="20150925t173233" logging.write_entriesentry TowriteaJSONpayloadtothelog,simplypassahashargument: require"google/cloud/logging" logging=Google::Cloud::Logging.new entry=logging.entry entry.payload={"stats"=>{"a"=>8,"b"=>12.5}} entry.log_name="my_app_log" entry.resource.type="gae_app" entry.resource.labels[:module_id]="1" entry.resource.labels[:version_id]="20150925t173233" logging.write_entriesentry Ifyouwriteacollectionoflogentries,youcanprovidethelogname, resource,and/orlabelshashtobeusedforalloftheentries,andomitthese valuesfromtheindividualentries. require"google/cloud/logging" logging=Google::Cloud::Logging.new entry1=logging.entry entry1.payload="Jobstarted." entry2=logging.entry entry2.payload="Jobcompleted." labels={job_size:"large",job_code:"red"} resource=logging.resource"gae_app", "module_id"=>"1", "version_id"=>"20150925t173233" logging.write_entries[entry1,entry2], log_name:"my_app_log", resource:resource, labels:labels Normally,writinglogentriesisdonesynchronously;thecallto Project#write_entrieswillblock untilithaseithercompletedtransmittingthedataorencounteredanerror.To "fireandforget"withoutblocking,useAsyncWriter;itspinsupabackgroundthreadthatwriteslogentriesin batches.CallstoAsyncWriter#write_entriessimplyaddentriestoitsworkqueueandreturn immediately. require"google/cloud/logging" logging=Google::Cloud::Logging.new async=logging.async_writer entry1=logging.entry entry1.payload="Jobstarted." entry2=logging.entry entry2.payload="Jobcompleted." labels={job_size:"large",job_code:"red"} resource=logging.resource"gae_app", "module_id"=>"1", "version_id"=>"20150925t173233" async.write_entries[entry1,entry2], log_name:"my_app_log", resource:resource, labels:labels, partial_success:true CreatingaRubyLoggerimplementation IfyourenvironmentrequiresaloggerinstancethatisAPI-compatiblewith Ruby'sstandardlibraryLogger, youcanuseProject#loggertocreate one. require"google/cloud/logging" logging=Google::Cloud::Logging.new resource=logging.resource"gae_app", module_id:"1", version_id:"20150925t173233" logger=logging.logger"my_app_log",resource,env::production logger.info"Jobstarted." Bydefault,theloggerinstancewriteslogentriesasynchronouslyina backgroundthreadusinganAsyncWriter.If youwanttocustomizeordisableasynchronouswriting,youmaycalltheLogger constructordirectly. require"google/cloud/logging" logging=Google::Cloud::Logging.new resource=logging.resource"gae_app", module_id:"1", version_id:"20150925t173233" logger=Google::Cloud::Logging::Logger.newlogging, "my_app_log", resource, {env::production} logger.info"Logentrywrittensynchronously." Configuringtimeout Youcanconfiguretherequesttimeoutvalueinseconds. require"google/cloud/logging" logging=Google::Cloud::Logging.newtimeout:120 Additionalinformation StackdriverLoggingcanbeconfiguredtobeusedinRackapplicationsortouse gRPC'slogging.Tolearnmore,seetheInstrumentationGuideandLoggingguide. GeneratedonThuOct2117:23:472021by yard 0.9.26(ruby-2.6.7).
延伸文章資訊
- 1Increase Log Retention Using Google Cloud Logging CLI
Unfortunately the default log retention period is 30 days on Google Cloud Platform (GCP), but you...
- 2googleapis/google-cloud-php-logging - GitHub
Contribute to googleapis/google-cloud-php-logging development by creating an account on GitHub.
- 311. Stackdriver Logging Support - Spring
Stackdriver Logging is the managed logging service provided by Google Cloud Platform. This module...
- 4Cloud Logging
Use Logs Explorer to view your logs from Google Cloud services and others in the Console · Get de...
- 5Stackdriver Logging - Google Cloud
The Stackdriver Logging service collects and stores logs from applications and services on the Go...