mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
zap
This commit is contained in:
parent
d0211ebd89
commit
6afb47ba06
@ -5,7 +5,6 @@ import (
|
||||
"OpenIM/pkg/common/cmd"
|
||||
"OpenIM/pkg/common/config"
|
||||
"OpenIM/pkg/common/log"
|
||||
"OpenIM/pkg/common/mw"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
@ -34,7 +33,6 @@ func run(port int) error {
|
||||
return err
|
||||
}
|
||||
log.NewPrivateLog(constant.LogFileName)
|
||||
zk.AddOption(mw.GrpcClient())
|
||||
router := api.NewGinRouter(zk)
|
||||
var address string
|
||||
if config.Config.Api.ListenIP != "" {
|
||||
|
@ -6,7 +6,6 @@ import (
|
||||
"OpenIM/pkg/common/constant"
|
||||
"OpenIM/pkg/common/db/cache"
|
||||
"OpenIM/pkg/common/db/controller"
|
||||
"OpenIM/pkg/common/log"
|
||||
"OpenIM/pkg/common/tokenverify"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"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) {
|
||||
log.Info("", "rpc come UserToken")
|
||||
operationID, ok := ctx.Value(constant.OperationID).(string)
|
||||
if !ok {
|
||||
log.Error("2222", "ctx missing operationID", operationID)
|
||||
}
|
||||
resp := pbAuth.UserTokenResp{}
|
||||
if _, err := s.userCheck.GetUserInfo(ctx, req.UserID); err != nil {
|
||||
log.Info("", "UserToken err:", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
token, err := s.authDatabase.CreateToken(ctx, req.UserID, constant.PlatformIDToName(int(req.PlatformID)))
|
||||
if err != nil {
|
||||
log.Info("", "Create Token err:", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
resp.Token = token
|
||||
@ -114,7 +106,6 @@ func (s *authServer) forceKickOff(ctx context.Context, userID string, platformID
|
||||
for _, v := range conns {
|
||||
client := msggateway.NewMsgGatewayClient(v)
|
||||
kickReq := &msggateway.KickUserOfflineReq{KickUserIDList: []string{userID}, PlatformID: platformID}
|
||||
log.NewInfo(operationID, "KickUserOffline ", client, kickReq.String())
|
||||
_, err := client.KickUserOffline(ctx, kickReq)
|
||||
return utils.Wrap(err, "")
|
||||
}
|
||||
|
@ -272,6 +272,7 @@ const (
|
||||
|
||||
const OperationID = "operationID"
|
||||
const OpUserID = "opUserID"
|
||||
const ConnID = "connID"
|
||||
|
||||
const (
|
||||
UnreliableNotification = 1
|
||||
|
@ -85,7 +85,7 @@ func (l *ZapLogger) cores() (zap.Option, error) {
|
||||
c := zap.NewProductionEncoderConfig()
|
||||
c.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
c.EncodeDuration = zapcore.SecondsDurationEncoder
|
||||
c.EncodeLevel = zapcore.LowercaseColorLevelEncoder
|
||||
//c.EncodeLevel = zapcore.LowercaseColorLevelEncoder
|
||||
fileEncoder := zapcore.NewJSONEncoder(c)
|
||||
fileEncoder.AddInt("PID", os.Getpid())
|
||||
writer, err := l.getWriter()
|
||||
@ -99,7 +99,7 @@ func (l *ZapLogger) cores() (zap.Option, error) {
|
||||
}
|
||||
}
|
||||
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 zapcore.NewTee(cores...)
|
||||
@ -117,10 +117,6 @@ func (l *ZapLogger) getWriter() (zapcore.WriteSyncer, error) {
|
||||
return zapcore.AddSync(logf), nil
|
||||
}
|
||||
|
||||
func (l *ZapLogger) GetConsoleWriter() {
|
||||
return
|
||||
}
|
||||
|
||||
func (l *ZapLogger) ToZap() *zap.SugaredLogger {
|
||||
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{} {
|
||||
operationID := tracelog.GetOperationID(ctx)
|
||||
opUserID := tracelog.GetOpUserID(ctx)
|
||||
connID := tracelog.GetConnID(ctx)
|
||||
if opUserID != "" {
|
||||
keysAndValues = append([]interface{}{constant.OpUserID, tracelog.GetOpUserID(ctx)}, keysAndValues...)
|
||||
}
|
||||
if operationID != "" {
|
||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||
}
|
||||
if connID != "" {
|
||||
keysAndValues = append([]interface{}{constant.ConnID, tracelog.GetConnID(ctx)}, keysAndValues...)
|
||||
}
|
||||
return keysAndValues
|
||||
}
|
||||
|
||||
|
@ -4,9 +4,9 @@ import (
|
||||
"OpenIM/pkg/common/constant"
|
||||
"OpenIM/pkg/common/log"
|
||||
"OpenIM/pkg/common/mw/specialerror"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"OpenIM/pkg/errs"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
@ -24,11 +24,11 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
|
||||
var operationID string
|
||||
defer func() {
|
||||
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
|
||||
log.ZInfo(ctx, "rpc req", "funcName", funcName, "req", rpcString(req))
|
||||
md, ok := metadata.FromIncomingContext(ctx)
|
||||
if !ok {
|
||||
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 {
|
||||
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, OpUserID, opUserID)
|
||||
resp, err = handler(ctx, req)
|
||||
if err != nil {
|
||||
log.Info(operationID, "rpc error:", err.Error())
|
||||
log.ZError(ctx, "handler rpc error", err, "req", req)
|
||||
unwrap := errs.Unwrap(err)
|
||||
codeErr := specialerror.ErrCode(unwrap)
|
||||
if codeErr == nil {
|
||||
log.Error(operationID, "rpc InternalServer:", err.Error())
|
||||
log.ZError(ctx, "rpc InternalServer:", err, "req", req)
|
||||
codeErr = errs.ErrInternalServer
|
||||
}
|
||||
if unwrap != err {
|
||||
log.Info(operationID, "rpc error stack:", fmt.Sprintf("%+v", err))
|
||||
log.ZError(ctx, "rpc error stack:", err)
|
||||
}
|
||||
code := codeErr.Code()
|
||||
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
|
||||
}
|
||||
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()
|
||||
}
|
||||
log.Info(OperationID, "opUserID", opUserID, "RPC", funcName, "Resp", rpcString(resp))
|
||||
log.ZInfo(ctx, "rpc resp", "funcName", funcName, "Resp", rpcString(resp))
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@ -83,7 +81,7 @@ func rpcClientInterceptor(ctx context.Context, method string, req, reply interfa
|
||||
}
|
||||
operationID, ok := ctx.Value(constant.OperationID).(string)
|
||||
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")
|
||||
}
|
||||
md := metadata.Pairs(constant.OperationID, operationID)
|
||||
@ -91,13 +89,10 @@ func rpcClientInterceptor(ctx context.Context, method string, req, reply interfa
|
||||
if ok {
|
||||
md.Append(constant.OpUserID, opUserID)
|
||||
}
|
||||
log.Info("", "rpc come here before")
|
||||
err = invoker(metadata.NewOutgoingContext(ctx, md), method, req, reply, cc, opts...)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
log.Info("", "rpc come here err", err.Error())
|
||||
|
||||
rpcErr, ok := err.(interface{ GRPCStatus() *status.Status })
|
||||
if !ok {
|
||||
return errs.ErrInternalServer.Wrap(err.Error())
|
||||
|
@ -54,6 +54,16 @@ func GetOpUserID(ctx context.Context) string {
|
||||
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 {
|
||||
for err != nil {
|
||||
unwrap, ok := err.(interface {
|
||||
|
Loading…
x
Reference in New Issue
Block a user