Merge pull request #1194 from s1061123/fix-logging

Fix to use lumberjack only for logging files
This commit is contained in:
Doug Smith 2023-12-07 09:14:30 -05:00 committed by GitHub
commit c76db9c7a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 53 additions and 8 deletions

View File

@ -15,6 +15,8 @@
package checkpoint
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"fmt"
"os"

View File

@ -15,6 +15,8 @@
// Package cmdutils is the package that contains utilities for multus command
package cmdutils
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

View File

@ -15,6 +15,8 @@
// Package cmdutils is the package that contains utilities for multus command
package cmdutils
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"fmt"
"os"

View File

@ -15,6 +15,8 @@
package k8sclient
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"fmt"
"os"

View File

@ -15,6 +15,8 @@
package kubeletclient
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"context"
"fmt"

View File

@ -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,
@ -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
@ -195,5 +208,5 @@ func init() {
loggingStderr = true
loggingW = nil
loggingLevel = PanicLevel
logger = &lumberjack.Logger{}
logger = nil
}

View File

@ -15,6 +15,8 @@
package logging
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"fmt"
"os"
@ -106,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

View File

@ -14,6 +14,8 @@
package multus
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"context"
"fmt"

View File

@ -14,6 +14,8 @@
package multus
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"context"
"fmt"

View File

@ -14,6 +14,8 @@
package multus
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"context"
"fmt"

View File

@ -14,6 +14,8 @@
package multus
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"bytes"
"context"

View File

@ -15,6 +15,8 @@
package netutils
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"encoding/json"
"net"

View File

@ -14,6 +14,8 @@
package server
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"context"
"os"

View File

@ -14,6 +14,8 @@
package server
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

View File

@ -14,6 +14,8 @@
package server
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

View File

@ -14,6 +14,8 @@
package server
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"context"
"fmt"

View File

@ -15,6 +15,8 @@
package types
// disable dot-imports only for testing
//revive:disable:dot-imports
import (
"encoding/json"
"fmt"