Skip to content

Commit

Permalink
Add ability to change log level (#1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
RachitSharma2001 committed Dec 28, 2022
1 parent a72af60 commit 23a976c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
16 changes: 13 additions & 3 deletions internal/config/config.go
Expand Up @@ -40,6 +40,7 @@ type Config struct {
bucketLocation string
certificateLocation string
privateKeyLocation string
LogLevel logrus.Level
}

type EventConfig struct {
Expand All @@ -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)")
Expand All @@ -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, ",")
}
Expand Down Expand Up @@ -157,19 +165,21 @@ 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,
Port: uint16(c.port),
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
}
9 changes: 9 additions & 0 deletions internal/config/config_test.go
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
Expand All @@ -56,6 +58,7 @@ func TestLoadConfig(t *testing.T) {
list: []string{"finalize", "delete", "metadataUpdate", "archive"},
},
bucketLocation: "US-EAST1",
LogLevel: logrus.WarnLevel,
},
},
{
Expand All @@ -74,6 +77,7 @@ func TestLoadConfig(t *testing.T) {
list: []string{"finalize"},
},
bucketLocation: "US-CENTRAL1",
LogLevel: logrus.InfoLevel,
},
},
{
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions main.go
Expand Up @@ -29,6 +29,7 @@ func main() {
log.Fatal(err)
}
logger := logrus.New()
logger.SetLevel(cfg.LogLevel)

var emptyBuckets []string
opts := cfg.ToFakeGcsOptions()
Expand Down

0 comments on commit 23a976c

Please sign in to comment.