go module 생성하기
mkdir go-logging & cd go-logging
go mod init main
cloud logging 패키지 설치하기
go get cloud.google.com/go/logging
간단 코드 작성하기
// vim main.go
// Sample stdlogging writes log.Logger logs to the Cloud Logging.
package main
import (
"context"
"log"
"cloud.google.com/go/logging"
)
func main() {
ctx := context.Background()
// Sets your Google Cloud Platform project ID.
projectID := "GCP 프로젝트 ID를 입력해주세요"
// Creates a client.
client, err := logging.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
// Sets the name of the log to write to.
logName := "my-log"
logger := client.Logger(logName).StandardLogger(logging.Info)
// Logs "hello world", log entry is visible at
// Cloud Logs.
logger.Println("hello world")
}
package main
import (
"context"
"log"
"cloud.google.com/go/logging"
"google.golang.org/api/option"
)
func main() {
ctx := context.Background()
// Sets your Google Cloud Platform project ID.
projectID := "gcp-pjt-share-2022-08-26"
// Creates a client.
client, err := logging.NewClient(ctx, projectID,option.WithCredentialsFile("service account json 파일 위치를 입력해주세요"))
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer client.Close()
// Sets the name of the log to write to.
logName := "my-log"
labels := map[string]string{
"app": "my-app",
}
option := logging.CommonLabels(labels)
logger := client.Logger(logName,option).StandardLogger(logging.Info)
logger.Print("hello world service account")
}