mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
fix cron
This commit is contained in:
parent
d545a7f753
commit
662254f30f
@ -5,7 +5,9 @@ import (
|
||||
"OpenIM/pkg/common/cmd"
|
||||
"OpenIM/pkg/common/config"
|
||||
"OpenIM/pkg/common/log"
|
||||
log2 "OpenIM/pkg/common/logger"
|
||||
"OpenIM/pkg/common/mw"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/OpenIMSDK/openKeeper"
|
||||
"os"
|
||||
@ -40,6 +42,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)
|
||||
err = router.Run(address)
|
||||
if err != nil {
|
||||
log.Error("", "api run failed ", address, err.Error())
|
||||
|
76
go.mod
76
go.mod
@ -1,6 +1,6 @@
|
||||
module OpenIM
|
||||
|
||||
go 1.18
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
firebase.google.com/go v3.13.0+incompatible
|
||||
@ -47,80 +47,9 @@ require (
|
||||
github.com/minio/minio-go v6.0.14+incompatible
|
||||
)
|
||||
|
||||
require (
|
||||
cloud.google.com/go v0.105.0 // indirect
|
||||
cloud.google.com/go/compute v1.13.0 // indirect
|
||||
cloud.google.com/go/compute/metadata v0.2.1 // indirect
|
||||
cloud.google.com/go/firestore v1.9.0 // indirect
|
||||
cloud.google.com/go/iam v0.8.0 // indirect
|
||||
cloud.google.com/go/longrunning v0.3.0 // indirect
|
||||
cloud.google.com/go/storage v1.27.0 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/dustin/go-humanize v1.0.0 // indirect
|
||||
github.com/eapache/go-resiliency v1.2.0 // indirect
|
||||
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 // indirect
|
||||
github.com/eapache/queue v1.1.0 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.6.0 // indirect
|
||||
github.com/go-stack/stack v1.8.0 // indirect
|
||||
github.com/go-zookeeper/zk v1.0.3 // indirect
|
||||
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
|
||||
github.com/golang/snappy v0.0.3 // indirect
|
||||
github.com/google/go-cmp v0.5.9 // indirect
|
||||
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
|
||||
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
|
||||
github.com/hashicorp/go-uuid v1.0.2 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.0.1 // indirect
|
||||
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
|
||||
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
|
||||
github.com/jcmturner/gofork v1.0.0 // indirect
|
||||
github.com/jcmturner/gokrb5/v8 v8.4.2 // indirect
|
||||
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/josharian/intern v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.13.6 // indirect
|
||||
github.com/klauspost/cpuid v1.3.1 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/lithammer/shortuuid v3.0.0+incompatible // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
|
||||
github.com/minio/md5-simd v1.1.0 // indirect
|
||||
github.com/minio/sha256-simd v0.1.1 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
|
||||
github.com/pierrec/lz4 v2.6.0+incompatible // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/prometheus/client_model v0.2.0 // indirect
|
||||
github.com/prometheus/common v0.37.0 // indirect
|
||||
github.com/prometheus/procfs v0.8.0 // indirect
|
||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
|
||||
github.com/rs/xid v1.2.1 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||
github.com/xdg-go/scram v1.0.2 // indirect
|
||||
github.com/xdg-go/stringprep v1.0.2 // indirect
|
||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
|
||||
go.opencensus.io v0.24.0 // indirect
|
||||
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
|
||||
golang.org/x/sync v0.1.0 // indirect
|
||||
golang.org/x/sys v0.4.0 // indirect
|
||||
golang.org/x/text v0.6.0 // indirect
|
||||
golang.org/x/time v0.1.0 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
|
||||
google.golang.org/appengine v1.6.7 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-logr/logr v1.2.3
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/goccy/go-json v0.10.0 // indirect
|
||||
github.com/jonboulle/clockwork v0.3.0 // indirect
|
||||
@ -128,6 +57,7 @@ require (
|
||||
github.com/mattn/go-isatty v0.0.17 // indirect
|
||||
github.com/spf13/cobra v1.6.1
|
||||
github.com/ugorji/go/codec v1.2.8 // indirect
|
||||
go.uber.org/zap v1.24.0
|
||||
golang.org/x/crypto v0.5.0 // indirect
|
||||
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
|
||||
gopkg.in/ini.v1 v1.66.2 // indirect
|
||||
|
14
go.sum
14
go.sum
@ -389,8 +389,6 @@ firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIw
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
|
||||
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
|
||||
github.com/OpenIMSDK/openKeeper v0.0.3 h1:pkyLFuIau58SjQLeg4CWMSkP79nbmR3DP0NfBJZ7Kr8=
|
||||
github.com/OpenIMSDK/openKeeper v0.0.3/go.mod h1:RvyRXEcvWbonkmHLtT8KxGSCNlXY7OfDohhu53E6INU=
|
||||
github.com/OpenIMSDK/openKeeper v0.0.4 h1:WwieeVzNCMYvd6HWRFh0cqrem3OoXDSJePA7taZ1ahQ=
|
||||
github.com/OpenIMSDK/openKeeper v0.0.4/go.mod h1:RvyRXEcvWbonkmHLtT8KxGSCNlXY7OfDohhu53E6INU=
|
||||
github.com/OpenIMSDK/open_utils v1.0.8 h1:IopxWgJwEF5ZAPsRuiZZOfcxNOQOCt/p8VDENcHN9r4=
|
||||
@ -408,6 +406,8 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd
|
||||
github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ=
|
||||
github.com/antonfisher/nested-logrus-formatter v1.3.1/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA=
|
||||
github.com/aws/aws-sdk-go v1.38.3/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
|
||||
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
||||
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
@ -487,6 +487,8 @@ github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9
|
||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
|
||||
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
|
||||
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
|
||||
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
|
||||
@ -863,6 +865,14 @@ go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
|
||||
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
|
||||
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
|
||||
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
||||
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
|
||||
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
|
@ -118,7 +118,7 @@ func (c *MsgTool) FixGroupSeq(ctx context.Context, groupID string) error {
|
||||
func (c *MsgTool) fixGroupSeq(ctx context.Context, groupID string, userIDs []string) error {
|
||||
_, maxSeqMongo, maxSeqCache, err := c.msgDatabase.GetSuperGroupMinMaxSeqInMongoAndCache(ctx, groupID)
|
||||
if err != nil {
|
||||
if err != unrelation.ErrMsgNotFound {
|
||||
if err == unrelation.ErrMsgNotFound {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
|
@ -3,6 +3,7 @@ package cmd
|
||||
import (
|
||||
"OpenIM/pkg/common/config"
|
||||
"OpenIM/pkg/common/constant"
|
||||
log "OpenIM/pkg/common/logger"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -19,6 +20,16 @@ func NewRootCmd() (rootCmd *RootCmd) {
|
||||
Short: "Start the server",
|
||||
Long: `Start the server`,
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
log.InitFromConfig(log.Config{
|
||||
JSON: true,
|
||||
Level: "",
|
||||
Sample: false,
|
||||
SampleInitial: 0,
|
||||
SampleInterval: 0,
|
||||
ItemSampleSeconds: 0,
|
||||
ItemSampleInitial: 0,
|
||||
ItemSampleInterval: 0,
|
||||
}, "newlog")
|
||||
return rootCmd.getConfFromCmdAndInit(cmd)
|
||||
},
|
||||
}
|
||||
@ -27,6 +38,12 @@ 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")
|
||||
}
|
||||
|
23
pkg/common/logger/config.go
Normal file
23
pkg/common/logger/config.go
Normal file
@ -0,0 +1,23 @@
|
||||
package log
|
||||
|
||||
type Config struct {
|
||||
JSON bool `yaml:"json"`
|
||||
Level string `yaml:"level"`
|
||||
// true to enable log sampling, where the same log message and level will be throttled.
|
||||
// we have two layers of sampling
|
||||
// 1. global sampling - within a second, it will log the first SampleInitial, then every SampleInterval messages.
|
||||
// 2. per participant/track sampling - to be used with Logger.WithItemSampler(). This would be used to throttle
|
||||
// the logs for a particular participant/track.
|
||||
Sample bool `yaml:"sample,omitempty"`
|
||||
|
||||
// global sampling per server
|
||||
// when sampling, the first N logs will be logged
|
||||
SampleInitial int `yaml:"sample_initial,omitempty"`
|
||||
// when sampling, every Mth log will be logged
|
||||
SampleInterval int `yaml:"sample_interval,omitempty"`
|
||||
|
||||
// participant/track level sampling
|
||||
ItemSampleSeconds int `yaml:"item_sample_seconds,omitempty"`
|
||||
ItemSampleInitial int `yaml:"item_sample_initial,omitempty"`
|
||||
ItemSampleInterval int `yaml:"item_sample_interval,omitempty"`
|
||||
}
|
277
pkg/common/logger/log.go
Normal file
277
pkg/common/logger/log.go
Normal file
@ -0,0 +1,277 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"OpenIM/pkg/common/constant"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
var (
|
||||
discardLogger = logr.Discard()
|
||||
defaultLogger Logger = LogRLogger(discardLogger)
|
||||
pkgLogger Logger = LogRLogger(discardLogger)
|
||||
)
|
||||
|
||||
// InitFromConfig initializes a Zap-based logger
|
||||
func InitFromConfig(conf Config, name string) {
|
||||
l, err := NewZapLogger(&conf)
|
||||
if err == nil {
|
||||
setLogger(l, name)
|
||||
}
|
||||
}
|
||||
|
||||
// GetLogger returns the logger that was set with SetLogger with an extra depth of 1
|
||||
func GetLogger() Logger {
|
||||
return defaultLogger
|
||||
}
|
||||
|
||||
// SetLogger lets you use a custom logger. Pass in a logr.Logger with default depth
|
||||
func setLogger(l Logger, name string) {
|
||||
defaultLogger = l.WithCallDepth(1).WithName(name)
|
||||
// pkg wrapper needs to drop two levels of depth
|
||||
pkgLogger = l.WithCallDepth(2).WithName(name)
|
||||
}
|
||||
|
||||
func Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||||
pkgLogger.Debug(ctx, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||||
pkgLogger.Info(ctx, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func Warn(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{}) {
|
||||
pkgLogger.Error(ctx, msg, err, keysAndValues...)
|
||||
}
|
||||
|
||||
func ParseZapLevel(level string) zapcore.Level {
|
||||
lvl := zapcore.InfoLevel
|
||||
if level != "" {
|
||||
_ = lvl.UnmarshalText([]byte(level))
|
||||
}
|
||||
return lvl
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func NewZapLogger(conf *Config) (*ZapLogger, error) {
|
||||
lvl := ParseZapLevel(conf.Level)
|
||||
zapConfig := zap.Config{
|
||||
Level: zap.NewAtomicLevelAt(lvl),
|
||||
Development: false,
|
||||
Encoding: "console",
|
||||
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
|
||||
OutputPaths: []string{"stderr"},
|
||||
ErrorOutputPaths: []string{"stderr"},
|
||||
}
|
||||
if conf.JSON {
|
||||
zapConfig.Encoding = "json"
|
||||
zapConfig.EncoderConfig = zap.NewProductionEncoderConfig()
|
||||
}
|
||||
l, err := zapConfig.Build()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
zl := &ZapLogger{
|
||||
unsampled: l.Sugar(),
|
||||
SampleDuration: time.Duration(conf.ItemSampleSeconds) * time.Second,
|
||||
SampleInitial: conf.ItemSampleInitial,
|
||||
SampleInterval: conf.ItemSampleInterval,
|
||||
}
|
||||
|
||||
if conf.Sample {
|
||||
// use a sampling logger for the main logger
|
||||
samplingConf := &zap.SamplingConfig{
|
||||
Initial: conf.SampleInitial,
|
||||
Thereafter: conf.SampleInterval,
|
||||
}
|
||||
// sane defaults
|
||||
if samplingConf.Initial == 0 {
|
||||
samplingConf.Initial = 20
|
||||
}
|
||||
if samplingConf.Thereafter == 0 {
|
||||
samplingConf.Thereafter = 100
|
||||
}
|
||||
zl.zap = l.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
|
||||
return zapcore.NewSamplerWithOptions(
|
||||
core,
|
||||
time.Second,
|
||||
samplingConf.Initial,
|
||||
samplingConf.Thereafter,
|
||||
)
|
||||
})).Sugar()
|
||||
} else {
|
||||
zl.zap = zl.unsampled
|
||||
}
|
||||
return zl, nil
|
||||
}
|
||||
|
||||
func (l *ZapLogger) ToZap() *zap.SugaredLogger {
|
||||
return l.zap
|
||||
}
|
||||
|
||||
func (l *ZapLogger) Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||
l.zap.Debugw(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (l *ZapLogger) Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||
l.zap.Infow(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (l *ZapLogger) Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
|
||||
if err != nil {
|
||||
keysAndValues = append(keysAndValues, "error", err)
|
||||
}
|
||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||
l.zap.Warnw(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
|
||||
if err != nil {
|
||||
keysAndValues = append(keysAndValues, "error", err)
|
||||
}
|
||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||
l.zap.Errorw(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (l *ZapLogger) WithValues(keysAndValues ...interface{}) Logger {
|
||||
dup := *l
|
||||
dup.zap = l.zap.With(keysAndValues...)
|
||||
// mirror unsampled logger too
|
||||
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
|
||||
}
|
||||
|
||||
type LogRLogger logr.Logger
|
||||
|
||||
func (l LogRLogger) toLogr() logr.Logger {
|
||||
if logr.Logger(l).GetSink() == nil {
|
||||
return discardLogger
|
||||
}
|
||||
return logr.Logger(l)
|
||||
}
|
||||
|
||||
func (l LogRLogger) Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||
l.toLogr().V(1).Info(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (l LogRLogger) Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||
l.toLogr().Info(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (l LogRLogger) Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
|
||||
if err != nil {
|
||||
keysAndValues = append(keysAndValues, "error", err)
|
||||
}
|
||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||
l.toLogr().Info(msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (l LogRLogger) Error(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
|
||||
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||||
l.toLogr().Error(err, msg, keysAndValues...)
|
||||
}
|
||||
|
||||
func (l LogRLogger) WithValues(keysAndValues ...interface{}) Logger {
|
||||
return LogRLogger(l.toLogr().WithValues(keysAndValues...))
|
||||
}
|
||||
|
||||
func (l LogRLogger) WithName(name string) Logger {
|
||||
return LogRLogger(l.toLogr().WithName(name))
|
||||
}
|
||||
|
||||
func (l LogRLogger) WithCallDepth(depth int) Logger {
|
||||
return LogRLogger(l.toLogr().WithCallDepth(depth))
|
||||
}
|
||||
|
||||
func (l LogRLogger) WithItemSampler() Logger {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l LogRLogger) WithoutSampler() Logger {
|
||||
return l
|
||||
}
|
@ -34,7 +34,11 @@ func SetOperationID(ctx context.Context, operationID string) {
|
||||
}
|
||||
|
||||
func GetOperationID(ctx context.Context) string {
|
||||
return ctx.Value(TraceLogKey).(*FuncInfos).OperationID
|
||||
f, ok := ctx.Value(TraceLogKey).(*FuncInfos)
|
||||
if ok {
|
||||
return f.OperationID
|
||||
}
|
||||
return utils.GetFuncName(1)
|
||||
}
|
||||
|
||||
func GetOpUserID(ctx context.Context) string {
|
||||
|
Loading…
x
Reference in New Issue
Block a user