From d93e63ab09c3ba9d1aba9efa9c33ee564de78b62 Mon Sep 17 00:00:00 2001 From: bryanqiu Date: Mon, 10 Apr 2023 17:12:23 +0800 Subject: [PATCH] v1.1.1 donot write to file when in k8s/docker --- log.go | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/log.go b/log.go index 9a43d53..2a4867f 100644 --- a/log.go +++ b/log.go @@ -29,7 +29,6 @@ const ( var ( golog *log.Logger = nil mylog *Logger = nil - logWriter *cacheWriter = nil logidCreator LogidCreator = nil modulelogs map[string]*Logger = map[string]*Logger{} ) @@ -176,6 +175,15 @@ func (log *Logger) logwrite(typ logLevel, calldepth int, format string, v ...int } } +func needWriteLogToFile() bool { + if cls, ok := os.LookupEnv("QOOLOG_CLOSE_FILELOG"); ok && cls != "N" && cls != "n" { + return false + } else if ink8s, ok := os.LookupEnv("KUBERNETES_PORT"); ok && ink8s != "" { + return false + } + return true +} + // ////////////////////////////////////////////////////////////////////////////////// // ///////////////////////Initialize///////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////////// @@ -199,17 +207,18 @@ func initlog() { ) // Step 2. init golog(golang std logger) & mylog(myself defined logger) - logWriter = NewCacheWriter(baseWriter) - golog = log.New(logWriter, "", gologFlags) + golog = log.New(NewCacheWriter(baseWriter), "", gologFlags) mylog = New("system") - baseWriter.OpenLogFile(logDir, logName, logDirsOptional) - // Step 3. backgroud split log (log rotate) - go func() { - for { - tag := time.Now().Local().Format(logSplitTag) - baseWriter.TryBackupLogFile(tag) - time.Sleep(2 * time.Minute) - } - }() + // Step 3. backgroud split log (log rotate) if need + if needWriteLogToFile() { + go func() { + baseWriter.OpenLogFile(logDir, logName, logDirsOptional) + for { + tag := time.Now().Local().Format(logSplitTag) + baseWriter.TryBackupLogFile(tag) + time.Sleep(2 * time.Minute) + } + }() + } }