From 10bcdf9e0357adfe636f3efe32387a24d73e71a1 Mon Sep 17 00:00:00 2001 From: Shingo INADA Date: Mon, 4 May 2020 01:50:59 +0900 Subject: [PATCH] update logger setting --- gdxsv/lbs.go | 4 ++-- gdxsv/main.go | 46 ++++++++++++++++++++++++++++++++++++++-------- go.mod | 1 + go.sum | 2 ++ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/gdxsv/lbs.go b/gdxsv/lbs.go index 632f008..fee4aaf 100644 --- a/gdxsv/lbs.go +++ b/gdxsv/lbs.go @@ -80,7 +80,7 @@ func (lbs *Lbs) GetLobby(platform uint8, lobbyID uint16) *LbsLobby { } func (lbs *Lbs) ListenAndServe(addr string) error { - logger.Info("ListenAndServe", zap.String("addr", addr)) + logger.Info("lbs.ListenAndServe", zap.String("addr", addr)) tcpAddr, err := net.ResolveTCPAddr("tcp", addr) if err != nil { @@ -158,7 +158,7 @@ func (lbs *Lbs) Quit() { SendServerShutDown(p) } }) - time.Sleep(1000 * time.Millisecond) + time.Sleep(200 * time.Millisecond) close(lbs.chQuit) } diff --git a/gdxsv/main.go b/gdxsv/main.go index 38376f5..5aaaaa9 100644 --- a/gdxsv/main.go +++ b/gdxsv/main.go @@ -16,6 +16,7 @@ import ( "github.com/caarlos0/env" "github.com/davecgh/go-spew/spew" "github.com/jmoiron/sqlx" + "github.com/tommy351/zap-stackdriver" "go.uber.org/zap" ) @@ -31,6 +32,7 @@ var ( cpu = flag.Int("cpu", 2, "setting GOMAXPROCS") profile = flag.Int("profile", 1, "0: no profile, 1: enable http pprof, 2: enable blocking profile") prodlog = flag.Bool("prodlog", false, "use production logging mode") + loglevel = flag.Int("v", 2, "logging level. 1:error, 2:info, 3:debug") mcsdelay = flag.Duration("mcsdelay", 0, "mcs room delay for network lag emulation") ) @@ -173,23 +175,51 @@ func mainMcs() { } } -func main() { - printHeader() - flag.Parse() - +func prepareLogger() { var err error + var zapConfig zap.Config + if *prodlog { - logger, err = zap.NewDevelopment() + zapConfig = zap.NewProductionConfig() + zapConfig.EncoderConfig = stackdriver.EncoderConfig } else { - logger, err = zap.NewProduction() - logger = logger.With(zap.String("version", gdxsvVersion), zap.String("revision", gdxsvRevision)) + zapConfig = zap.NewDevelopmentConfig() + zapConfig.Encoding = "console" } + + switch *loglevel { + case 0: + zapConfig.Level = zap.NewAtomicLevelAt(zap.ErrorLevel) + case 1: + zapConfig.Level = zap.NewAtomicLevelAt(zap.InfoLevel) + case 2: + zapConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel) + default: + zapConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel) + } + + logger, err = zapConfig.Build() if err != nil { log.Fatalf("Failed to setup logger: %v", err) } + + if *prodlog { + logger = logger.With( + zap.String("gdxsv_version", gdxsvVersion), + zap.String("gdxsv_revision", gdxsvRevision)) + } +} + +func main() { + printHeader() + flag.Parse() + + prepareLogger() defer logger.Sync() - logger.Info("gdxsv", zap.String("version", gdxsvVersion), zap.String("revision", gdxsvRevision)) + logger.Info("hello gdxsv", + zap.String("gdxsv_version", gdxsvVersion), + zap.String("gdxsv_revision", gdxsvRevision)) rand.Seed(time.Now().UnixNano()) args := flag.Args() diff --git a/go.mod b/go.mod index eb6b21e..ab06891 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/mattn/go-sqlite3 v2.0.3+incompatible github.com/pkg/errors v0.9.1 github.com/stretchr/testify v1.5.1 // indirect + github.com/tommy351/zap-stackdriver v0.1.3 go.uber.org/zap v1.15.0 golang.org/x/net v0.0.0-20190923162816-aa69164e4478 golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 diff --git a/go.sum b/go.sum index 2ce3dff..31ed542 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/tommy351/zap-stackdriver v0.1.3 h1:luDjvQeLLYFwuTdlPsKl6w68aJwrhUKReV2wa0iLrIc= +github.com/tommy351/zap-stackdriver v0.1.3/go.mod h1:02SLb1eIv1QhUBnZZW6VBY9jlHYzekvxsSRXcEy2gRA= go.opencensus.io v0.21.0 h1:mU6zScU4U1YAFPHEHYk+3JC4SY7JxgkqS10ZOSyksNg= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=