Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to change log level #1023

Merged
merged 1 commit into from Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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