logging has been changed.

This commit is contained in:
crusader 2017-09-11 13:42:52 +09:00
parent b3f8909c67
commit 3b7c5aad87
12 changed files with 54 additions and 84 deletions

View File

@ -14,8 +14,7 @@ var _pool cgp.Pool
func InitializePool(ctx context.Context) {
var err error
h := &poolHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
ctx: ctx,
}
h.cfg = config.Sub("grpc")
h.MaxIdle = h.cfg.GetInt("pool.MaxIdle")
@ -23,6 +22,6 @@ func InitializePool(ctx context.Context) {
_pool, err = cgp.New(ctx, h)
if nil != err {
h.logger.Fatal(fmt.Sprintf("GRpc Pool: %v", err))
logging.Logger.Fatal(fmt.Sprintf("GRpc Pool: %v", err))
}
}

View File

@ -8,14 +8,12 @@ import (
"git.loafle.net/commons_go/config"
cgp "git.loafle.net/commons_go/grpc_pool"
oas "git.loafle.net/overflow/overflow_api_server/golang"
"go.uber.org/zap"
)
type poolHandlers struct {
cgp.PoolHandlers
ctx context.Context
logger *zap.Logger
cfg config.Configurator
ctx context.Context
cfg config.Configurator
}
func (h *poolHandlers) OnCreate() (*grpc.ClientConn, interface{}, error) {

View File

@ -15,8 +15,7 @@ import (
func newAuthJSONRpcHandler(ctx context.Context) ogw.ProtocolHandler {
h := &authJSONRpcHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
ctx: ctx,
}
p := jsonrpc.New(ctx, h)
@ -26,12 +25,11 @@ func newAuthJSONRpcHandler(ctx context.Context) ogw.ProtocolHandler {
type authJSONRpcHandlers struct {
jsonrpc.JSONRpcHandlers
ctx context.Context
logger *zap.Logger
ctx context.Context
}
func (h *authJSONRpcHandlers) OnRequest(soc ogw.Socket, method string, params []string) (interface{}, error) {
h.logger.Info("OnRequest",
logging.Logger.Info("OnRequest",
zap.String("path", soc.Path()),
zap.String("method", method),
zap.Any("params", params),

View File

@ -5,22 +5,18 @@ import (
"time"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
ogw "git.loafle.net/overflow/overflow_gateway_websocket"
"go.uber.org/zap"
)
type authSocketHandlers struct {
ogw.SocketHandlers
ctx context.Context
logger *zap.Logger
cfg config.Configurator
ctx context.Context
cfg config.Configurator
}
func NewAuthSocketHandler(ctx context.Context) ogw.SocketHandler {
h := &authSocketHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
ctx: ctx,
}
h.cfg = config.Sub("handlers.auth")
h.MaxMessageSize = h.cfg.GetInt64("socket.MaxMessageSize")

View File

@ -15,8 +15,7 @@ import (
func newProbeJSONRpcHandler(ctx context.Context) ogw.ProtocolHandler {
h := &probeJSONRpcHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
ctx: ctx,
}
p := jsonrpc.New(ctx, h)
@ -26,12 +25,11 @@ func newProbeJSONRpcHandler(ctx context.Context) ogw.ProtocolHandler {
type probeJSONRpcHandlers struct {
jsonrpc.JSONRpcHandlers
ctx context.Context
logger *zap.Logger
ctx context.Context
}
func (h *probeJSONRpcHandlers) OnRequest(soc ogw.Socket, method string, params []string) (interface{}, error) {
h.logger.Info("OnRequest",
logging.Logger.Info("OnRequest",
zap.String("path", soc.Path()),
zap.String("method", method),
zap.Any("params", params),

View File

@ -5,22 +5,18 @@ import (
"time"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
ogw "git.loafle.net/overflow/overflow_gateway_websocket"
"go.uber.org/zap"
)
type probeSocketHandlers struct {
ogw.SocketHandlers
ctx context.Context
logger *zap.Logger
cfg config.Configurator
ctx context.Context
cfg config.Configurator
}
func NewProbeSocketHandler(ctx context.Context) ogw.SocketHandler {
h := &probeSocketHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
ctx: ctx,
}
h.cfg = config.Sub("handlers.probe")
h.MaxMessageSize = h.cfg.GetInt64("socket.MaxMessageSize")

27
logging.json Normal file
View File

@ -0,0 +1,27 @@
{
"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"]
}

34
main.go
View File

@ -2,12 +2,9 @@ package main
import (
"context"
"encoding/json"
"flag"
"log"
"go.uber.org/zap"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_gateway_probe/grpc"
@ -17,17 +14,16 @@ import (
"git.loafle.net/overflow/overflow_gateway_probe/subscribe"
)
var logger *zap.Logger
func main() {
configPath := flag.String("config", ".", "The path of config file")
flag.Parse()
loadConfig(*configPath)
ctx := newContext()
defer logger.Sync()
ctx := context.Background()
defer logging.Logger.Sync()
s := server.NewServer(ctx)
rp := redis.NewPool(ctx)
@ -52,27 +48,3 @@ func loadConfig(path string) {
log.Fatalf("config error: %v", err)
}
}
func newContext() context.Context {
var err error
ctx := context.Background()
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)
return ctx
}

View File

@ -5,14 +5,12 @@ import (
"time"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
rp "git.loafle.net/commons_go/redis_pool"
)
func NewPool(ctx context.Context) rp.Pool {
h := &poolHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
ctx: ctx,
}
h.cfg = config.Sub("redis")
h.MaxIdle = h.cfg.GetInt("pool.MaxIdle")

View File

@ -6,10 +6,8 @@ import (
"time"
"git.loafle.net/commons_go/config"
"git.loafle.net/commons_go/logging"
jwt "github.com/dgrijalva/jwt-go"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
ogw "git.loafle.net/overflow/overflow_gateway_websocket"
)
@ -18,8 +16,7 @@ var ofSigningKey []byte
func newServerHandler(ctx context.Context) ogw.ServerHandler {
h := &serverHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
ctx: ctx,
}
h.cfg = config.Sub("websocket")
h.HandshakeTimeout = h.cfg.GetDuration("HandshakeTimeout") * time.Second
@ -32,9 +29,8 @@ func newServerHandler(ctx context.Context) ogw.ServerHandler {
type serverHandlers struct {
ogw.ServerHandlers
ctx context.Context
logger *zap.Logger
cfg config.Configurator
ctx context.Context
cfg config.Configurator
}
func (h *serverHandlers) OnConnection(soc ogw.Socket) {

View File

@ -3,16 +3,13 @@ package subscribe
import (
"context"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_gateway_probe/server"
ofs "git.loafle.net/overflow/overflow_subscriber"
"go.uber.org/zap"
)
func newAuthSubscriberHandler(ctx context.Context, channel string) ofs.SubscriberHandler {
h := &authSubscriberHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
ctx: ctx,
}
h.Channel = channel
@ -21,8 +18,7 @@ func newAuthSubscriberHandler(ctx context.Context, channel string) ofs.Subscribe
type authSubscriberHandlers struct {
ofs.SubscriberHandlers
ctx context.Context
logger *zap.Logger
ctx context.Context
}
func (h *authSubscriberHandlers) OnSubscribe(payload string) {

View File

@ -3,16 +3,13 @@ package subscribe
import (
"context"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_gateway_probe/server"
ofs "git.loafle.net/overflow/overflow_subscriber"
"go.uber.org/zap"
)
func newProbeSubscriberHandler(ctx context.Context, channel string) ofs.SubscriberHandler {
h := &probeSubscriberHandlers{
ctx: ctx,
logger: logging.WithContext(ctx),
ctx: ctx,
}
h.Channel = channel
@ -21,8 +18,7 @@ func newProbeSubscriberHandler(ctx context.Context, channel string) ofs.Subscrib
type probeSubscriberHandlers struct {
ofs.SubscriberHandlers
ctx context.Context
logger *zap.Logger
ctx context.Context
}
func (h *probeSubscriberHandlers) OnSubscribe(payload string) {