Cloud Logging 可讓您儲存、搜尋、分析、監控 Google Cloud 和 Amazon Web Services 中的記錄資料與事件,並發出相關快訊。 Google Cloud 本頁面說明如何使用 PowerShell 管理記錄。其中提供的簡單範例將說明如何建立記錄、記錄接收器和記錄指標。
如要進一步瞭解 Logging cmdlet,請參閱 Cloud Tools for PowerShell cmdlet 參考資料。如要進一步瞭解 Logging 的一般資訊,請參閱「Logging 總覽」指南。
建立記錄和記錄項目
記錄是專案中記錄項目的統稱。記錄項目會記錄狀態或事件。項目可能由 Google Cloud服務、AWS 服務、第三方應用程式或您自己的應用程式建立。記錄項目內包含的「訊息」稱為酬載,可以是簡單的字串或結構化資料。每個記錄項目都包含受控資源的名稱,以此指示記錄項目的來源。
您可以使用 New‑GcLogEntry
cmdlet 建立記錄項目。您必須指定該項目所屬的記錄 (如果該記錄不存在,系統會自動建立)。如要將記錄與受控資源建立關聯,您可以使用 -MonitoredResource
參數。記錄項目預設會與「全域」資源建立關聯。如要建立受控資源,請使用 New‑GcLogMonitoredResource
指令碼。
# Creates a log entry in the log "my-log". New-GcLogEntry -LogName "my-log" -TextPayload "This is a log entry." # Creates a log entry associated with a Cloud SQL monitored resource $resource = New-GcLogMonitoredResource -ResourceType "cloudsql_database" ` -Labels @{"project_id" = "my-project"; "database_id" = "id"} New-GcLogEntry -LogName "my-log" ` -TextPayload "This is a log entry." ` -MonitoredResource $resource
您可以使用 Get‑GcLogEntry.
cmdlet 擷取記錄項目。
# Gets all entries from log "my-log" Get-GcLogEntry -LogName "my-log" # Gets all entries associated with Compute Engine instances Get-GcLogEntry -ResourceName "gce_instance"
建立記錄接收器
如要匯出記錄項目,可以使用 New‑GcLogSink
cmdlet 建立記錄接收器。Stackdriver Logging 會比對收到的記錄項目與您的接收器,接著,所有與各接收器相符的記錄項目就會複製到相關聯的目的地。在接收器建立之前就存在的記錄項目則不會匯出。
匯出記錄的目的地可以是 Cloud Storage 值區、BigQuery 資料集或 Pub/Sub 主題。
# Creates a log sink for log entries in the default project. # The entries will be sent to the Cloud Storage bucket "my-bucket" New-GcLogSink -Sink "my-sink" -GcsBucketDestination "my-bucket" # Creates a log sink for log entries in log "my-log". # The entries will be sent to the BigQuery data set "my_dataset" New-GcLogSink -Sink "my-sink" ` -LogName "my-log" ` -BigQueryDataSetDestination "my_dataset" # Creates a log sink for log entries that match the filter. # The entries will be sent to the Pub/Sub topic "my-topic". New-GcLogSink -Sink "my-sink" ` -Filter "textPayload = `"Testing`"" ` -PubSubTopicDestination "my-topic"
建立記錄指標
您可以使用 New‑GcLogMetric
cmdlet 建立記錄指標,藉此計算有多少記錄項目符合特定條件。這些指標可用於在 Stackdriver Monitoring 中建立圖表和快訊政策。
# Creates a metric for entries in log "my-log". New-GcLogMetric -Metric "my-metric" -LogName "my-log" # Creates a metric for entries associated with Compute Engine instances New-GcLogMetric -Metric "my-metric" -ResourceType "gce_instance" # Creates a metric for entries that match the filter. New-GcLogMetric -Metric "my-metric" -Filter "textPayload = `"Testing`""