mirror of
https://github.com/NVIDIA/nvidia-container-toolkit
synced 2025-01-22 18:47:32 +00:00
Fix logger.Update and Reset
Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
parent
7a3aabbbda
commit
7789ac6331
@ -44,9 +44,15 @@ func NewLogger() *Logger {
|
|||||||
|
|
||||||
// Update constructs a Logger with a preddefined formatter
|
// Update constructs a Logger with a preddefined formatter
|
||||||
func (l *Logger) Update(filename string, logLevel string, argv []string) error {
|
func (l *Logger) Update(filename string, logLevel string, argv []string) error {
|
||||||
|
|
||||||
configFromArgs := parseArgs(argv)
|
configFromArgs := parseArgs(argv)
|
||||||
|
|
||||||
level, logLevelError := configFromArgs.getLevel(logLevel)
|
level, logLevelError := configFromArgs.getLevel(logLevel)
|
||||||
|
defer func() {
|
||||||
|
if logLevelError != nil {
|
||||||
|
l.Warn(logLevelError)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
var logFiles []*os.File
|
var logFiles []*os.File
|
||||||
var argLogFileError error
|
var argLogFileError error
|
||||||
@ -67,15 +73,15 @@ func (l *Logger) Update(filename string, logLevel string, argv []string) error {
|
|||||||
}
|
}
|
||||||
argLogFileError = err
|
argLogFileError = err
|
||||||
}
|
}
|
||||||
|
defer func() {
|
||||||
|
if argLogFileError != nil {
|
||||||
|
l.Warnf("Failed to open log file: %v", argLogFileError)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
previous := l.Logger
|
newLogger := logrus.New()
|
||||||
l = &Logger{
|
|
||||||
Logger: logrus.New(),
|
|
||||||
previousLogger: previous,
|
|
||||||
logFiles: logFiles,
|
|
||||||
}
|
|
||||||
|
|
||||||
l.SetLevel(level)
|
newLogger.SetLevel(level)
|
||||||
if level == logrus.DebugLevel {
|
if level == logrus.DebugLevel {
|
||||||
logrus.SetReportCaller(true)
|
logrus.SetReportCaller(true)
|
||||||
// Shorten function and file names reported by the logger, by
|
// Shorten function and file names reported by the logger, by
|
||||||
@ -93,27 +99,25 @@ func (l *Logger) Update(filename string, logLevel string, argv []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if configFromArgs.format == "json" {
|
if configFromArgs.format == "json" {
|
||||||
l.SetFormatter(new(logrus.JSONFormatter))
|
newLogger.SetFormatter(new(logrus.JSONFormatter))
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(logFiles) == 0 {
|
if len(logFiles) == 0 {
|
||||||
l.SetOutput(io.Discard)
|
newLogger.SetOutput(io.Discard)
|
||||||
} else if len(logFiles) == 1 {
|
} else if len(logFiles) == 1 {
|
||||||
l.SetOutput(logFiles[0])
|
newLogger.SetOutput(logFiles[0])
|
||||||
} else if len(logFiles) > 1 {
|
} else if len(logFiles) > 1 {
|
||||||
var writers []io.Writer
|
var writers []io.Writer
|
||||||
for _, f := range logFiles {
|
for _, f := range logFiles {
|
||||||
writers = append(writers, f)
|
writers = append(writers, f)
|
||||||
}
|
}
|
||||||
l.SetOutput(io.MultiWriter(writers...))
|
newLogger.SetOutput(io.MultiWriter(writers...))
|
||||||
}
|
}
|
||||||
|
|
||||||
if logLevelError != nil {
|
*l = Logger{
|
||||||
l.Warn(logLevelError)
|
Logger: newLogger,
|
||||||
}
|
previousLogger: l.Logger,
|
||||||
|
logFiles: logFiles,
|
||||||
if argLogFileError != nil {
|
|
||||||
l.Warnf("Failed to open log file: %v", argLogFileError)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -127,7 +131,9 @@ func (l *Logger) Reset() error {
|
|||||||
if previous == nil {
|
if previous == nil {
|
||||||
previous = logrus.New()
|
previous = logrus.New()
|
||||||
}
|
}
|
||||||
l = &Logger{Logger: previous}
|
l.Logger = previous
|
||||||
|
l.previousLogger = nil
|
||||||
|
l.logFiles = nil
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var errs []error
|
var errs []error
|
||||||
|
Loading…
Reference in New Issue
Block a user