Skip to content

Commit

Permalink
Fix to use lumberjack only for logging files
Browse files Browse the repository at this point in the history
  • Loading branch information
s1061123 committed Dec 7, 2023
1 parent d97514f commit 540a887
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
25 changes: 19 additions & 6 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ type LogOptions struct {

// SetLogOptions set the LoggingOptions of NetConf
func SetLogOptions(options *LogOptions) {
// logger is used only if filname is supplied
if logger == nil || logger.Filename == "" {
return
}

// give some default value
updatedLogger := lumberjack.Logger{
Filename: logger.Filename,
Expand Down Expand Up @@ -176,16 +181,24 @@ func SetLogStderr(enable bool) {

// SetLogFile sets logging file
func SetLogFile(filename string) {
// logger is used only if filname is supplied
if filename == "" {
return
}

updatedLogger := lumberjack.Logger{
Filename: filename,
MaxAge: logger.MaxAge,
MaxBackups: logger.MaxBackups,
Compress: logger.Compress,
MaxSize: logger.MaxSize,
LocalTime: logger.LocalTime,
MaxAge: 5,
MaxBackups: 5,
Compress: true,
MaxSize: 100,
}

if logger != nil {
updatedLogger.MaxAge = logger.MaxAge
updatedLogger.MaxBackups = logger.MaxBackups
updatedLogger.Compress = logger.Compress
updatedLogger.MaxSize = logger.MaxSize
}
logger = &updatedLogger
loggingW = logger
Expand All @@ -195,5 +208,5 @@ func init() {
loggingStderr = true
loggingW = nil
loggingLevel = PanicLevel
logger = &lumberjack.Logger{}
logger = nil
}
4 changes: 2 additions & 2 deletions pkg/logging/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ var _ = Describe("logging operations", func() {
Verbosef("foobar")
Expect(Errorf("foobar")).NotTo(BeNil())
Panicf("foobar")
logger.Filename = ""
logger = nil
loggingW = nil
err = os.RemoveAll(tmpDir)
Expect(err).NotTo(HaveOccurred())
// Revert the log variable to init
loggingW = nil
logger = &lumberjack.Logger{}
logger = nil
})

// Tests public getter
Expand Down

0 comments on commit 540a887

Please sign in to comment.