Merge remote-tracking branch 'origin/errcode' into errcode

This commit is contained in:
withchao
2023-03-15 11:25:18 +08:00
5 changed files with 20 additions and 21 deletions
+10 -10
View File
@@ -47,25 +47,20 @@ func ZError(ctx context.Context, msg string, err error, keysAndValues ...interfa
type ZapLogger struct {
zap *zap.SugaredLogger
// store original logger without sampling to avoid multiple samplers
SampleDuration time.Duration
SampleInitial int
SampleInterval int
}
func NewZapLogger() (*ZapLogger, error) {
zapConfig := zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
Development: true,
Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(),
DisableStacktrace: true,
InitialFields: map[string]interface{}{"PID": os.Getegid()},
}
zl := &ZapLogger{}
if config.Config.Log.Stderr {
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stderr")
}
zl := &ZapLogger{}
opts, err := zl.cores()
if err != nil {
return nil, err
@@ -75,13 +70,10 @@ func NewZapLogger() (*ZapLogger, error) {
return nil, err
}
zl.zap = l.Sugar()
zl.zap.WithOptions(zap.AddStacktrace(zap.DPanicLevel))
return zl, nil
}
func (l *ZapLogger) timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05"))
}
func (l *ZapLogger) cores() (zap.Option, error) {
c := zap.NewProductionEncoderConfig()
c.EncodeTime = zapcore.ISO8601TimeEncoder
@@ -111,6 +103,14 @@ func (l *ZapLogger) cores() (zap.Option, error) {
}), nil
}
func NewErrStackCore(c zapcore.Core) zapcore.Core {
return &errStackCore{c}
}
type errStackCore struct {
zapcore.Core
}
func (l *ZapLogger) getWriter() (zapcore.WriteSyncer, error) {
logf, err := rotatelogs.New(config.Config.Log.StorageLocation+sp+"OpenIM.log.all"+".%Y-%m-%d",
rotatelogs.WithRotationCount(config.Config.Log.RemainRotationCount),
+1 -1
View File
@@ -20,7 +20,7 @@ func rpcClientInterceptor(ctx context.Context, method string, req, resp interfac
if ctx == nil {
return errs.ErrInternalServer.Wrap("call rpc request context is nil")
}
log.ZInfo(ctx, "rpc client req", "req", "funcName", method, rpcString(req))
log.ZInfo(ctx, "rpc client req", "funcName", method, "req", rpcString(req))
operationID, ok := ctx.Value(constant.OperationID).(string)
if !ok {
log.ZWarn(ctx, "ctx missing operationID", errors.New("ctx missing operationID"), "funcName", method)
+8 -7
View File
@@ -4,6 +4,7 @@ import (
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/mw/specialerror"
"OpenIM/pkg/errs"
"OpenIM/pkg/proto/wrapperspb"
"context"
"fmt"
"google.golang.org/grpc"
@@ -65,13 +66,13 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
code = errs.ServerInternalError
}
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
//if unwrap != err {
// stack := fmt.Sprintf("%+v", err)
// if details, err := grpcStatus.WithDetails(wrapperspb.String(stack)); err == nil {
// grpcStatus = details
// }
//}
log.ZWarn(ctx, "rpc resp", unwrap, "funcName", funcName)
if unwrap != err {
stack := fmt.Sprintf("%+v", err)
if details, err := grpcStatus.WithDetails(wrapperspb.String(stack)); err == nil {
grpcStatus = details
}
}
log.ZWarn(ctx, "rpc resp", err, "funcName", funcName)
return nil, grpcStatus.Err()
}