From 2a6556176df6c4c7b3bc6cc1c7267660e39c6a0f Mon Sep 17 00:00:00 2001 From: Klaus Date: Mon, 19 Oct 2020 20:11:47 +0800 Subject: [PATCH] support set log output path (#508) * support set log output path * err return definition. Co-authored-by: John Sun --- rlog/log.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/rlog/log.go b/rlog/log.go index 444ec3fb..3e3dfd3e 100644 --- a/rlog/log.go +++ b/rlog/log.go @@ -42,6 +42,7 @@ type Logger interface { Error(msg string, fields map[string]interface{}) Fatal(msg string, fields map[string]interface{}) Level(level string) + OutputPath(path string) (err error) } func init() { @@ -102,6 +103,7 @@ func (l *defaultLogger) Fatal(msg string, fields map[string]interface{}) { } l.logger.WithFields(fields).Fatal(msg) } + func (l *defaultLogger) Level(level string) { switch strings.ToLower(level) { case "debug": @@ -115,6 +117,17 @@ func (l *defaultLogger) Level(level string) { } } +func (l *defaultLogger) OutputPath(path string) (err error) { + var file *os.File + file, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + if err != nil { + return + } + + l.logger.Out = file + return +} + // SetLogger use specified logger user customized, in general, we suggest user to replace the default logger with specified func SetLogger(logger Logger) { rLog = logger @@ -126,6 +139,14 @@ func SetLogLevel(level string) { rLog.Level(level) } +func SetOutputPath(path string) (err error) { + if "" == path { + return + } + + return rLog.OutputPath(path) +} + func Debug(msg string, fields map[string]interface{}) { rLog.Debug(msg, fields) }