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) + } + }() + } }