2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-03 05:55:18 -04:00
|
|
|
package gcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-09 08:43:48 -05:00
|
|
|
"fmt"
|
2022-06-03 05:55:18 -04:00
|
|
|
"log"
|
|
|
|
|
2022-11-09 08:43:48 -05:00
|
|
|
"cloud.google.com/go/compute/metadata"
|
2022-06-03 05:55:18 -04:00
|
|
|
"cloud.google.com/go/logging"
|
|
|
|
)
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// Logger logs to GCP cloud logging. Do not use to log sensitive information.
|
2022-06-03 05:55:18 -04:00
|
|
|
type Logger struct {
|
|
|
|
client *logging.Client
|
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogger creates a new Cloud Logger for GCP.
|
|
|
|
// https://cloud.google.com/logging/docs/setup/go
|
2022-11-09 08:43:48 -05:00
|
|
|
func NewLogger(ctx context.Context, logName string) (*Logger, error) {
|
|
|
|
projectID, err := metadata.NewClient(nil).ProjectID()
|
2022-06-03 05:55:18 -04:00
|
|
|
if err != nil {
|
2022-11-09 08:43:48 -05:00
|
|
|
return nil, fmt.Errorf("retrieving project ID from imds: %w", err)
|
2022-06-03 05:55:18 -04:00
|
|
|
}
|
2022-11-09 08:43:48 -05:00
|
|
|
|
2022-06-03 05:55:18 -04:00
|
|
|
client, err := logging.NewClient(ctx, projectID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := client.Logger(logName).StandardLogger(logging.Info)
|
|
|
|
|
|
|
|
return &Logger{
|
|
|
|
client: client,
|
|
|
|
logger: logger,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disclose stores log information in GCP Cloud Logging! Do **NOT** log sensitive
|
|
|
|
// information!
|
|
|
|
func (l *Logger) Disclose(msg string) {
|
|
|
|
l.logger.Println(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close waits for all buffer to be written.
|
|
|
|
func (l *Logger) Close() error {
|
|
|
|
return l.client.Close()
|
|
|
|
}
|