feat: add env var to set log level

This commit is contained in:
RockYang 2023-07-31 08:34:11 +08:00
parent e7ac26ff5a
commit 9eafd3e6ca
3 changed files with 17 additions and 6 deletions

View File

@ -57,7 +57,7 @@ func (h *ConfigHandler) Update(c *gin.Context) {
resp.ERROR(c, "Failed to update config cache: "+err.Error())
return
}
logger.Infof("Update AppServer's config successfully: %v", config.Config)
logger.Debugf("Update AppServer's config successfully: %v", config.Config)
}
resp.SUCCESS(c, config)

View File

@ -205,9 +205,7 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session types.ChatSession
}
}
if h.App.Debug { // 调试打印聊天上下文
logger.Info("聊天上下文:", chatCtx)
}
logger.Debugf("聊天上下文:%+v", chatCtx)
}
reqMgs := make([]interface{}, 0)
for _, m := range chatCtx {
@ -312,7 +310,6 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session types.ChatSession
logger.Info(functionName)
logger.Info(arguments)
f := h.App.Functions[functionName]
// TODO 调用函数完成任务
data, err := f.Invoke(arguments)
if err != nil {
msg := "调用函数出错:" + err.Error()

View File

@ -5,6 +5,7 @@ import (
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"strings"
)
var logger *zap.Logger
@ -15,7 +16,7 @@ func GetLogger() *zap.SugaredLogger {
return sugarLogger
}
logLevel := zap.NewAtomicLevelAt(zapcore.InfoLevel)
logLevel := zap.NewAtomicLevelAt(getLogLevel(os.Getenv("LOG_LEVEL")))
encoder := getEncoder()
writerSyncer := getLogWriter()
fileCore := zapcore.NewCore(encoder, writerSyncer, logLevel)
@ -58,3 +59,16 @@ func getLogWriter() zapcore.WriteSyncer {
}
return zapcore.AddSync(lumberJackLogger)
}
func getLogLevel(level string) zapcore.Level {
switch strings.ToUpper(level) {
case "DEBUG":
return zapcore.DebugLevel
case "WARN":
return zapcore.WarnLevel
case "ERROR":
return zapcore.ErrorLevel
default:
return zapcore.InfoLevel
}
}