mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-28 12:56:35 +08:00
log
This commit is contained in:
parent
9645d16b2d
commit
9fcfdc9fab
@ -6,6 +6,7 @@ import (
|
|||||||
"OpenIM/pkg/common/config"
|
"OpenIM/pkg/common/config"
|
||||||
"OpenIM/pkg/common/log"
|
"OpenIM/pkg/common/log"
|
||||||
"OpenIM/pkg/common/mw"
|
"OpenIM/pkg/common/mw"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/OpenIMSDK/openKeeper"
|
"github.com/OpenIMSDK/openKeeper"
|
||||||
"os"
|
"os"
|
||||||
@ -40,7 +41,7 @@ func run(port int) error {
|
|||||||
address = config.Config.Api.ListenIP + ":" + strconv.Itoa(port)
|
address = config.Config.Api.ListenIP + ":" + strconv.Itoa(port)
|
||||||
}
|
}
|
||||||
fmt.Println("start api server, address: ", address, ", OpenIM version: ", config.Version)
|
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")
|
log.Info("s", "start server")
|
||||||
err = router.Run(address)
|
err = router.Run(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pushCmd := cmd.NewRpcCmd()
|
pushCmd := cmd.NewRpcCmd("push")
|
||||||
pushCmd.AddPortFlag()
|
pushCmd.AddPortFlag()
|
||||||
pushCmd.AddPrometheusPortFlag()
|
pushCmd.AddPrometheusPortFlag()
|
||||||
if err := pushCmd.Exec(); err != nil {
|
if err := pushCmd.Exec(); err != nil {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
authCmd := cmd.NewRpcCmd()
|
authCmd := cmd.NewRpcCmd("auth")
|
||||||
authCmd.AddPortFlag()
|
authCmd.AddPortFlag()
|
||||||
authCmd.AddPrometheusPortFlag()
|
authCmd.AddPrometheusPortFlag()
|
||||||
if err := authCmd.Exec(); err != nil {
|
if err := authCmd.Exec(); err != nil {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rpcCmd := cmd.NewRpcCmd()
|
rpcCmd := cmd.NewRpcCmd("conversation")
|
||||||
rpcCmd.AddPortFlag()
|
rpcCmd.AddPortFlag()
|
||||||
rpcCmd.AddPrometheusPortFlag()
|
rpcCmd.AddPrometheusPortFlag()
|
||||||
if err := rpcCmd.Exec(); err != nil {
|
if err := rpcCmd.Exec(); err != nil {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rpcCmd := cmd.NewRpcCmd()
|
rpcCmd := cmd.NewRpcCmd("friend")
|
||||||
rpcCmd.AddPortFlag()
|
rpcCmd.AddPortFlag()
|
||||||
rpcCmd.AddPrometheusPortFlag()
|
rpcCmd.AddPrometheusPortFlag()
|
||||||
if err := rpcCmd.Exec(); err != nil {
|
if err := rpcCmd.Exec(); err != nil {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rpcCmd := cmd.NewRpcCmd()
|
rpcCmd := cmd.NewRpcCmd("group")
|
||||||
rpcCmd.AddPortFlag()
|
rpcCmd.AddPortFlag()
|
||||||
rpcCmd.AddPrometheusPortFlag()
|
rpcCmd.AddPrometheusPortFlag()
|
||||||
if err := rpcCmd.Exec(); err != nil {
|
if err := rpcCmd.Exec(); err != nil {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rpcCmd := cmd.NewRpcCmd()
|
rpcCmd := cmd.NewRpcCmd("msg")
|
||||||
rpcCmd.AddPortFlag()
|
rpcCmd.AddPortFlag()
|
||||||
rpcCmd.AddPrometheusPortFlag()
|
rpcCmd.AddPrometheusPortFlag()
|
||||||
if err := rpcCmd.Exec(); err != nil {
|
if err := rpcCmd.Exec(); err != nil {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rpcCmd := cmd.NewRpcCmd()
|
rpcCmd := cmd.NewRpcCmd("third")
|
||||||
rpcCmd.AddPortFlag()
|
rpcCmd.AddPortFlag()
|
||||||
rpcCmd.AddPrometheusPortFlag()
|
rpcCmd.AddPrometheusPortFlag()
|
||||||
if err := rpcCmd.Exec(); err != nil {
|
if err := rpcCmd.Exec(); err != nil {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
rpcCmd := cmd.NewRpcCmd()
|
rpcCmd := cmd.NewRpcCmd("user")
|
||||||
rpcCmd.AddPortFlag()
|
rpcCmd.AddPortFlag()
|
||||||
rpcCmd.AddPrometheusPortFlag()
|
rpcCmd.AddPrometheusPortFlag()
|
||||||
if err := rpcCmd.Exec(); err != nil {
|
if err := rpcCmd.Exec(); err != nil {
|
||||||
|
1
go.mod
1
go.mod
@ -55,6 +55,7 @@ require (
|
|||||||
github.com/jonboulle/clockwork v0.3.0 // indirect
|
github.com/jonboulle/clockwork v0.3.0 // indirect
|
||||||
github.com/lestrrat-go/strftime v1.0.6 // indirect
|
github.com/lestrrat-go/strftime v1.0.6 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.17 // 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/spf13/cobra v1.6.1
|
||||||
github.com/ugorji/go/codec v1.2.8 // indirect
|
github.com/ugorji/go/codec v1.2.8 // indirect
|
||||||
go.uber.org/zap v1.24.0
|
go.uber.org/zap v1.24.0
|
||||||
|
2
go.sum
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/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-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/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 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
|
||||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
|
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=
|
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||||
|
@ -7,7 +7,7 @@ type ApiCmd struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewApiCmd() *ApiCmd {
|
func NewApiCmd() *ApiCmd {
|
||||||
return &ApiCmd{NewRootCmd()}
|
return &ApiCmd{NewRootCmd("api")}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ApiCmd) AddApi(f func(port int) error) {
|
func (a *ApiCmd) AddApi(f func(port int) error) {
|
||||||
|
@ -7,7 +7,7 @@ type CronTaskCmd struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewCronTaskCmd() *CronTaskCmd {
|
func NewCronTaskCmd() *CronTaskCmd {
|
||||||
return &CronTaskCmd{NewRootCmd()}
|
return &CronTaskCmd{NewRootCmd("cronTask")}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CronTaskCmd) addRunE(f func() error) {
|
func (c *CronTaskCmd) addRunE(f func() error) {
|
||||||
|
@ -12,7 +12,7 @@ type MsgGatewayCmd struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewMsgGatewayCmd() MsgGatewayCmd {
|
func NewMsgGatewayCmd() MsgGatewayCmd {
|
||||||
return MsgGatewayCmd{NewRootCmd()}
|
return MsgGatewayCmd{NewRootCmd("msgGateway")}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgGatewayCmd) AddWsPortFlag() {
|
func (m *MsgGatewayCmd) AddWsPortFlag() {
|
||||||
|
@ -10,7 +10,7 @@ type MsgTransferCmd struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewMsgTransferCmd() MsgTransferCmd {
|
func NewMsgTransferCmd() MsgTransferCmd {
|
||||||
return MsgTransferCmd{NewRootCmd()}
|
return MsgTransferCmd{NewRootCmd("msgTransfer")}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgTransferCmd) addRunE() {
|
func (m *MsgTransferCmd) addRunE() {
|
||||||
|
@ -18,7 +18,7 @@ type MsgUtilsCmd struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewMsgUtilsCmd() MsgUtilsCmd {
|
func NewMsgUtilsCmd() MsgUtilsCmd {
|
||||||
return MsgUtilsCmd{RootCmd: NewRootCmd()}
|
return MsgUtilsCmd{RootCmd: NewRootCmd("msgUtils")}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) AddUserIDFlag() {
|
func (m *MsgUtilsCmd) AddUserIDFlag() {
|
||||||
|
@ -3,23 +3,29 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"OpenIM/pkg/common/config"
|
"OpenIM/pkg/common/config"
|
||||||
"OpenIM/pkg/common/constant"
|
"OpenIM/pkg/common/constant"
|
||||||
|
"OpenIM/pkg/common/log"
|
||||||
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RootCmd struct {
|
type RootCmd struct {
|
||||||
Command cobra.Command
|
Command cobra.Command
|
||||||
|
Name string
|
||||||
port int
|
port int
|
||||||
prometheusPort int
|
prometheusPort int
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRootCmd() (rootCmd *RootCmd) {
|
func NewRootCmd(name string) (rootCmd *RootCmd) {
|
||||||
rootCmd = &RootCmd{}
|
rootCmd = &RootCmd{Name: name}
|
||||||
c := cobra.Command{
|
c := cobra.Command{
|
||||||
Use: "start",
|
Use: "start",
|
||||||
Short: "Start the server",
|
Short: fmt.Sprintf(`Start %s server`, name),
|
||||||
Long: `Start the server`,
|
Long: fmt.Sprintf(`Start %s server`, name),
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
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
|
rootCmd.Command = c
|
||||||
@ -27,12 +33,6 @@ func NewRootCmd() (rootCmd *RootCmd) {
|
|||||||
return 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() {
|
func (r *RootCmd) addConfFlag() {
|
||||||
r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
|
r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,8 @@ type RpcCmd struct {
|
|||||||
*RootCmd
|
*RootCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRpcCmd() *RpcCmd {
|
func NewRpcCmd(name string) *RpcCmd {
|
||||||
authCmd := &RpcCmd{NewRootCmd()}
|
authCmd := &RpcCmd{NewRootCmd(name)}
|
||||||
return authCmd
|
return authCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
39
pkg/common/log/lfs_hk.go
Normal file
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{})
|
Info(ctx context.Context, msg string, keysAndValues ...interface{})
|
||||||
Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{})
|
Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{})
|
||||||
Error(ctx context.Context, msg string, err error, keysAndValues ...interface{})
|
Error(ctx context.Context, msg string, err error, keysAndValues ...interface{})
|
||||||
WithValues(keysAndValues ...interface{}) LogrusLogger
|
WithValues(keysAndValues ...interface{}) Logger
|
||||||
WithName(name string) LogrusLogger
|
WithName(name string) Logger
|
||||||
WithCallDepth(depth int) LogrusLogger
|
WithCallDepth(depth int) Logger
|
||||||
}
|
}
|
||||||
|
@ -2,15 +2,16 @@ package log
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"OpenIM/pkg/common/config"
|
"OpenIM/pkg/common/config"
|
||||||
|
"OpenIM/pkg/common/tracelog"
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
|
|
||||||
//"bufio"
|
//"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
nested "github.com/antonfisher/nested-logrus-formatter"
|
nested "github.com/antonfisher/nested-logrus-formatter"
|
||||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
|
||||||
"github.com/rifflock/lfshook"
|
|
||||||
"github.com/sirupsen/logrus"
|
"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{
|
func InfoKv(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||||||
logrus.DebugLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
|
operationID := tracelog.GetOperationID(ctx)
|
||||||
logrus.InfoLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
|
logger.WithFields(logrus.Fields{
|
||||||
logrus.WarnLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
|
"OperationID": operationID,
|
||||||
logrus.ErrorLevel: initRotateLogs(rotationTime, maxRemainNum, "all", moduleName),
|
"PID": logger.Pid,
|
||||||
}, &nested.Formatter{
|
"Msg": msg,
|
||||||
TimestampFormat: "2006-01-02 15:04:05.000",
|
}).Infoln(keysAndValues)
|
||||||
HideKeys: false,
|
|
||||||
FieldsOrder: []string{"PID", "FilePath", "OperationID"},
|
|
||||||
})
|
|
||||||
return lfsHook
|
|
||||||
}
|
}
|
||||||
func initRotateLogs(rotationTime time.Duration, maxRemainNum uint, level string, moduleName string) *rotatelogs.RotateLogs {
|
|
||||||
if moduleName != "" {
|
func DebugKv(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||||||
moduleName = moduleName + "."
|
|
||||||
}
|
}
|
||||||
writer, err := rotatelogs.New(
|
|
||||||
config.Config.Log.StorageLocation+moduleName+level+"."+"%Y-%m-%d",
|
func ErrorKv(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
|
||||||
rotatelogs.WithRotationTime(rotationTime),
|
|
||||||
rotatelogs.WithRotationCount(maxRemainNum),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(err.Error())
|
|
||||||
} else {
|
|
||||||
return writer
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WarnKv(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Info(OperationID string, args ...interface{}) {
|
func Info(OperationID string, args ...interface{}) {
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package log
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"OpenIM/pkg/common/config"
|
||||||
"OpenIM/pkg/common/constant"
|
"OpenIM/pkg/common/constant"
|
||||||
"OpenIM/pkg/common/tracelog"
|
"OpenIM/pkg/common/tracelog"
|
||||||
"context"
|
"context"
|
||||||
|
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@ -12,6 +15,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
pkgLogger Logger = &ZapLogger{}
|
pkgLogger Logger = &ZapLogger{}
|
||||||
|
sp = string(filepath.Separator)
|
||||||
)
|
)
|
||||||
|
|
||||||
// InitFromConfig initializes a Zap-based logger
|
// InitFromConfig initializes a Zap-based logger
|
||||||
@ -24,39 +28,25 @@ func InitFromConfig(name string) error {
|
|||||||
return nil
|
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...)
|
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...)
|
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...)
|
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...)
|
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 {
|
type ZapLogger struct {
|
||||||
zap *zap.SugaredLogger
|
zap *zap.SugaredLogger
|
||||||
// store original logger without sampling to avoid multiple samplers
|
// store original logger without sampling to avoid multiple samplers
|
||||||
unsampled *zap.SugaredLogger
|
|
||||||
SampleDuration time.Duration
|
SampleDuration time.Duration
|
||||||
SampleInitial int
|
SampleInitial int
|
||||||
SampleInterval int
|
SampleInterval int
|
||||||
@ -68,23 +58,44 @@ func NewZapLogger() (*ZapLogger, error) {
|
|||||||
Development: true,
|
Development: true,
|
||||||
Encoding: "json",
|
Encoding: "json",
|
||||||
EncoderConfig: zap.NewProductionEncoderConfig(),
|
EncoderConfig: zap.NewProductionEncoderConfig(),
|
||||||
OutputPaths: []string{"stdout"},
|
|
||||||
ErrorOutputPaths: []string{"stdout"},
|
|
||||||
Sampling: &zap.SamplingConfig{
|
|
||||||
Initial: 0,
|
|
||||||
Thereafter: 0,
|
|
||||||
Hook: nil,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
zl := &ZapLogger{
|
zl.zap = l.Sugar()
|
||||||
unsampled: l.Sugar(),
|
return zl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
func (l *ZapLogger) ToZap() *zap.SugaredLogger {
|
||||||
@ -120,57 +131,17 @@ func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndVal
|
|||||||
func (l *ZapLogger) WithValues(keysAndValues ...interface{}) Logger {
|
func (l *ZapLogger) WithValues(keysAndValues ...interface{}) Logger {
|
||||||
dup := *l
|
dup := *l
|
||||||
dup.zap = l.zap.With(keysAndValues...)
|
dup.zap = l.zap.With(keysAndValues...)
|
||||||
if l.unsampled == l.zap {
|
|
||||||
dup.unsampled = dup.zap
|
|
||||||
} else {
|
|
||||||
dup.unsampled = l.unsampled.With(keysAndValues...)
|
|
||||||
}
|
|
||||||
return &dup
|
return &dup
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ZapLogger) WithName(name string) Logger {
|
func (l *ZapLogger) WithName(name string) Logger {
|
||||||
dup := *l
|
dup := *l
|
||||||
dup.zap = l.zap.Named(name)
|
dup.zap = l.zap.Named(name)
|
||||||
if l.unsampled == l.zap {
|
|
||||||
dup.unsampled = dup.zap
|
|
||||||
} else {
|
|
||||||
dup.unsampled = l.unsampled.Named(name)
|
|
||||||
}
|
|
||||||
return &dup
|
return &dup
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ZapLogger) WithCallDepth(depth int) Logger {
|
func (l *ZapLogger) WithCallDepth(depth int) Logger {
|
||||||
dup := *l
|
dup := *l
|
||||||
dup.zap = l.zap.WithOptions(zap.AddCallerSkip(depth))
|
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
|
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 {
|
func GetOperationID(ctx context.Context) string {
|
||||||
|
if ctx.Value(TraceLogKey) != nil {
|
||||||
f, ok := ctx.Value(TraceLogKey).(*FuncInfos)
|
f, ok := ctx.Value(TraceLogKey).(*FuncInfos)
|
||||||
if ok {
|
if ok {
|
||||||
return f.OperationID
|
return f.OperationID
|
||||||
}
|
}
|
||||||
return utils.GetFuncName(1)
|
}
|
||||||
|
return utils.GetFuncName(2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOpUserID(ctx context.Context) string {
|
func GetOpUserID(ctx context.Context) string {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user