From 23a976cd015394ddb0c4a517ffdf6128525e16da Mon Sep 17 00:00:00 2001 From: RachitSharma2001 <45299880+RachitSharma2001@users.noreply.github.com> Date: Wed, 28 Dec 2022 14:44:03 -0800 Subject: [PATCH] Add ability to change log level (#1023) --- internal/config/config.go | 16 +++++++++++++--- internal/config/config_test.go | 9 +++++++++ main.go | 1 + 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index d0d8ce90ab..0bc72a70ae 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -40,6 +40,7 @@ type Config struct { bucketLocation string certificateLocation string privateKeyLocation string + LogLevel logrus.Level } type EventConfig struct { @@ -55,6 +56,7 @@ func Load(args []string) (Config, error) { var cfg Config var allowedCORSHeaders string var eventList string + var givenLogLevel string fs := flag.NewFlagSet("fake-gcs-server", flag.ContinueOnError) fs.StringVar(&cfg.backend, "backend", filesystemBackend, "storage backend (memory or filesystem)") @@ -73,12 +75,18 @@ func Load(args []string) (Config, error) { fs.StringVar(&cfg.bucketLocation, "location", "US-CENTRAL1", "location for buckets") fs.StringVar(&cfg.certificateLocation, "cert-location", "", "location for server certificate") fs.StringVar(&cfg.privateKeyLocation, "private-key-location", "", "location for private key") + fs.StringVar(&givenLogLevel, "log-level", "info", "level for logging. Options same as for logrus: trace, debug, info, warn, error, fatal, and panic") err := fs.Parse(args) if err != nil { return cfg, err } + cfg.LogLevel, err = logrus.ParseLevel(givenLogLevel) + if err != nil { + return cfg, err + } + if allowedCORSHeaders != "" { cfg.allowedCORSHeaders = strings.Split(allowedCORSHeaders, ",") } @@ -157,8 +165,9 @@ func (c *Config) ToFakeGcsOptions() fakestorage.Options { } } } - - return fakestorage.Options{ + logger := logrus.New() + logger.SetLevel(c.LogLevel) + opts := fakestorage.Options{ StorageRoot: storageRoot, Scheme: c.scheme, Host: c.host, @@ -166,10 +175,11 @@ func (c *Config) ToFakeGcsOptions() fakestorage.Options { PublicHost: c.publicHost, ExternalURL: c.externalURL, AllowedCORSHeaders: c.allowedCORSHeaders, - Writer: logrus.New().Writer(), + Writer: logger.Writer(), EventOptions: eventOptions, BucketsLocation: c.bucketLocation, CertificateLocation: c.certificateLocation, PrivateKeyLocation: c.privateKeyLocation, } + return opts } diff --git a/internal/config/config_test.go b/internal/config/config_test.go index fd7ed676f4..6da0b59e69 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -11,6 +11,7 @@ import ( "github.com/fsouza/fake-gcs-server/internal/notification" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" + "github.com/sirupsen/logrus" ) func TestLoadConfig(t *testing.T) { @@ -38,6 +39,7 @@ func TestLoadConfig(t *testing.T) { "-event.object-prefix", "uploads/", "-event.list", "finalize,delete,metadataUpdate,archive", "-location", "US-EAST1", + "-log-level", "warn", }, expectedConfig: Config{ Seed: "/var/gcs", @@ -56,6 +58,7 @@ func TestLoadConfig(t *testing.T) { list: []string{"finalize", "delete", "metadataUpdate", "archive"}, }, bucketLocation: "US-EAST1", + LogLevel: logrus.WarnLevel, }, }, { @@ -74,6 +77,7 @@ func TestLoadConfig(t *testing.T) { list: []string{"finalize"}, }, bucketLocation: "US-CENTRAL1", + LogLevel: logrus.InfoLevel, }, }, { @@ -111,6 +115,11 @@ func TestLoadConfig(t *testing.T) { args: []string{"-event.list", "invalid,stuff", "-event.pubsub-topic", "gcs-events", "-event.pubsub-project-id", "test-project"}, expectErr: true, }, + { + name: "invalid log level", + args: []string{"-log-level", "non-existent-level"}, + expectErr: true, + }, } for _, test := range tests { test := test diff --git a/main.go b/main.go index db69daa907..df6b63bdc8 100644 --- a/main.go +++ b/main.go @@ -29,6 +29,7 @@ func main() { log.Fatal(err) } logger := logrus.New() + logger.SetLevel(cfg.LogLevel) var emptyBuckets []string opts := cfg.ToFakeGcsOptions()