This commit is contained in:
wangchuxiao 2023-03-13 12:34:56 +08:00
parent 9645d16b2d
commit 9fcfdc9fab
24 changed files with 146 additions and 146 deletions

@ -6,6 +6,7 @@ import (
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/log"
"OpenIM/pkg/common/mw"
"context"
"fmt"
"github.com/OpenIMSDK/openKeeper"
"os"
@ -40,7 +41,7 @@ func run(port int) error {
address = config.Config.Api.ListenIP + ":" + strconv.Itoa(port)
}
fmt.Println("start api server, address: ", address, ", OpenIM version: ", config.Version)
//log2.Info(context.Background(), "start server success", "address", address, "version", config.Version)
log.ZInfo(context.Background(), "start server success", "address", address, "version", config.Version)
log.Info("s", "start server")
err = router.Run(address)
if err != nil {

@ -9,7 +9,7 @@ import (
)
func main() {
pushCmd := cmd.NewRpcCmd()
pushCmd := cmd.NewRpcCmd("push")
pushCmd.AddPortFlag()
pushCmd.AddPrometheusPortFlag()
if err := pushCmd.Exec(); err != nil {

@ -9,7 +9,7 @@ import (
)
func main() {
authCmd := cmd.NewRpcCmd()
authCmd := cmd.NewRpcCmd("auth")
authCmd.AddPortFlag()
authCmd.AddPrometheusPortFlag()
if err := authCmd.Exec(); err != nil {

@ -9,7 +9,7 @@ import (
)
func main() {
rpcCmd := cmd.NewRpcCmd()
rpcCmd := cmd.NewRpcCmd("conversation")
rpcCmd.AddPortFlag()
rpcCmd.AddPrometheusPortFlag()
if err := rpcCmd.Exec(); err != nil {

@ -9,7 +9,7 @@ import (
)
func main() {
rpcCmd := cmd.NewRpcCmd()
rpcCmd := cmd.NewRpcCmd("friend")
rpcCmd.AddPortFlag()
rpcCmd.AddPrometheusPortFlag()
if err := rpcCmd.Exec(); err != nil {

@ -9,7 +9,7 @@ import (
)
func main() {
rpcCmd := cmd.NewRpcCmd()
rpcCmd := cmd.NewRpcCmd("group")
rpcCmd.AddPortFlag()
rpcCmd.AddPrometheusPortFlag()
if err := rpcCmd.Exec(); err != nil {

@ -9,7 +9,7 @@ import (
)
func main() {
rpcCmd := cmd.NewRpcCmd()
rpcCmd := cmd.NewRpcCmd("msg")
rpcCmd.AddPortFlag()
rpcCmd.AddPrometheusPortFlag()
if err := rpcCmd.Exec(); err != nil {

@ -9,7 +9,7 @@ import (
)
func main() {
rpcCmd := cmd.NewRpcCmd()
rpcCmd := cmd.NewRpcCmd("third")
rpcCmd.AddPortFlag()
rpcCmd.AddPrometheusPortFlag()
if err := rpcCmd.Exec(); err != nil {

@ -9,7 +9,7 @@ import (
)
func main() {
rpcCmd := cmd.NewRpcCmd()
rpcCmd := cmd.NewRpcCmd("user")
rpcCmd.AddPortFlag()
rpcCmd.AddPrometheusPortFlag()
if err := rpcCmd.Exec(); err != nil {

1
go.mod

@ -55,6 +55,7 @@ require (
github.com/jonboulle/clockwork v0.3.0 // indirect
github.com/lestrrat-go/strftime v1.0.6 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
github.com/spf13/cobra v1.6.1
github.com/ugorji/go/codec v1.2.8 // indirect
go.uber.org/zap v1.24.0

2
go.sum

@ -727,6 +727,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=

@ -7,7 +7,7 @@ type ApiCmd struct {
}
func NewApiCmd() *ApiCmd {
return &ApiCmd{NewRootCmd()}
return &ApiCmd{NewRootCmd("api")}
}
func (a *ApiCmd) AddApi(f func(port int) error) {

@ -7,7 +7,7 @@ type CronTaskCmd struct {
}
func NewCronTaskCmd() *CronTaskCmd {
return &CronTaskCmd{NewRootCmd()}
return &CronTaskCmd{NewRootCmd("cronTask")}
}
func (c *CronTaskCmd) addRunE(f func() error) {

@ -12,7 +12,7 @@ type MsgGatewayCmd struct {
}
func NewMsgGatewayCmd() MsgGatewayCmd {
return MsgGatewayCmd{NewRootCmd()}
return MsgGatewayCmd{NewRootCmd("msgGateway")}
}
func (m *MsgGatewayCmd) AddWsPortFlag() {

@ -10,7 +10,7 @@ type MsgTransferCmd struct {
}
func NewMsgTransferCmd() MsgTransferCmd {
return MsgTransferCmd{NewRootCmd()}
return MsgTransferCmd{NewRootCmd("msgTransfer")}
}
func (m *MsgTransferCmd) addRunE() {

@ -18,7 +18,7 @@ type MsgUtilsCmd struct {
}
func NewMsgUtilsCmd() MsgUtilsCmd {
return MsgUtilsCmd{RootCmd: NewRootCmd()}
return MsgUtilsCmd{RootCmd: NewRootCmd("msgUtils")}
}
func (m *MsgUtilsCmd) AddUserIDFlag() {

@ -3,23 +3,29 @@ package cmd
import (
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/log"
"fmt"
"github.com/spf13/cobra"
)
type RootCmd struct {
Command cobra.Command
Name string
port int
prometheusPort int
}
func NewRootCmd() (rootCmd *RootCmd) {
rootCmd = &RootCmd{}
func NewRootCmd(name string) (rootCmd *RootCmd) {
rootCmd = &RootCmd{Name: name}
c := cobra.Command{
Use: "start",
Short: "Start the server",
Long: `Start the server`,
Short: fmt.Sprintf(`Start %s server`, name),
Long: fmt.Sprintf(`Start %s server`, name),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return rootCmd.getConfFromCmdAndInit(cmd)
if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil {
return err
}
return log.InitFromConfig(name)
},
}
rootCmd.Command = c
@ -27,12 +33,6 @@ func NewRootCmd() (rootCmd *RootCmd) {
return rootCmd
}
func (r *RootCmd) SetDesc(use, short, long string) {
r.Command.Use = use
r.Command.Short = short
r.Command.Long = long
}
func (r *RootCmd) addConfFlag() {
r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
}

@ -11,8 +11,8 @@ type RpcCmd struct {
*RootCmd
}
func NewRpcCmd() *RpcCmd {
authCmd := &RpcCmd{NewRootCmd()}
func NewRpcCmd(name string) *RpcCmd {
authCmd := &RpcCmd{NewRootCmd(name)}
return authCmd
}

39
pkg/common/log/lfs_hk.go Normal file

@ -0,0 +1,39 @@
package log
import (
"OpenIM/pkg/common/config"
nested "github.com/antonfisher/nested-logrus-formatter"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"time"
)
func NewLfsHook(rotationTime time.Duration, maxRemainNum uint, moduleName string) logrus.Hook {
lfsHook := lfshook.NewHook(lfshook.WriterMap{
logrus.DebugLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
logrus.InfoLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
logrus.WarnLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
logrus.ErrorLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
}, &nested.Formatter{
TimestampFormat: "2006-01-02 15:04:05.000",
HideKeys: false,
FieldsOrder: []string{"PID", "FilePath", "OperationID", "Msg"},
})
return lfsHook
}
func initRotateLogs(rotationTime time.Duration, maxRemainNum uint, level string, moduleName string) *rotatelogs.RotateLogs {
if moduleName != "" {
moduleName = moduleName + "."
}
writer, err := rotatelogs.New(
config.Config.Log.StorageLocation+moduleName+level+"."+"%Y-%m-%d",
rotatelogs.WithRotationTime(rotationTime),
rotatelogs.WithRotationCount(maxRemainNum),
)
if err != nil {
panic(err.Error())
} else {
return writer
}
}

@ -7,7 +7,7 @@ type Logger interface {
Info(ctx context.Context, msg string, keysAndValues ...interface{})
Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{})
Error(ctx context.Context, msg string, err error, keysAndValues ...interface{})
WithValues(keysAndValues ...interface{}) LogrusLogger
WithName(name string) LogrusLogger
WithCallDepth(depth int) LogrusLogger
WithValues(keysAndValues ...interface{}) Logger
WithName(name string) Logger
WithCallDepth(depth int) Logger
}

@ -2,15 +2,16 @@ package log
import (
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/tracelog"
"bufio"
"context"
//"bufio"
"fmt"
"os"
"time"
nested "github.com/antonfisher/nested-logrus-formatter"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
)
@ -96,33 +97,26 @@ func loggerInit(moduleName string) *LogrusLogger {
"",
}
}
func NewLfsHook(rotationTime time.Duration, maxRemainNum uint, moduleName string) logrus.Hook {
lfsHook := lfshook.NewHook(lfshook.WriterMap{
logrus.DebugLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
logrus.InfoLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
logrus.WarnLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
logrus.ErrorLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
}, &nested.Formatter{
TimestampFormat: "2006-01-02 15:04:05.000",
HideKeys: false,
FieldsOrder: []string{"PID", "FilePath", "OperationID"},
})
return lfsHook
func InfoKv(ctx context.Context, msg string, keysAndValues ...interface{}) {
operationID := tracelog.GetOperationID(ctx)
logger.WithFields(logrus.Fields{
"OperationID": operationID,
"PID": logger.Pid,
"Msg": msg,
}).Infoln(keysAndValues)
}
func initRotateLogs(rotationTime time.Duration, maxRemainNum uint, level string, moduleName string) *rotatelogs.RotateLogs {
if moduleName != "" {
moduleName = moduleName + "."
}
writer, err := rotatelogs.New(
config.Config.Log.StorageLocation+moduleName+level+"."+"%Y-%m-%d",
rotatelogs.WithRotationTime(rotationTime),
rotatelogs.WithRotationCount(maxRemainNum),
)
if err != nil {
panic(err.Error())
} else {
return writer
}
func DebugKv(ctx context.Context, msg string, keysAndValues ...interface{}) {
}
func ErrorKv(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
}
func WarnKv(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
}
func Info(OperationID string, args ...interface{}) {

@ -1,9 +1,12 @@
package log
import (
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant"
"OpenIM/pkg/common/tracelog"
"context"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"path/filepath"
"time"
"go.uber.org/zap"
@ -12,6 +15,7 @@ import (
var (
pkgLogger Logger = &ZapLogger{}
sp = string(filepath.Separator)
)
// InitFromConfig initializes a Zap-based logger
@ -24,39 +28,25 @@ func InitFromConfig(name string) error {
return nil
}
func Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
func ZDebug(ctx context.Context, msg string, keysAndValues ...interface{}) {
pkgLogger.Debug(ctx, msg, keysAndValues...)
}
func Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
func ZInfo(ctx context.Context, msg string, keysAndValues ...interface{}) {
pkgLogger.Info(ctx, msg, keysAndValues...)
}
func Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
func ZWarn(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
pkgLogger.Warn(ctx, msg, err, keysAndValues...)
}
func Error(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
func ZError(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
pkgLogger.Error(ctx, msg, err, keysAndValues...)
}
type Logger interface {
Debug(ctx context.Context, msg string, keysAndValues ...interface{})
Info(ctx context.Context, msg string, keysAndValues ...interface{})
Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{})
Error(ctx context.Context, msg string, err error, keysAndValues ...interface{})
WithValues(keysAndValues ...interface{}) Logger
WithName(name string) Logger
WithCallDepth(depth int) Logger
WithItemSampler() Logger
// WithoutSampler returns the original logger without sampling
WithoutSampler() Logger
}
type ZapLogger struct {
zap *zap.SugaredLogger
// store original logger without sampling to avoid multiple samplers
unsampled *zap.SugaredLogger
SampleDuration time.Duration
SampleInitial int
SampleInterval int
@ -64,29 +54,50 @@ type ZapLogger struct {
func NewZapLogger() (*ZapLogger, error) {
zapConfig := zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
Development: true,
Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stdout"},
Sampling: &zap.SamplingConfig{
Initial: 0,
Thereafter: 0,
Hook: nil,
},
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
Development: true,
Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(),
}
l, err := zapConfig.Build()
zl := &ZapLogger{}
if config.Config.Log.Stderr {
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stderr")
}
l, err := zapConfig.Build(zl.cores())
if err != nil {
return nil, err
}
zl := &ZapLogger{
unsampled: l.Sugar(),
}
zl.zap = l.Sugar()
return zl, nil
}
func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05"))
}
func (l *ZapLogger) cores() zap.Option {
fileEncoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
writer := l.getWriter()
var cores []zapcore.Core
if config.Config.Log.StorageLocation != "" {
cores = []zapcore.Core{
zapcore.NewCore(fileEncoder, writer, zapcore.DebugLevel),
}
}
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return zapcore.NewTee(cores...)
})
}
func (l *ZapLogger) getWriter() zapcore.WriteSyncer {
logf, _ := rotatelogs.New(config.Config.Log.StorageLocation+sp+"openIM"+"-"+".%Y_%m%d_%H",
rotatelogs.WithLinkName(config.Config.Log.StorageLocation+sp+"openIM"+"-"),
rotatelogs.WithMaxAge(2*24*time.Hour),
rotatelogs.WithRotationTime(time.Minute),
)
return zapcore.AddSync(logf)
}
func (l *ZapLogger) ToZap() *zap.SugaredLogger {
return l.zap
}
@ -120,57 +131,17 @@ func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndVal
func (l *ZapLogger) WithValues(keysAndValues ...interface{}) Logger {
dup := *l
dup.zap = l.zap.With(keysAndValues...)
if l.unsampled == l.zap {
dup.unsampled = dup.zap
} else {
dup.unsampled = l.unsampled.With(keysAndValues...)
}
return &dup
}
func (l *ZapLogger) WithName(name string) Logger {
dup := *l
dup.zap = l.zap.Named(name)
if l.unsampled == l.zap {
dup.unsampled = dup.zap
} else {
dup.unsampled = l.unsampled.Named(name)
}
return &dup
}
func (l *ZapLogger) WithCallDepth(depth int) Logger {
dup := *l
dup.zap = l.zap.WithOptions(zap.AddCallerSkip(depth))
if l.unsampled == l.zap {
dup.unsampled = dup.zap
} else {
dup.unsampled = l.unsampled.WithOptions(zap.AddCallerSkip(depth))
}
return &dup
}
func (l *ZapLogger) WithItemSampler() Logger {
if l.SampleDuration == 0 {
return l
}
dup := *l
dup.zap = l.unsampled.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewSamplerWithOptions(
core,
l.SampleDuration,
l.SampleInitial,
l.SampleInterval,
)
}))
return &dup
}
func (l *ZapLogger) WithoutSampler() Logger {
if l.SampleDuration == 0 {
return l
}
dup := *l
dup.zap = l.unsampled
return &dup
}

@ -1,10 +0,0 @@
package log
var logMap = map[int]int{
6: -1,
5: 0,
4: 1,
3: 2,
2: 3,
1: 4,
}

@ -34,11 +34,13 @@ func SetOperationID(ctx context.Context, operationID string) {
}
func GetOperationID(ctx context.Context) string {
f, ok := ctx.Value(TraceLogKey).(*FuncInfos)
if ok {
return f.OperationID
if ctx.Value(TraceLogKey) != nil {
f, ok := ctx.Value(TraceLogKey).(*FuncInfos)
if ok {
return f.OperationID
}
}
return utils.GetFuncName(1)
return utils.GetFuncName(2)
}
func GetOpUserID(ctx context.Context) string {