Skip to content

Commit

Permalink
update logger setting
Browse files Browse the repository at this point in the history
  • Loading branch information
inada-s committed May 3, 2020
1 parent 3d31395 commit 10bcdf9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
4 changes: 2 additions & 2 deletions gdxsv/lbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -158,7 +158,7 @@ func (lbs *Lbs) Quit() {
SendServerShutDown(p)
}
})
time.Sleep(1000 * time.Millisecond)
time.Sleep(200 * time.Millisecond)
close(lbs.chQuit)
}

Expand Down
46 changes: 38 additions & 8 deletions gdxsv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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")
)

Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 10bcdf9

Please sign in to comment.