mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 04:14:39 +03:00
Only cache log items that meet minimum log level (#2421)
This commit is contained in:
@@ -3,6 +3,7 @@ package log
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -94,27 +95,36 @@ func (log *Logger) SetLogLevel(level string) {
|
|||||||
func logLevelFromString(level string) logrus.Level {
|
func logLevelFromString(level string) logrus.Level {
|
||||||
ret := logrus.InfoLevel
|
ret := logrus.InfoLevel
|
||||||
|
|
||||||
switch level {
|
switch strings.ToLower(level) {
|
||||||
case "Debug":
|
case "debug":
|
||||||
ret = logrus.DebugLevel
|
ret = logrus.DebugLevel
|
||||||
case "Warning":
|
case "warning":
|
||||||
ret = logrus.WarnLevel
|
ret = logrus.WarnLevel
|
||||||
case "Error":
|
case "error":
|
||||||
ret = logrus.ErrorLevel
|
ret = logrus.ErrorLevel
|
||||||
case "Trace":
|
case "trace":
|
||||||
ret = logrus.TraceLevel
|
ret = logrus.TraceLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (log *Logger) addToCache(l *LogItem) {
|
||||||
|
// assumes mutex held
|
||||||
|
// only add to cache if meets minimum log level
|
||||||
|
level := logLevelFromString(l.Type)
|
||||||
|
if level <= log.logger.Level {
|
||||||
|
log.logCache = append([]LogItem{*l}, log.logCache...)
|
||||||
|
if len(log.logCache) > 30 {
|
||||||
|
log.logCache = log.logCache[:len(log.logCache)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (log *Logger) addLogItem(l *LogItem) {
|
func (log *Logger) addLogItem(l *LogItem) {
|
||||||
log.mutex.Lock()
|
log.mutex.Lock()
|
||||||
l.Time = time.Now()
|
l.Time = time.Now()
|
||||||
log.logCache = append([]LogItem{*l}, log.logCache...)
|
log.addToCache(l)
|
||||||
if len(log.logCache) > 30 {
|
|
||||||
log.logCache = log.logCache[:len(log.logCache)-1]
|
|
||||||
}
|
|
||||||
log.mutex.Unlock()
|
log.mutex.Unlock()
|
||||||
go log.broadcastLogItem(l)
|
go log.broadcastLogItem(l)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user