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 package checkpoint
// disable dot-imports only for testing
//revive:disable:dot-imports
import ( import (
"fmt" "fmt"
"os" "os"

View File

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

View File

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

View File

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

View File

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

View File

@ -56,6 +56,11 @@ type LogOptions struct {
// SetLogOptions set the LoggingOptions of NetConf // SetLogOptions set the LoggingOptions of NetConf
func SetLogOptions(options *LogOptions) { func SetLogOptions(options *LogOptions) {
// logger is used only if filname is supplied
if logger == nil || logger.Filename == "" {
return
}
// give some default value // give some default value
updatedLogger := lumberjack.Logger{ updatedLogger := lumberjack.Logger{
Filename: logger.Filename, Filename: logger.Filename,
@ -176,16 +181,24 @@ func SetLogStderr(enable bool) {
// SetLogFile sets logging file // SetLogFile sets logging file
func SetLogFile(filename string) { func SetLogFile(filename string) {
// logger is used only if filname is supplied
if filename == "" { if filename == "" {
return return
} }
updatedLogger := lumberjack.Logger{ updatedLogger := lumberjack.Logger{
Filename: filename, Filename: filename,
MaxAge: logger.MaxAge, MaxAge: 5,
MaxBackups: logger.MaxBackups, MaxBackups: 5,
Compress: logger.Compress, Compress: true,
MaxSize: logger.MaxSize, MaxSize: 100,
LocalTime: logger.LocalTime, }
if logger != nil {
updatedLogger.MaxAge = logger.MaxAge
updatedLogger.MaxBackups = logger.MaxBackups
updatedLogger.Compress = logger.Compress
updatedLogger.MaxSize = logger.MaxSize
} }
logger = &updatedLogger logger = &updatedLogger
loggingW = logger loggingW = logger
@ -195,5 +208,5 @@ func init() {
loggingStderr = true loggingStderr = true
loggingW = nil loggingW = nil
loggingLevel = PanicLevel loggingLevel = PanicLevel
logger = &lumberjack.Logger{} logger = nil
} }

View File

@ -15,6 +15,8 @@
package logging package logging
// disable dot-imports only for testing
//revive:disable:dot-imports
import ( import (
"fmt" "fmt"
"os" "os"
@ -106,13 +108,13 @@ var _ = Describe("logging operations", func() {
Verbosef("foobar") Verbosef("foobar")
Expect(Errorf("foobar")).NotTo(BeNil()) Expect(Errorf("foobar")).NotTo(BeNil())
Panicf("foobar") Panicf("foobar")
logger.Filename = "" logger = nil
loggingW = nil loggingW = nil
err = os.RemoveAll(tmpDir) err = os.RemoveAll(tmpDir)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
// Revert the log variable to init // Revert the log variable to init
loggingW = nil loggingW = nil
logger = &lumberjack.Logger{} logger = nil
}) })
// Tests public getter // Tests public getter

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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