mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-23 18:00:32 +08:00
zap
This commit is contained in:
parent
d0211ebd89
commit
6afb47ba06
@ -5,7 +5,6 @@ import (
|
|||||||
"OpenIM/pkg/common/cmd"
|
"OpenIM/pkg/common/cmd"
|
||||||
"OpenIM/pkg/common/config"
|
"OpenIM/pkg/common/config"
|
||||||
"OpenIM/pkg/common/log"
|
"OpenIM/pkg/common/log"
|
||||||
"OpenIM/pkg/common/mw"
|
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -34,7 +33,6 @@ func run(port int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.NewPrivateLog(constant.LogFileName)
|
log.NewPrivateLog(constant.LogFileName)
|
||||||
zk.AddOption(mw.GrpcClient())
|
|
||||||
router := api.NewGinRouter(zk)
|
router := api.NewGinRouter(zk)
|
||||||
var address string
|
var address string
|
||||||
if config.Config.Api.ListenIP != "" {
|
if config.Config.Api.ListenIP != "" {
|
||||||
|
@ -6,7 +6,6 @@ import (
|
|||||||
"OpenIM/pkg/common/constant"
|
"OpenIM/pkg/common/constant"
|
||||||
"OpenIM/pkg/common/db/cache"
|
"OpenIM/pkg/common/db/cache"
|
||||||
"OpenIM/pkg/common/db/controller"
|
"OpenIM/pkg/common/db/controller"
|
||||||
"OpenIM/pkg/common/log"
|
|
||||||
"OpenIM/pkg/common/tokenverify"
|
"OpenIM/pkg/common/tokenverify"
|
||||||
"OpenIM/pkg/common/tracelog"
|
"OpenIM/pkg/common/tracelog"
|
||||||
"OpenIM/pkg/discoveryregistry"
|
"OpenIM/pkg/discoveryregistry"
|
||||||
@ -38,19 +37,12 @@ func Start(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *authServer) UserToken(ctx context.Context, req *pbAuth.UserTokenReq) (*pbAuth.UserTokenResp, error) {
|
func (s *authServer) UserToken(ctx context.Context, req *pbAuth.UserTokenReq) (*pbAuth.UserTokenResp, error) {
|
||||||
log.Info("", "rpc come UserToken")
|
|
||||||
operationID, ok := ctx.Value(constant.OperationID).(string)
|
|
||||||
if !ok {
|
|
||||||
log.Error("2222", "ctx missing operationID", operationID)
|
|
||||||
}
|
|
||||||
resp := pbAuth.UserTokenResp{}
|
resp := pbAuth.UserTokenResp{}
|
||||||
if _, err := s.userCheck.GetUserInfo(ctx, req.UserID); err != nil {
|
if _, err := s.userCheck.GetUserInfo(ctx, req.UserID); err != nil {
|
||||||
log.Info("", "UserToken err:", err.Error())
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
token, err := s.authDatabase.CreateToken(ctx, req.UserID, constant.PlatformIDToName(int(req.PlatformID)))
|
token, err := s.authDatabase.CreateToken(ctx, req.UserID, constant.PlatformIDToName(int(req.PlatformID)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info("", "Create Token err:", err.Error())
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp.Token = token
|
resp.Token = token
|
||||||
@ -114,7 +106,6 @@ func (s *authServer) forceKickOff(ctx context.Context, userID string, platformID
|
|||||||
for _, v := range conns {
|
for _, v := range conns {
|
||||||
client := msggateway.NewMsgGatewayClient(v)
|
client := msggateway.NewMsgGatewayClient(v)
|
||||||
kickReq := &msggateway.KickUserOfflineReq{KickUserIDList: []string{userID}, PlatformID: platformID}
|
kickReq := &msggateway.KickUserOfflineReq{KickUserIDList: []string{userID}, PlatformID: platformID}
|
||||||
log.NewInfo(operationID, "KickUserOffline ", client, kickReq.String())
|
|
||||||
_, err := client.KickUserOffline(ctx, kickReq)
|
_, err := client.KickUserOffline(ctx, kickReq)
|
||||||
return utils.Wrap(err, "")
|
return utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
@ -272,6 +272,7 @@ const (
|
|||||||
|
|
||||||
const OperationID = "operationID"
|
const OperationID = "operationID"
|
||||||
const OpUserID = "opUserID"
|
const OpUserID = "opUserID"
|
||||||
|
const ConnID = "connID"
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UnreliableNotification = 1
|
UnreliableNotification = 1
|
||||||
|
@ -85,7 +85,7 @@ func (l *ZapLogger) cores() (zap.Option, error) {
|
|||||||
c := zap.NewProductionEncoderConfig()
|
c := zap.NewProductionEncoderConfig()
|
||||||
c.EncodeTime = zapcore.ISO8601TimeEncoder
|
c.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||||
c.EncodeDuration = zapcore.SecondsDurationEncoder
|
c.EncodeDuration = zapcore.SecondsDurationEncoder
|
||||||
c.EncodeLevel = zapcore.LowercaseColorLevelEncoder
|
//c.EncodeLevel = zapcore.LowercaseColorLevelEncoder
|
||||||
fileEncoder := zapcore.NewJSONEncoder(c)
|
fileEncoder := zapcore.NewJSONEncoder(c)
|
||||||
fileEncoder.AddInt("PID", os.Getpid())
|
fileEncoder.AddInt("PID", os.Getpid())
|
||||||
writer, err := l.getWriter()
|
writer, err := l.getWriter()
|
||||||
@ -99,7 +99,7 @@ func (l *ZapLogger) cores() (zap.Option, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if config.Config.Log.Stderr {
|
if config.Config.Log.Stderr {
|
||||||
cores = append(cores, zapcore.NewCore(zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()), zapcore.Lock(os.Stdout), zapcore.DebugLevel))
|
cores = append(cores, zapcore.NewCore(fileEncoder, zapcore.Lock(os.Stdout), zapcore.DebugLevel))
|
||||||
}
|
}
|
||||||
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
|
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
|
||||||
return zapcore.NewTee(cores...)
|
return zapcore.NewTee(cores...)
|
||||||
@ -117,10 +117,6 @@ func (l *ZapLogger) getWriter() (zapcore.WriteSyncer, error) {
|
|||||||
return zapcore.AddSync(logf), nil
|
return zapcore.AddSync(logf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ZapLogger) GetConsoleWriter() {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *ZapLogger) ToZap() *zap.SugaredLogger {
|
func (l *ZapLogger) ToZap() *zap.SugaredLogger {
|
||||||
return l.zap
|
return l.zap
|
||||||
}
|
}
|
||||||
@ -154,12 +150,16 @@ func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndVal
|
|||||||
func (l *ZapLogger) kvAppend(ctx context.Context, keysAndValues []interface{}) []interface{} {
|
func (l *ZapLogger) kvAppend(ctx context.Context, keysAndValues []interface{}) []interface{} {
|
||||||
operationID := tracelog.GetOperationID(ctx)
|
operationID := tracelog.GetOperationID(ctx)
|
||||||
opUserID := tracelog.GetOpUserID(ctx)
|
opUserID := tracelog.GetOpUserID(ctx)
|
||||||
|
connID := tracelog.GetConnID(ctx)
|
||||||
if opUserID != "" {
|
if opUserID != "" {
|
||||||
keysAndValues = append([]interface{}{constant.OpUserID, tracelog.GetOpUserID(ctx)}, keysAndValues...)
|
keysAndValues = append([]interface{}{constant.OpUserID, tracelog.GetOpUserID(ctx)}, keysAndValues...)
|
||||||
}
|
}
|
||||||
if operationID != "" {
|
if operationID != "" {
|
||||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||||
}
|
}
|
||||||
|
if connID != "" {
|
||||||
|
keysAndValues = append([]interface{}{constant.ConnID, tracelog.GetConnID(ctx)}, keysAndValues...)
|
||||||
|
}
|
||||||
return keysAndValues
|
return keysAndValues
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,9 +4,9 @@ import (
|
|||||||
"OpenIM/pkg/common/constant"
|
"OpenIM/pkg/common/constant"
|
||||||
"OpenIM/pkg/common/log"
|
"OpenIM/pkg/common/log"
|
||||||
"OpenIM/pkg/common/mw/specialerror"
|
"OpenIM/pkg/common/mw/specialerror"
|
||||||
"OpenIM/pkg/common/tracelog"
|
|
||||||
"OpenIM/pkg/errs"
|
"OpenIM/pkg/errs"
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
@ -24,11 +24,11 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
|||||||
var operationID string
|
var operationID string
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
log.NewError(operationID, info.FullMethod, "type:", fmt.Sprintf("%T", r), "panic:", r, "stack:", string(debug.Stack()))
|
log.ZError(ctx, "rpc panic", nil, "FullMethod", info.FullMethod, "type:", fmt.Sprintf("%T", r), "panic:", r, string(debug.Stack()))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
log.Info("", "rpc come here,in rpc call")
|
|
||||||
funcName := info.FullMethod
|
funcName := info.FullMethod
|
||||||
|
log.ZInfo(ctx, "rpc req", "funcName", funcName, "req", rpcString(req))
|
||||||
md, ok := metadata.FromIncomingContext(ctx)
|
md, ok := metadata.FromIncomingContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, status.New(codes.InvalidArgument, "missing metadata").Err()
|
return nil, status.New(codes.InvalidArgument, "missing metadata").Err()
|
||||||
@ -42,25 +42,23 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
|||||||
if opts := md.Get(OpUserID); len(opts) == 1 {
|
if opts := md.Get(OpUserID); len(opts) == 1 {
|
||||||
opUserID = opts[0]
|
opUserID = opts[0]
|
||||||
}
|
}
|
||||||
log.Info(OperationID, "opUserID", opUserID, "RPC", funcName, "Req", rpcString(req))
|
|
||||||
ctx = tracelog.SetFuncInfos(ctx, funcName, operationID)
|
|
||||||
ctx = context.WithValue(ctx, OperationID, operationID)
|
ctx = context.WithValue(ctx, OperationID, operationID)
|
||||||
ctx = context.WithValue(ctx, OpUserID, opUserID)
|
ctx = context.WithValue(ctx, OpUserID, opUserID)
|
||||||
resp, err = handler(ctx, req)
|
resp, err = handler(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Info(operationID, "rpc error:", err.Error())
|
log.ZError(ctx, "handler rpc error", err, "req", req)
|
||||||
unwrap := errs.Unwrap(err)
|
unwrap := errs.Unwrap(err)
|
||||||
codeErr := specialerror.ErrCode(unwrap)
|
codeErr := specialerror.ErrCode(unwrap)
|
||||||
if codeErr == nil {
|
if codeErr == nil {
|
||||||
log.Error(operationID, "rpc InternalServer:", err.Error())
|
log.ZError(ctx, "rpc InternalServer:", err, "req", req)
|
||||||
codeErr = errs.ErrInternalServer
|
codeErr = errs.ErrInternalServer
|
||||||
}
|
}
|
||||||
if unwrap != err {
|
if unwrap != err {
|
||||||
log.Info(operationID, "rpc error stack:", fmt.Sprintf("%+v", err))
|
log.ZError(ctx, "rpc error stack:", err)
|
||||||
}
|
}
|
||||||
code := codeErr.Code()
|
code := codeErr.Code()
|
||||||
if code <= 0 || code > math.MaxUint32 {
|
if code <= 0 || code > math.MaxUint32 {
|
||||||
log.Error(operationID, "rpc UnknownCode:", code, "err:", err.Error())
|
log.ZError(ctx, "rpc UnknownError", err, "rpc UnknownCode:", code)
|
||||||
code = errs.UnknownCode
|
code = errs.UnknownCode
|
||||||
}
|
}
|
||||||
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
|
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
|
||||||
@ -73,7 +71,7 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
|||||||
}
|
}
|
||||||
return nil, grpcStatus.Err()
|
return nil, grpcStatus.Err()
|
||||||
}
|
}
|
||||||
log.Info(OperationID, "opUserID", opUserID, "RPC", funcName, "Resp", rpcString(resp))
|
log.ZInfo(ctx, "rpc resp", "funcName", funcName, "Resp", rpcString(resp))
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,7 +81,7 @@ func rpcClientInterceptor(ctx context.Context, method string, req, reply interfa
|
|||||||
}
|
}
|
||||||
operationID, ok := ctx.Value(constant.OperationID).(string)
|
operationID, ok := ctx.Value(constant.OperationID).(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Error("1111", "ctx missing operationID")
|
log.ZError(ctx, "ctx missing operationID", errors.New("ctx missing operationID"))
|
||||||
return errs.ErrArgs.Wrap("ctx missing operationID")
|
return errs.ErrArgs.Wrap("ctx missing operationID")
|
||||||
}
|
}
|
||||||
md := metadata.Pairs(constant.OperationID, operationID)
|
md := metadata.Pairs(constant.OperationID, operationID)
|
||||||
@ -91,13 +89,10 @@ func rpcClientInterceptor(ctx context.Context, method string, req, reply interfa
|
|||||||
if ok {
|
if ok {
|
||||||
md.Append(constant.OpUserID, opUserID)
|
md.Append(constant.OpUserID, opUserID)
|
||||||
}
|
}
|
||||||
log.Info("", "rpc come here before")
|
|
||||||
err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...)
|
err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
log.Info("", "rpc come here err", err.Error())
|
|
||||||
|
|
||||||
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
|
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
|
||||||
if !ok {
|
if !ok {
|
||||||
return errs.ErrInternalServer.Wrap(err.Error())
|
return errs.ErrInternalServer.Wrap(err.Error())
|
||||||
|
@ -54,6 +54,16 @@ func GetOpUserID(ctx context.Context) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetConnID(ctx context.Context) string {
|
||||||
|
if ctx.Value(constant.ConnID) != "" {
|
||||||
|
s, ok := ctx.Value(constant.ConnID).(string)
|
||||||
|
if ok {
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func Unwrap(err error) error {
|
func Unwrap(err error) error {
|
||||||
for err != nil {
|
for err != nil {
|
||||||
unwrap, ok := err.(interface {
|
unwrap, ok := err.(interface {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user