Logging 작업

Cloud Logging을 사용하면 Google Cloud와 AWS(Amazon Web Services)의 로그 데이터 및 이벤트를 저장, 검색, 분석, 모니터링하고 알림을 받을 수 있습니다. 이 페이지에서는 PowerShell을 사용하여 Logging을 관리하는 방법을 보여줍니다. 로그, 로그 싱크, 로그 측정항목을 작성하는 간단한 예를 살펴봅니다.

Logging cmdlet에 대한 자세한 내용은 PowerShell용 Cloud Tools cmdlet 참조를 확인하세요. Logging에 대한 일반적인 내용은 Logging 개요 가이드를 참조하세요.

로그 및 로그 항목 만들기

로그는 프로젝트 내의 여러 로그 항목을 모아 이름을 붙인 집합입니다. 로그 항목은 상태나 이벤트를 기록합니다. 이러한 항목은 Google Cloud 서비스, AWS 서비스, 타사 애플리케이션 또는 자체 애플리케이션에서 생성될 수 있습니다. 로그 항목이 전달하는 '메시지'를 페이로드라고 하는데, 이는 간단한 문자열일 수도 있고 구조화된 데이터일 수도 있습니다. 각 로그 항목은 모니터링 리소스의 이름을 포함하여 출처를 표시합니다.

cmdlet New‑GcLogEntry는 로그 항목을 만드는 데 사용될 수 있습니다. 항목이 속한 로그를 지정해야 합니다(로그가 존재하지 않으면 새로 생성됨). 로그를 모니터링 리소스에 연결하려면 -MonitoredResource 매개변수를 사용하면 됩니다. 기본적으로 로그 항목은 '전역' 리소스와 연결됩니다. 모니터링 리소스를 만들려면 New‑GcLogMonitoredResource cmdlet을 사용합니다.

# 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

cmdlet Get‑GcLogEntry.를 사용하여 로그 항목을 검색할 수 있습니다.

# 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"

로그 싱크 만들기

로그 항목을 내보내려면 cmdlet New‑GcLogSink를 사용하여 로그 싱크를 만들 수 있습니다. 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"

로그 측정항목 만들기

cmdlet New‑GcLogMetric을 사용하여 특정 기준과 일치하는 로그 항목 수를 계산하는 로그 측정항목을 만들 수 있습니다. 이 측정항목을 사용하여 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`""