This commit is contained in:
wangchuxiao 2023-04-17 19:13:34 +08:00
parent 085fc7adbf
commit 15f6626282

View File

@ -2,6 +2,7 @@ package log
import ( import (
"context" "context"
"errors"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -68,6 +69,8 @@ func NewZapLogger(logLevel int, isStdout bool, isJson bool) (*ZapLogger, error)
} }
if isJson { if isJson {
zapConfig.Encoding = "json" zapConfig.Encoding = "json"
} else {
zapConfig.Encoding = "console"
} }
if isStdout { if isStdout {
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stdout", "stderr") zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stdout", "stderr")
@ -101,11 +104,14 @@ func (l *ZapLogger) cores(logLevel int, isStdout bool) (zap.Option, error) {
return nil, err return nil, err
} }
var cores []zapcore.Core var cores []zapcore.Core
if config.Config.Log.StorageLocation != "" { if config.Config.Log.StorageLocation != "" && !isStdout {
cores = []zapcore.Core{ cores = []zapcore.Core{
zapcore.NewCore(fileEncoder, writer, zap.NewAtomicLevelAt(logLevelMap[logLevel])), zapcore.NewCore(fileEncoder, writer, zap.NewAtomicLevelAt(logLevelMap[logLevel])),
} }
} }
if config.Config.Log.StorageLocation == "" && !isStdout {
return nil, errors.New("log storage location is empty and not stdout")
}
if isStdout { if isStdout {
cores = append(cores, zapcore.NewCore(fileEncoder, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(logLevelMap[logLevel]))) cores = append(cores, zapcore.NewCore(fileEncoder, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(logLevelMap[logLevel])))
} }