From d3f40ec6be12972c649dd837dc2e1f243053e653 Mon Sep 17 00:00:00 2001 From: crusader Date: Wed, 30 Aug 2017 20:48:38 +0900 Subject: [PATCH] ing --- config.json | 37 ++++++++++++++++++++++++++++--------- main.go | 21 ++++++++++++++++++++- 2 files changed, 48 insertions(+), 10 deletions(-) diff --git a/config.json b/config.json index 787358c..6cf7f63 100644 --- a/config.json +++ b/config.json @@ -28,15 +28,6 @@ "increaseCapacity": 10 } }, - "logging": { - "addr": "127.0.0.1:6379", - "tls": false, - "pool": { - "initialCapacity": 30, - "maxCapacity": 100, - "increaseCapacity": 10 - } - }, "handlers": { "web": { "entry": "/web", @@ -63,6 +54,34 @@ } } + }, + "logging": { + "level": "debug", + "development": true, + "disableCaller": true, + "disableStacktrace": true, + "sampling": { + "initial": 100, + "thereafter": 100 + }, + "encoding": "console", + "encoderConfig": { + "messageKey": "message", + "levelKey": "level", + "timeKey": "time", + "nameKey": "name", + "callerKey": "caller", + "stacktraceKey": "stacktrace", + "lineEnding": "\n", + "levelEncoder": "color", + "timeEncoder": "ISO8601", + "durationEncoder": "string", + "callerEncoder": "full", + "nameEncoder": "full" + }, + "outputPaths": ["stdout", "/tmp/logs"], + "errorOutputPaths": ["stderr"], + "initialFields": {"foo": "bar"} } } diff --git a/main.go b/main.go index 9cc6e2c..21190aa 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/json" "log" "time" @@ -36,6 +37,7 @@ func main() { defer cancel() logger = logging.WithContext(ctx) + defer logger.Sync() o := &gws.ServerOptions{ OnConnection: onConnection, @@ -97,8 +99,25 @@ func main() { } func NewContext() (context.Context, context.CancelFunc) { + var err error ctx := context.Background() - ctx = logging.NewContext(ctx, nil) + logConfig := config.Sub("logging") + + buf, err := logConfig.Marshal("json") + if err != nil { + panic(err) + } + var cfg zap.Config + if err = json.Unmarshal(buf, &cfg); err != nil { + panic(err) + } + + logger, err := cfg.Build() + if err != nil { + panic(err) + } + + ctx = logging.NewContext(ctx, logger) ctx, cancel := context.WithCancel(ctx)