Skip to content

Commit

Permalink
update from milvus v2.3.10 (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicedy authored Feb 26, 2024
1 parent 519d7cd commit 52eaa86
Show file tree
Hide file tree
Showing 9 changed files with 934 additions and 485 deletions.
25 changes: 12 additions & 13 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Expand All @@ -27,6 +26,8 @@ const (

// FileLogConfig serializes file log related config in toml/json.
type FileLogConfig struct {
// Log rootpath
RootPath string `toml:"rootpath" json:"rootpath"`
// Log filename, leave empty to disable file log.
Filename string `toml:"filename" json:"filename"`
// Max size for a single file, in MB.
Expand All @@ -35,19 +36,20 @@ type FileLogConfig struct {
MaxDays int `toml:"max-days" json:"max-days"`
// Maximum number of old log files to retain.
MaxBackups int `toml:"max-backups" json:"max-backups"`
// Compress function for rotated files.
// Currently only `gzip` and empty are supported, empty means compression disabled.
Compress string `toml:"compress" json:"compress"`
}

// Config serializes log related config in toml/json.
type Config struct {
// Log level.
Level string `toml:"level" json:"level"`
// Log format. One of json or text.
// grpc log level
GrpcLevel string `toml:"grpc-level" json:"grpc-level"`
// Log format. one of json, text, or console.
Format string `toml:"format" json:"format"`
// Disable automatic timestamps in output.
DisableTimestamp bool `toml:"disable-timestamp" json:"disable-timestamp"`
// Stdout enable or not.
Stdout bool `toml:"stdout" json:"stdout"`
// File log config.
File FileLogConfig `toml:"file" json:"file"`
// Development puts the logger in development mode, which changes the
Expand All @@ -69,13 +71,6 @@ type Config struct {
//
// Values configured here are per-second. See zapcore.NewSampler for details.
Sampling *zap.SamplingConfig `toml:"sampling" json:"sampling"`
// ErrorOutputPath is a path to write internal logger errors to.
// If this field is not set, the internal logger errors will be sent to the same file as in File field.
// Note: if we want to output the logger errors to stderr, we can just set this field to "stderr"
ErrorOutputPath string `toml:"error-output-path" json:"error-output-path"`
// Timeout for writing log, if TiDB hang on writing log, make it panic.
// The value is seconds, 0 means no timeout
Timeout int `toml:"timeout" json:"timeout"`
}

// ZapProperties records some information about zap.
Expand All @@ -85,6 +80,10 @@ type ZapProperties struct {
Level zap.AtomicLevel
}

func newZapTextEncoder(cfg *Config) zapcore.Encoder {
return NewTextEncoderByConfig(cfg)
}

func (cfg *Config) buildOptions(errSink zapcore.WriteSyncer) []zap.Option {
opts := []zap.Option{zap.ErrorOutput(errSink)}

Expand All @@ -106,7 +105,7 @@ func (cfg *Config) buildOptions(errSink zapcore.WriteSyncer) []zap.Option {

if cfg.Sampling != nil {
opts = append(opts, zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewSamplerWithOptions(core, time.Second, int(cfg.Sampling.Initial), int(cfg.Sampling.Thereafter))
return zapcore.NewSamplerWithOptions(core, time.Second, cfg.Sampling.Initial, cfg.Sampling.Thereafter, zapcore.SamplerHook(cfg.Sampling.Hook))
}))
}
return opts
Expand Down
159 changes: 142 additions & 17 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,52 @@
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package log

import (
"context"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// ZapEncodingName is the encoder name registered in zap
var ZapEncodingName = "pingcap-log"
type ctxLogKeyType struct{}

var CtxLogKey = ctxLogKeyType{}

// Debug logs a message at DebugLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Debug(msg string, fields ...zap.Field) {
ll().Debug(msg, fields...)
L().Debug(msg, fields...)
}

// Info logs a message at InfoLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Info(msg string, fields ...zap.Field) {
ll().Info(msg, fields...)
L().Info(msg, fields...)
}

// Warn logs a message at WarnLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Warn(msg string, fields ...zap.Field) {
ll().Warn(msg, fields...)
L().Warn(msg, fields...)
}

// Error logs a message at ErrorLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
func Error(msg string, fields ...zap.Field) {
ll().Error(msg, fields...)
L().Error(msg, fields...)
}

// Panic logs a message at PanicLevel. The message includes any fields passed
// at the log site, as well as any fields accumulated on the logger.
//
// The logger then panics, even if logging at PanicLevel is disabled.
func Panic(msg string, fields ...zap.Field) {
ll().Panic(msg, fields...)
L().Panic(msg, fields...)
}

// Fatal logs a message at FatalLevel. The message includes any fields passed
Expand All @@ -60,25 +62,148 @@ func Panic(msg string, fields ...zap.Field) {
// The logger then calls os.Exit(1), even if logging at FatalLevel is
// disabled.
func Fatal(msg string, fields ...zap.Field) {
ll().Fatal(msg, fields...)
L().Fatal(msg, fields...)
}

// RatedDebug print logs at debug level
// it limit log print to avoid too many logs
// return true if log successfully
func RatedDebug(cost float64, msg string, fields ...zap.Field) bool {
if R().CheckCredit(cost) {
L().Debug(msg, fields...)
return true
}
return false
}

// RatedInfo print logs at info level
// it limit log print to avoid too many logs
// return true if log successfully
func RatedInfo(cost float64, msg string, fields ...zap.Field) bool {
if R().CheckCredit(cost) {
L().Info(msg, fields...)
return true
}
return false
}

// RatedWarn print logs at warn level
// it limit log print to avoid too many logs
// return true if log successfully
func RatedWarn(cost float64, msg string, fields ...zap.Field) bool {
if R().CheckCredit(cost) {
L().Warn(msg, fields...)
return true
}
return false
}

// With creates a child logger and adds structured context to it.
// Fields added to the child don't affect the parent, and vice versa.
//
// Deprecated: With should not add caller skip, since it's not a logging function.
// Please use log.L().With instead. With is kept for compatibility.
// See https://github.com/pingcap/log/issues/32 for more details.
func With(fields ...zap.Field) *zap.Logger {
return L().WithOptions(zap.AddCallerSkip(1)).With(fields...)
func With(fields ...zap.Field) *MLogger {
return &MLogger{
Logger: L().With(fields...).WithOptions(zap.AddCallerSkip(-1)),
}
}

// SetLevel alters the logging level.
func SetLevel(l zapcore.Level) {
globalProperties.Load().(*ZapProperties).Level.SetLevel(l)
_globalP.Load().(*ZapProperties).Level.SetLevel(l)
}

// GetLevel gets the logging level.
func GetLevel() zapcore.Level {
return globalProperties.Load().(*ZapProperties).Level.Level()
return _globalP.Load().(*ZapProperties).Level.Level()
}

// WithTraceID returns a context with trace_id attached
func WithTraceID(ctx context.Context, traceID string) context.Context {
return WithFields(ctx, zap.String("traceID", traceID))
}

// WithReqID adds given reqID field to the logger in ctx
func WithReqID(ctx context.Context, reqID int64) context.Context {
fields := []zap.Field{zap.Int64("reqID", reqID)}
return WithFields(ctx, fields...)
}

// WithModule adds given module field to the logger in ctx
func WithModule(ctx context.Context, module string) context.Context {
fields := []zap.Field{zap.String("module", module)}
return WithFields(ctx, fields...)
}

// WithFields returns a context with fields attached
func WithFields(ctx context.Context, fields ...zap.Field) context.Context {
var zlogger *zap.Logger
if ctxLogger, ok := ctx.Value(CtxLogKey).(*MLogger); ok {
zlogger = ctxLogger.Logger
} else {
zlogger = ctxL()
}
mLogger := &MLogger{
Logger: zlogger.With(fields...),
}
return context.WithValue(ctx, CtxLogKey, mLogger)
}

// Ctx returns a logger which will log contextual messages attached in ctx
func Ctx(ctx context.Context) *MLogger {
if ctx == nil {
return &MLogger{Logger: ctxL()}
}
if ctxLogger, ok := ctx.Value(CtxLogKey).(*MLogger); ok {
return ctxLogger
}
return &MLogger{Logger: ctxL()}
}

// withLogLevel returns ctx with a leveled logger, notes that it will overwrite logger previous attached!
func withLogLevel(ctx context.Context, level zapcore.Level) context.Context {
var zlogger *zap.Logger
switch level {
case zap.DebugLevel:
zlogger = debugL()
case zap.InfoLevel:
zlogger = infoL()
case zap.WarnLevel:
zlogger = warnL()
case zap.ErrorLevel:
zlogger = errorL()
case zap.FatalLevel:
zlogger = fatalL()
default:
zlogger = L()
}
return context.WithValue(ctx, CtxLogKey, &MLogger{Logger: zlogger})
}

// WithDebugLevel returns context with a debug level enabled logger.
// Notes that it will overwrite previous attached logger within context
func WithDebugLevel(ctx context.Context) context.Context {
return withLogLevel(ctx, zapcore.DebugLevel)
}

// WithInfoLevel returns context with a info level enabled logger.
// Notes that it will overwrite previous attached logger within context
func WithInfoLevel(ctx context.Context) context.Context {
return withLogLevel(ctx, zapcore.InfoLevel)
}

// WithWarnLevel returns context with a warning level enabled logger.
// Notes that it will overwrite previous attached logger within context
func WithWarnLevel(ctx context.Context) context.Context {
return withLogLevel(ctx, zapcore.WarnLevel)
}

// WithErrorLevel returns context with a error level enabled logger.
// Notes that it will overwrite previous attached logger within context
func WithErrorLevel(ctx context.Context) context.Context {
return withLogLevel(ctx, zapcore.ErrorLevel)
}

// WithFatalLevel returns context with a fatal level enabled logger.
// Notes that it will overwrite previous attached logger within context
func WithFatalLevel(ctx context.Context) context.Context {
return withLogLevel(ctx, zapcore.FatalLevel)
}
Loading

0 comments on commit 52eaa86

Please sign in to comment.