Skip to content

Commit 5d2a545

Browse files
set default log level when not set in policy (#5452) (#5474)
* set default log level when not set in policy * added changelog * fix lint * update log message * fix tests * move default logic to SetLogLevel * Update changelog/fragments/1725620525-fix-default-log-level-policy-change.yaml Co-authored-by: Paolo Chilà <[email protected]> * updated changelog --------- Co-authored-by: Paolo Chilà <[email protected]> (cherry picked from commit 3b911c7) Co-authored-by: Julia Bardi <[email protected]>
1 parent 5df6fcb commit 5d2a545

File tree

4 files changed

+50
-9
lines changed

4 files changed

+50
-9
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: bug-fix
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Set default log level on policy change, when policy does not have a log level
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
#description:
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component: elastic-agent
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
pr: https://github.com/elastic/elastic-agent/pull/5452
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
issue: https://github.com/elastic/elastic-agent/issues/5451

internal/pkg/agent/application/actions/handlers/handler_action_policy_change.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,11 @@ func (h *PolicyChangeHandler) handlePolicyChange(ctx context.Context, c *config.
342342
}
343343

344344
// hasEventLoggingOutputChanged returns true if the output of the event logger has changed
345-
func (p *PolicyChangeHandler) hasEventLoggingOutputChanged(new *configuration.Configuration) bool {
345+
func (h *PolicyChangeHandler) hasEventLoggingOutputChanged(new *configuration.Configuration) bool {
346346
switch {
347-
case p.config.Settings.EventLoggingConfig.ToFiles != new.Settings.EventLoggingConfig.ToFiles:
347+
case h.config.Settings.EventLoggingConfig.ToFiles != new.Settings.EventLoggingConfig.ToFiles:
348348
return true
349-
case p.config.Settings.EventLoggingConfig.ToStderr != new.Settings.EventLoggingConfig.ToStderr:
349+
case h.config.Settings.EventLoggingConfig.ToStderr != new.Settings.EventLoggingConfig.ToStderr:
350350
return true
351351
default:
352352
return false

internal/pkg/agent/application/actions/handlers/handler_action_settings.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,19 @@ func (h *Settings) SetLogLevel(ctx context.Context, lvl *logp.Level) error {
103103
h.fallbackLogLevel = lvl
104104
rawLogLevel := h.agentInfo.RawLogLevel()
105105
h.log.Debugf("received fallback loglevel %s, raw loglevel %s", lvl, rawLogLevel)
106-
if rawLogLevel == "" && lvl != nil {
107-
h.log.Debugf("setting log level %s", lvl)
108-
// set the runtime log level only if we don't have one set for the specific agent
109-
return h.logLevelSetter.SetLogLevel(ctx, lvl)
106+
if rawLogLevel == "" {
107+
// There's no log level set for this specific agent
108+
if lvl != nil {
109+
// log level set on the policy
110+
h.log.Debugf("setting log level %s", lvl)
111+
// set the runtime log level only if we don't have one set for the specific agent
112+
return h.logLevelSetter.SetLogLevel(ctx, lvl)
113+
} else {
114+
// Both the policy log level and the agent-specific log level are not set: set the default agent level
115+
h.log.Debugf("setting log level to default")
116+
defaultLogLevel := logger.DefaultLogLevel
117+
return h.logLevelSetter.SetLogLevel(ctx, &defaultLogLevel)
118+
}
110119
}
111120
return nil
112121
}

internal/pkg/agent/application/actions/handlers/handler_action_settings_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestSettings_SetLogLevel(t *testing.T) {
2525

2626
// test log level we use in testcases
2727
testWarnLevel := logp.WarnLevel
28+
defaultLogLevel := logger.DefaultLogLevel
2829

2930
type fields struct {
3031
fallbackLogLevel *logp.Level
@@ -61,8 +62,7 @@ func TestSettings_SetLogLevel(t *testing.T) {
6162
},
6263
setupMocks: func(t *testing.T, setter *mockhandlers.LogLevelSetter, agent *mockinfo.Agent) {
6364
agent.EXPECT().RawLogLevel().Return("").Once()
64-
// we should never call the SetLogLevel with nil, for simplicity remove the expectation altogether
65-
// setter.EXPECT().SetLogLevel(mock.Anything, nil).Return(nil).Times(0)
65+
setter.EXPECT().SetLogLevel(mock.Anything, &defaultLogLevel).Return(nil).Once()
6666
},
6767
wantErr: assert.NoError,
6868
wantFallbackLogLevel: nil,

0 commit comments

Comments
 (0)