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

19
log.go
View File

@ -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,13 +207,13 @@ 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)
// 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)
@ -213,3 +221,4 @@ func initlog() {
}
}()
}
}