mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-03-26 19:45:06 +00:00
Add context logger utilities for enhanced logging support
Signed-off-by: Priyansh Choudhary <im1706@gmail.com>
This commit is contained in:
@@ -38,12 +38,11 @@ func (c *AzureBackend) Setup(ctx context.Context, flags map[string]string, logge
|
||||
c.option = azure.Option{
|
||||
Config: flags,
|
||||
Limits: setupLimits(ctx, flags),
|
||||
Logger: logger,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *AzureBackend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
|
||||
c.option.Logger = logger
|
||||
ctx = udmrepo.WithLogger(ctx, logger)
|
||||
return azure.NewStorage(ctx, &c.option, false)
|
||||
}
|
||||
|
||||
@@ -19,8 +19,6 @@ package azure
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/kopia/kopia/repo/blob"
|
||||
"github.com/kopia/kopia/repo/blob/azure"
|
||||
"github.com/kopia/kopia/repo/blob/throttling"
|
||||
@@ -34,13 +32,12 @@ const (
|
||||
)
|
||||
|
||||
func init() {
|
||||
blob.AddSupportedStorage(storageType, Option{Logger: logrus.New()}, NewStorage)
|
||||
blob.AddSupportedStorage(storageType, Option{}, NewStorage)
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
Config map[string]string `json:"config" kopia:"sensitive"`
|
||||
Limits throttling.Limits
|
||||
Logger logrus.FieldLogger
|
||||
}
|
||||
|
||||
type Storage struct {
|
||||
@@ -58,7 +55,10 @@ func (s *Storage) ConnectionInfo() blob.ConnectionInfo {
|
||||
func NewStorage(ctx context.Context, option *Option, isCreate bool) (blob.Storage, error) {
|
||||
cfg := option.Config
|
||||
|
||||
client, _, err := azureutil.NewStorageClient(option.Logger, cfg)
|
||||
// Get logger from context
|
||||
logger := udmrepo.LoggerFromContext(ctx)
|
||||
|
||||
client, _, err := azureutil.NewStorageClient(logger, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -73,6 +73,8 @@ func NewStorage(ctx context.Context, option *Option, isCreate bool) (blob.Storag
|
||||
return nil, err
|
||||
}
|
||||
|
||||
logger.Info("Successfully created Azure storage backend")
|
||||
|
||||
return &Storage{
|
||||
Option: option,
|
||||
Storage: azStorage,
|
||||
|
||||
@@ -50,6 +50,8 @@ func (c *FsBackend) Setup(ctx context.Context, flags map[string]string, logger l
|
||||
c.options.FileMode = defaultFileMode
|
||||
c.options.DirectoryMode = defaultDirMode
|
||||
|
||||
ctx = udmrepo.WithLogger(ctx, logger)
|
||||
|
||||
c.options.Limits = setupLimits(ctx, flags)
|
||||
|
||||
return nil
|
||||
@@ -59,6 +61,7 @@ func (c *FsBackend) Connect(ctx context.Context, isCreate bool, logger logrus.Fi
|
||||
if !filepath.IsAbs(c.options.Path) {
|
||||
return nil, errors.Errorf("filesystem repository path is not absolute, path: %s", c.options.Path)
|
||||
}
|
||||
ctx = udmrepo.WithLogger(ctx, logger)
|
||||
|
||||
return filesystem.New(ctx, &c.options, isCreate)
|
||||
}
|
||||
|
||||
@@ -46,11 +46,14 @@ func (c *GCSBackend) Setup(ctx context.Context, flags map[string]string, logger
|
||||
c.options.Prefix = optionalHaveString(udmrepo.StoreOptionPrefix, flags)
|
||||
c.options.ReadOnly = optionalHaveBool(ctx, udmrepo.StoreOptionGcsReadonly, flags)
|
||||
|
||||
ctx = udmrepo.WithLogger(ctx, logger)
|
||||
|
||||
c.options.Limits = setupLimits(ctx, flags)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *GCSBackend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
|
||||
ctx = udmrepo.WithLogger(ctx, logger)
|
||||
return gcs.New(ctx, &c.options, false)
|
||||
}
|
||||
|
||||
@@ -48,11 +48,14 @@ func (c *S3Backend) Setup(ctx context.Context, flags map[string]string, logger l
|
||||
c.options.SessionToken = optionalHaveString(udmrepo.StoreOptionS3Token, flags)
|
||||
c.options.RootCA = optionalHaveBase64(ctx, udmrepo.StoreOptionCACert, flags)
|
||||
|
||||
ctx = udmrepo.WithLogger(ctx, logger)
|
||||
|
||||
c.options.Limits = setupLimits(ctx, flags)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *S3Backend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
|
||||
ctx = udmrepo.WithLogger(ctx, logger)
|
||||
return s3.New(ctx, &c.options, false)
|
||||
}
|
||||
|
||||
@@ -20,6 +20,8 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type ID string
|
||||
@@ -156,3 +158,18 @@ type ObjectWriter interface {
|
||||
// Result returns the object's unified identifier after the write completes.
|
||||
Result() (ID, error)
|
||||
}
|
||||
|
||||
type ctxKeyLogger struct{}
|
||||
|
||||
// WithLogger returns a new context with the provided logger.
|
||||
func WithLogger(ctx context.Context, logger logrus.FieldLogger) context.Context {
|
||||
return context.WithValue(ctx, ctxKeyLogger{}, logger)
|
||||
}
|
||||
|
||||
// LoggerFromContext retrieves the logger from the context, or returns a default logger if none found.
|
||||
func LoggerFromContext(ctx context.Context) logrus.FieldLogger {
|
||||
if logger, ok := ctx.Value(ctxKeyLogger{}).(logrus.FieldLogger); ok && logger != nil {
|
||||
return logger
|
||||
}
|
||||
return logrus.New()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user