Skip to content

Commit 074eab9

Browse files
learnitallldelossa
authored andcommitted
log: Do not export InitializeDefaultLogger function
[ upstream commit 00ab252 ] [ Backporter's notes: ignoring changes from cilium#26327 ] The DefaultLogger variable in the logging module serves as a parent logger which all other loggers can be derived from. It is initialized using the InitializeDefaultLogger function and then adjusted on startup based on user configuration. Users should not call InitializeDefaultLogger to create a parent logger for their logger, since the logger returned by InitializeDefaultLogger will always use the hardcoded defaults. For example, the logger returned will always be of level INFO, even if a user has enabled debug logging. To make this clear, this commit renames InitializeDefaultLogger to initializeDefaultLogger to signal that it should not be used outside of the logging module. Fixes: cilium#29215 Signed-off-by: Ryan Drew <[email protected]>
1 parent 1e2a73f commit 074eab9

File tree

3 files changed

+38
-20
lines changed

3 files changed

+38
-20
lines changed

pkg/endpoint/log.go

+14-17
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,9 @@ func (e *Endpoint) UpdateLogger(fields map[string]interface{}) {
9191
return
9292
}
9393

94-
// default to a new default logger
95-
baseLogger := logging.InitializeDefaultLogger()
96-
97-
// If this endpoint is set to debug ensure it will print debug by giving it
98-
// an independent logger
99-
if e.Options != nil && e.Options.IsEnabled(option.Debug) {
100-
baseLogger.SetLevel(logrus.DebugLevel)
101-
} else {
102-
// Debug mode takes priority; if not in debug, check what log level user
103-
// has set and set the endpoint's log to log at that level.
104-
baseLogger.SetLevel(logging.DefaultLogger.Level)
105-
}
106-
10794
// When adding new fields, make sure they are abstracted by a setter
10895
// and update the logger when the value is set.
109-
l := baseLogger.WithFields(logrus.Fields{
96+
f := logrus.Fields{
11097
logfields.LogSubsys: subsystem,
11198
logfields.EndpointID: e.ID,
11299
logfields.ContainerID: e.getShortContainerIDLocked(),
@@ -115,13 +102,23 @@ func (e *Endpoint) UpdateLogger(fields map[string]interface{}) {
115102
logfields.IPv4: e.GetIPv4Address(),
116103
logfields.IPv6: e.GetIPv6Address(),
117104
logfields.K8sPodName: e.GetK8sNamespaceAndPodName(),
118-
})
105+
}
119106

120107
if e.SecurityIdentity != nil {
121-
l = l.WithField(logfields.Identity, e.SecurityIdentity.ID.StringID())
108+
f[logfields.Identity] = e.SecurityIdentity.ID.StringID()
109+
}
110+
111+
// Inherit properties from default logger.
112+
baseLogger := logging.DefaultLogger.WithFields(f)
113+
114+
// If this endpoint is set to debug ensure it will print debug by giving it
115+
// an independent logger.
116+
// If this endpoint is not set to debug, it will use the log level set by the user.
117+
if e.Options != nil && e.Options.IsEnabled(option.Debug) {
118+
baseLogger.Logger.SetLevel(logrus.DebugLevel)
122119
}
123120

124-
e.logger.Store(l)
121+
e.logger.Store(baseLogger)
125122
}
126123

127124
// Only to be called from UpdateLogger() above

pkg/endpoint/log_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,33 @@ import (
1111
. "github.com/cilium/checkmate"
1212
"github.com/sirupsen/logrus"
1313

14+
"github.com/cilium/cilium/pkg/logging"
1415
"github.com/cilium/cilium/pkg/option"
1516
"github.com/cilium/cilium/pkg/policy"
1617
testidentity "github.com/cilium/cilium/pkg/testutils/identity"
1718
testipcache "github.com/cilium/cilium/pkg/testutils/ipcache"
1819
)
1920

21+
func (s *EndpointSuite) TestEndpointLogFormat(c *C) {
22+
// Default log format is text
23+
do := &DummyOwner{repo: policy.NewPolicyRepository(nil, nil, nil, nil)}
24+
ep := NewEndpointWithState(do, do, testipcache.NewMockIPCache(), nil, testidentity.NewMockIdentityAllocator(nil), 12345, StateReady)
25+
26+
_, ok := ep.getLogger().Logger.Formatter.(*logrus.TextFormatter)
27+
c.Assert(ok, Equals, true)
28+
29+
// Log format is JSON when configured
30+
logging.SetLogFormat(logging.LogFormatJSON)
31+
defer func() {
32+
logging.SetLogFormat(logging.LogFormatText)
33+
}()
34+
do = &DummyOwner{repo: policy.NewPolicyRepository(nil, nil, nil, nil)}
35+
ep = NewEndpointWithState(do, do, testipcache.NewMockIPCache(), nil, testidentity.NewMockIdentityAllocator(nil), 12345, StateReady)
36+
37+
_, ok = ep.getLogger().Logger.Formatter.(*logrus.JSONFormatter)
38+
c.Assert(ok, Equals, true)
39+
}
40+
2041
func (s *EndpointSuite) TestPolicyLog(c *C) {
2142
do := &DummyOwner{repo: policy.NewPolicyRepository(nil, nil, nil, nil)}
2243
ep := NewEndpointWithState(do, do, testipcache.NewMockIPCache(), nil, testidentity.NewMockIdentityAllocator(nil), 12345, StateReady)

pkg/logging/logging.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const (
4141

4242
// DefaultLogger is the base logrus logger. It is different from the logrus
4343
// default to avoid external dependencies from writing out unexpectedly
44-
var DefaultLogger = InitializeDefaultLogger()
44+
var DefaultLogger = initializeDefaultLogger()
4545

4646
func initializeKLog() {
4747
log := DefaultLogger.WithField(logfields.LogSubsys, "klog")
@@ -73,8 +73,8 @@ func initializeKLog() {
7373
// LogOptions maps configuration key-value pairs related to logging.
7474
type LogOptions map[string]string
7575

76-
// InitializeDefaultLogger returns a logrus Logger with a custom text formatter.
77-
func InitializeDefaultLogger() (logger *logrus.Logger) {
76+
// initializeDefaultLogger returns a logrus Logger with a custom text formatter.
77+
func initializeDefaultLogger() (logger *logrus.Logger) {
7878
logger = logrus.New()
7979
logger.SetFormatter(GetFormatter(DefaultLogFormat))
8080
logger.SetLevel(DefaultLogLevel)

0 commit comments

Comments
 (0)