Support print log to file and console at the same time (#22080)

Signed-off-by: wayblink <anyang.wang@zilliz.com>
pull/22333/head
wayblink 2023-02-10 14:16:32 +08:00 committed by GitHub
parent f7ff6ab5ea
commit d63bf18236
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 7 deletions

View File

@ -328,9 +328,10 @@ dataNode:
# Configures the system log output.
log:
level: debug # Only supports debug, info, warn, error, panic, or fatal. Default 'info'.
stdout: "true" # default true, print log to stdout
file:
# please adjust in embedded Milvus: /tmp/milvus/logs
rootPath: "" # default to stdout, stderr
rootPath: "" # root dir path to put logs, default "" means no log file will print
maxSize: 300 # MB
maxAge: 10 # Maximum time for log retention in day.
maxBackups: 20

View File

@ -48,6 +48,8 @@ type Config struct {
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

View File

@ -68,23 +68,25 @@ func init() {
// InitLogger initializes a zap logger.
func InitLogger(cfg *Config, opts ...zap.Option) (*zap.Logger, *ZapProperties, error) {
var output zapcore.WriteSyncer
var outputs []zapcore.WriteSyncer
if len(cfg.File.Filename) > 0 {
lg, err := initFileLog(&cfg.File)
if err != nil {
return nil, nil, err
}
output = zapcore.AddSync(lg)
} else {
outputs = append(outputs, zapcore.AddSync(lg))
}
if cfg.Stdout {
stdOut, _, err := zap.Open([]string{"stdout"}...)
if err != nil {
return nil, nil, err
}
output = stdOut
outputs = append(outputs, stdOut)
}
debugCfg := *cfg
debugCfg.Level = "debug"
debugL, r, err := InitLoggerWithWriteSyncer(&debugCfg, output, opts...)
outputsWriter := zap.CombineWriteSyncers(outputs...)
debugL, r, err := InitLoggerWithWriteSyncer(&debugCfg, outputsWriter, opts...)
if err != nil {
return nil, nil, err
}
@ -149,7 +151,7 @@ func initFileLog(cfg *FileLogConfig) (*lumberjack.Logger, error) {
}
func newStdLogger() (*zap.Logger, *ZapProperties) {
conf := &Config{Level: "debug", File: FileLogConfig{}}
conf := &Config{Level: "debug", Stdout: true}
lg, r, _ := InitLogger(conf)
return lg, r
}

View File

@ -35,6 +35,8 @@ import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"testing"
"time"
@ -254,3 +256,32 @@ func TestLeveledLogger(t *testing.T) {
SetLevel(orgLevel)
}
func TestStdAndFileLogger(t *testing.T) {
tmpDir := t.TempDir()
filePath := tmpDir + string(filepath.Separator) + "TestStdAndFileLogger"
fileConf := FileLogConfig{
Filename: filePath,
}
fmt.Println(tmpDir)
conf := &Config{Level: "debug", Stdout: true, File: fileConf}
logger, _, err := InitLogger(conf)
assert.NoError(t, err)
logger.Info("1234567")
fileInfo, err := os.Stat(fileConf.Filename)
assert.NoError(t, err)
assert.NotEmpty(t, fileInfo)
assert.True(t, fileInfo.Size() > 0)
}
func TestStdLogger(t *testing.T) {
conf := &Config{Level: "debug", Stdout: true}
logger, _, err := InitLogger(conf)
assert.NoError(t, err)
logger.Info("1234567")
}

View File

@ -410,6 +410,13 @@ func (gp *BaseTable) InitLogCfg() {
gp.Log.File.MaxSize = gp.ParseIntWithDefault("log.file.maxSize", DefaultMaxSize)
gp.Log.File.MaxBackups = gp.ParseIntWithDefault("log.file.maxBackups", DefaultMaxBackups)
gp.Log.File.MaxDays = gp.ParseIntWithDefault("log.file.maxAge", DefaultMaxAge)
gp.Log.File.RootPath = gp.LoadWithDefault("log.file.rootPath", DefaultRootPath)
stdout, err := strconv.ParseBool(gp.LoadWithDefault("log.stdout", "true"))
if err != nil {
gp.Log.Stdout = true
} else {
gp.Log.Stdout = stdout
}
}
// SetLogConfig set log config of the base table