From 52e7d16d73e0a379883dbc39d3dbdb03c4911f05 Mon Sep 17 00:00:00 2001 From: Alex Hong <9397363+hongalex@users.noreply.github.com> Date: Thu, 4 Mar 2021 15:11:35 -0800 Subject: [PATCH] feat: add log level config via env var (#189) * feat: add log level config via env var * redirect to stdout * add better debug logging for streams * use correct log method * fix blob size formatting Co-authored-by: Emma Haruka Iwao --- cmd/server/main.go | 22 +++++++++++++++------- internal/app/server/open_saves.go | 5 +++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 12d4ef07..7ee27400 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -21,10 +21,9 @@ import ( "os" "strconv" - log "github.com/sirupsen/logrus" - "github.com/googleforgames/open-saves/internal/app/server" "github.com/googleforgames/open-saves/internal/pkg/cmd" + log "github.com/sirupsen/logrus" ) func main() { @@ -33,13 +32,15 @@ func main() { defaultBucket := cmd.GetEnvVarString("OPEN_SAVES_BUCKET", "") defaultProject := cmd.GetEnvVarString("OPEN_SAVES_PROJECT", "") defaultCache := cmd.GetEnvVarString("OPEN_SAVES_CACHE", "localhost:6379") + defaultLogLevel := cmd.GetEnvVarString("LOG_LEVEL", "info") var ( - port = flag.Uint("port", uint(defaultPort), "The port number to run Open Saves on") - cloud = flag.String("cloud", defaultCloud, "The public cloud provider you wish to run Open Saves on") - bucket = flag.String("bucket", defaultBucket, "The bucket which will hold Open Saves blobs") - project = flag.String("project", defaultProject, "The GCP project ID to use for Datastore") - cache = flag.String("cache", defaultCache, "The address of the cache store instance") + port = flag.Uint("port", uint(defaultPort), "The port number to run Open Saves on") + cloud = flag.String("cloud", defaultCloud, "The public cloud provider you wish to run Open Saves on") + bucket = flag.String("bucket", defaultBucket, "The bucket which will hold Open Saves blobs") + project = flag.String("project", defaultProject, "The GCP project ID to use for Datastore") + cache = flag.String("cache", defaultCache, "The address of the cache store instance") + logLevel = flag.String("log", defaultLogLevel, "The level to log messages at") ) flag.Parse() @@ -56,6 +57,13 @@ func main() { log.Fatal("missing -cache argument for cache store") } + ll, err := log.ParseLevel(*logLevel) + if err != nil { + ll = log.InfoLevel + } + log.SetLevel(ll) + log.Infof("Log level is: %s", ll.String()) + cfg := &server.Config{ Address: fmt.Sprintf(":%d", *port), Cloud: *cloud, diff --git a/internal/app/server/open_saves.go b/internal/app/server/open_saves.go index f9029fa5..f8d8a978 100644 --- a/internal/app/server/open_saves.go +++ b/internal/app/server/open_saves.go @@ -227,6 +227,7 @@ func (s *openSavesServer) QueryRecords(ctx context.Context, stream *pb.QueryReco } func (s *openSavesServer) insertInlineBlob(ctx context.Context, stream pb.OpenSaves_CreateBlobServer, meta *pb.BlobMetadata) error { + log.Debugf("Inserting inline blob: %v\n", meta) // Receive the blob size := meta.GetSize() buffer := bytes.NewBuffer(make([]byte, 0, size)) @@ -291,6 +292,7 @@ func (s *openSavesServer) blobRefFail(ctx context.Context, blobref *metadb.BlobR } func (s *openSavesServer) insertExternalBlob(ctx context.Context, stream pb.OpenSaves_CreateBlobServer, meta *pb.BlobMetadata) error { + log.Debugf("Inserting external blob: %v\n", meta) // Create a blob reference based on the metadata. blobref := metadb.NewBlobRef(meta.GetSize(), meta.GetStoreKey(), meta.GetRecordKey()) blobref, err := s.metaDB.InsertBlobRef(ctx, blobref) @@ -365,6 +367,7 @@ func (s *openSavesServer) insertExternalBlob(ctx context.Context, stream pb.Open } func (s *openSavesServer) CreateBlob(stream pb.OpenSaves_CreateBlobServer) error { + log.Debug("Creating blob stream\n") ctx := stream.Context() // The first message must be metadata. @@ -378,6 +381,8 @@ func (s *openSavesServer) CreateBlob(stream pb.OpenSaves_CreateBlobServer) error log.Error("CreateBlob: first message was not metadata") return status.Error(codes.InvalidArgument, "The first message must be metadata.") } + log.Debugf("Got metadata from stream: store(%s), record(%s), blob size(%d)\n", + meta.GetStoreKey(), meta.GetRecordKey(), meta.GetSize()) // TODO(yuryu): Make the threshold configurable if meta.GetSize() <= int64(maxInlineBlobSize) {