v1.1.1 donot write to file when in k8s/docker

This commit is contained in:
bryanqiu 2023-04-10 17:12:23 +08:00
parent be4c2164c8
commit d93e63ab09

33
log.go
View File

@ -29,7 +29,6 @@ const (
var ( var (
golog *log.Logger = nil golog *log.Logger = nil
mylog *Logger = nil mylog *Logger = nil
logWriter *cacheWriter = nil
logidCreator LogidCreator = nil logidCreator LogidCreator = nil
modulelogs map[string]*Logger = map[string]*Logger{} 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///////////////////////////////////////////////// // ///////////////////////Initialize/////////////////////////////////////////////////
// ////////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////////
@ -199,17 +207,18 @@ func initlog() {
) )
// Step 2. init golog(golang std logger) & mylog(myself defined logger) // Step 2. init golog(golang std logger) & mylog(myself defined logger)
logWriter = NewCacheWriter(baseWriter) golog = log.New(NewCacheWriter(baseWriter), "", gologFlags)
golog = log.New(logWriter, "", gologFlags)
mylog = New("system") mylog = New("system")
baseWriter.OpenLogFile(logDir, logName, logDirsOptional)
// Step 3. backgroud split log (log rotate) // Step 3. backgroud split log (log rotate) if need
go func() { if needWriteLogToFile() {
for { go func() {
tag := time.Now().Local().Format(logSplitTag) baseWriter.OpenLogFile(logDir, logName, logDirsOptional)
baseWriter.TryBackupLogFile(tag) for {
time.Sleep(2 * time.Minute) tag := time.Now().Local().Format(logSplitTag)
} baseWriter.TryBackupLogFile(tag)
}() time.Sleep(2 * time.Minute)
}
}()
}
} }