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"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"cloud.google.com/go/logging"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/gcpshared"
|
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
|
|
|
|
func NewLogger(ctx context.Context, providerID string, logName string) (*Logger, error) {
|
2022-06-10 07:18:30 -04:00
|
|
|
projectID, _, _, err := gcpshared.SplitProviderID(providerID)
|
2022-06-03 05:55:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|