Add ability to enable/disable logs by topic (#40126)

<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #40124 

# Details

Implements the proposal in
https://docs.google.com/document/d/16qe6oVLKK25nA9GEIPR9Gw_IJ342_wlJRdnWEMmWdas/edit?tab=t.0#heading=h.nlw4agv1xs3g

Allows doing e.g.
```go
logger.WarnContext(logCtx, "The `team_id` param is deprecated, use `fleet_id` instead", "log_topic", "deprecated-field-names")
```
or
```go
if logging.TopicEnabled("deprecated-api-params") {
  logging.WithLevel(ctx, slog.LevelWarn)
  logging.WithExtras(
    ctx, 
    "deprecated_param", 
    queryTagValue,
    "deprecation_warning", 
    fmt.Sprintf("'%s' is deprecated, use '%s'", queryTagValue, renameTo),
  )
}
```

Topics can be disabled at the app level, and enabled/disabled at the
command-line level.

# Checklist for submitter

If some of the following don't apply, delete the relevant line.

- [X] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.

## Testing

- [X] Added/updated automated tests
- [X] QA'd all new/changed functionality manually
No logs have this in prod yet, but I added some manually in a branch and
verified that I could enable/disable them via CLI options and env vars,
including enabling topics that were disabled on the server. Tested for
both server and `fleetctl gitops`.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

## Release Notes

* **New Features**
* Added per-topic logging control to enable or disable logging for
specific topics via configuration and CLI flags.
* Added context-aware logging methods (ErrorContext, WarnContext,
InfoContext, DebugContext) to support contextual logging.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Scott Gress
2026-02-20 17:22:50 -06:00
committed by GitHub
parent f147a1a607
commit 421dc67e0c
15 changed files with 479 additions and 16 deletions
+18
View File
@@ -32,6 +32,7 @@ import (
"github.com/fleetdm/fleet/v4/ee/server/service/hostidentity/httpsig"
"github.com/fleetdm/fleet/v4/pkg/fleethttp"
"github.com/fleetdm/fleet/v4/pkg/scripts"
"github.com/fleetdm/fleet/v4/pkg/str"
"github.com/fleetdm/fleet/v4/server"
"github.com/fleetdm/fleet/v4/server/acl/activityacl"
activity_api "github.com/fleetdm/fleet/v4/server/activity/api"
@@ -67,6 +68,7 @@ import (
nanomdm_pushsvc "github.com/fleetdm/fleet/v4/server/mdm/nanomdm/push/service"
"github.com/fleetdm/fleet/v4/server/platform/endpointer"
platform_http "github.com/fleetdm/fleet/v4/server/platform/http"
platform_logging "github.com/fleetdm/fleet/v4/server/platform/logging"
common_mysql "github.com/fleetdm/fleet/v4/server/platform/mysql"
"github.com/fleetdm/fleet/v4/server/pubsub"
"github.com/fleetdm/fleet/v4/server/service"
@@ -234,6 +236,22 @@ the way that the Fleet server works.
logger := initLogger(config, loggerProvider)
// If you want to disable any logs by default, this is where to do it.
//
// For example:
// platform_logging.DisableTopic("deprecated-api-keys")
// Apply log topic overrides from config. Enables run first, then
// disables, so disable wins on conflict.
// Note that any topic not included in these lists will be considered
// enabled if it's encountered in a log.
for _, topic := range str.SplitAndTrim(config.Logging.EnableLogTopics, ",", true) {
platform_logging.EnableTopic(topic)
}
for _, topic := range str.SplitAndTrim(config.Logging.DisableLogTopics, ",", true) {
platform_logging.DisableTopic(topic)
}
if dev_mode.IsEnabled {
createTestBuckets(&config, logger)
}
+42 -1
View File
@@ -1,12 +1,18 @@
package fleetctl
import "github.com/urfave/cli/v2"
import (
"github.com/fleetdm/fleet/v4/pkg/str"
"github.com/fleetdm/fleet/v4/server/platform/logging"
"github.com/urfave/cli/v2"
)
const (
outfileFlagName = "outfile"
debugFlagName = "debug"
fleetCertificateFlagName = "fleet-certificate"
stdoutFlagName = "stdout"
enableLogTopicsFlagName = "enable-log-topics"
disableLogTopicsFlagName = "disable-log-topics"
)
func outfileFlag() cli.Flag {
@@ -58,6 +64,41 @@ func getStdout(c *cli.Context) bool {
return c.Bool(stdoutFlagName)
}
func enableLogTopicsFlag() cli.Flag {
return &cli.StringFlag{
Name: enableLogTopicsFlagName,
EnvVars: []string{"FLEET_ENABLE_LOG_TOPICS"},
Usage: "Comma-separated log topics to enable",
}
}
func getEnabledLogTopics(c *cli.Context) string {
return c.String(enableLogTopicsFlagName)
}
func disableLogTopicsFlag() cli.Flag {
return &cli.StringFlag{
Name: disableLogTopicsFlagName,
EnvVars: []string{"FLEET_DISABLE_LOG_TOPICS"},
Usage: "Comma-separated log topics to disable",
}
}
func getDisabledLogTopics(c *cli.Context) string {
return c.String(disableLogTopicsFlagName)
}
// applyLogTopicFlags parses the enable/disable log topic flags and applies them.
// Enables run first, then disables, so disable wins on conflict.
func applyLogTopicFlags(c *cli.Context) {
for _, topic := range str.SplitAndTrim(getEnabledLogTopics(c), ",", true) {
logging.EnableTopic(topic)
}
for _, topic := range str.SplitAndTrim(getDisabledLogTopics(c), ",", true) {
logging.DisableTopic(topic)
}
}
func byHostIdentifier() cli.Flag {
return &cli.StringFlag{
Name: "host",
+5
View File
@@ -76,12 +76,17 @@ func gitopsCommand() *cli.Command {
configFlag(),
contextFlag(),
debugFlag(),
enableLogTopicsFlag(),
disableLogTopicsFlag(),
},
Action: func(c *cli.Context) error {
logf := func(format string, a ...interface{}) {
_, _ = fmt.Fprintf(c.App.Writer, format, a...)
}
// Apply log topic overrides from CLI flags.
applyLogTopicFlags(c)
if len(c.Args().Slice()) != 0 {
return errors.New("No positional arguments are allowed. To load multiple config files, use one -f flag per file.")
}