diff --git a/cmd/anubis/main.go b/cmd/anubis/main.go index fad6c9dd..60fb149f 100644 --- a/cmd/anubis/main.go +++ b/cmd/anubis/main.go @@ -276,7 +276,7 @@ func main() { internal.SetHealth("anubis", healthv1.HealthCheckResponse_NOT_SERVING) - lg := internal.InitSlog(*slogLevel, os.Stderr) + lg := internal.InitSlog(*slogLevel, os.Stderr, false) lg.Info("starting up Anubis") if *healthcheck { diff --git a/cmd/containerbuild/main.go b/cmd/containerbuild/main.go index 3eb52ffd..258081e6 100644 --- a/cmd/containerbuild/main.go +++ b/cmd/containerbuild/main.go @@ -28,7 +28,7 @@ func main() { flagenv.Parse() flag.Parse() - slog.SetDefault(internal.InitSlog(*slogLevel, os.Stderr)) + slog.SetDefault(internal.InitSlog(*slogLevel, os.Stderr, false)) koDockerRepo := strings.TrimSuffix(*dockerRepo, "/"+filepath.Base(*dockerRepo)) @@ -159,8 +159,8 @@ func run(command string) (string, error) { } func setOutput(key, val string) { - github_output := os.Getenv("GITHUB_OUTPUT") - f, _ := os.OpenFile(github_output, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) - fmt.Fprintf(f, "%s=%s\n", key, val) - f.Close() + github_output := os.Getenv("GITHUB_OUTPUT") + f, _ := os.OpenFile(github_output, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + fmt.Fprintf(f, "%s=%s\n", key, val) + f.Close() } diff --git a/docs/docs/CHANGELOG.md b/docs/docs/CHANGELOG.md index b961722a..7e959fd4 100644 --- a/docs/docs/CHANGELOG.md +++ b/docs/docs/CHANGELOG.md @@ -17,8 +17,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fixed mixed tab/space indentation in Caddy documentation code block - Improve error messages and fix broken REDIRECT_DOMAINS link in docs ([#1193](https://github.com/TecharoHQ/anubis/issues/1193)) - Add Bulgarian locale ([#1394](https://github.com/TecharoHQ/anubis/pull/1394)) +- Add option to hide source code origin of log lines in [logging configuration](./admin/policies.mdx#logging-management) + - Fix CEL internal errors when iterating `headers`/`query` map wrappers by implementing map iterators for `HTTPHeaders` and `URLValues` ([#1465](https://github.com/TecharoHQ/anubis/pull/1465)). ## v1.25.0: Necron diff --git a/docs/docs/admin/policies.mdx b/docs/docs/admin/policies.mdx index 24317a41..b3822e82 100644 --- a/docs/docs/admin/policies.mdx +++ b/docs/docs/admin/policies.mdx @@ -339,6 +339,7 @@ Anubis exposes the following logging settings in the policy file: | `level` | [log level](#log-levels) | `info` | The logging level threshold. Any logs that are at or above this threshold will be drained to the sink. Any other logs will be discarded. | | `sink` | string | `stdio`, `file` | The sink where the logs drain to as they are being recorded in Anubis. | | `parameters` | object | | Parameters for the given logging sink. This will vary based on the logging sink of choice. See below for more information. | +| `hideSource` | boolean | `false` | If set, hide the details of which line of code triggered a log line. Setting this to `true` may make getting support more difficult. | Anubis supports the following logging sinks: diff --git a/internal/log.go b/internal/log.go index 4b8bd02f..0c27b0f5 100644 --- a/internal/log.go +++ b/internal/log.go @@ -10,7 +10,7 @@ import ( "strings" ) -func InitSlog(level string, sink io.Writer) *slog.Logger { +func InitSlog(level string, sink io.Writer, hideSource bool) *slog.Logger { var programLevel slog.Level if err := (&programLevel).UnmarshalText([]byte(level)); err != nil { fmt.Fprintf(os.Stderr, "invalid log level %s: %v, using info\n", level, err) @@ -20,10 +20,16 @@ func InitSlog(level string, sink io.Writer) *slog.Logger { leveler := &slog.LevelVar{} leveler.Set(programLevel) - h := slog.NewJSONHandler(sink, &slog.HandlerOptions{ + opts := &slog.HandlerOptions{ AddSource: true, Level: leveler, - }) + } + + if hideSource { + opts.AddSource = false + } + + h := slog.NewJSONHandler(sink, opts) result := slog.New(h) return result } diff --git a/lib/config/logging.go b/lib/config/logging.go index f82cac4f..5f419c79 100644 --- a/lib/config/logging.go +++ b/lib/config/logging.go @@ -17,6 +17,7 @@ type Logging struct { Sink string `json:"sink"` // Logging sink, either "stdio" or "file" Level *slog.Level `json:"level"` // Log level, if set supersedes the level in flags Parameters *LoggingFileConfig `json:"parameters"` // Logging parameters, to be dynamic in the future + HideSource bool `json:"hideSource"` // If true, hide the `source` field in log lines } const ( diff --git a/lib/policy/policy.go b/lib/policy/policy.go index 5bd4958d..779ca3c4 100644 --- a/lib/policy/policy.go +++ b/lib/policy/policy.go @@ -75,7 +75,7 @@ func ParseConfig(ctx context.Context, fin io.Reader, fname string, defaultDiffic switch c.Logging.Sink { case config.LogSinkStdio: - result.Logger = internal.InitSlog(logLevel, os.Stderr) + result.Logger = internal.InitSlog(logLevel, os.Stderr, c.Logging.HideSource) case config.LogSinkFile: out := &logrotate.Logger{ Filename: c.Logging.Parameters.Filename, @@ -87,7 +87,7 @@ func ParseConfig(ctx context.Context, fin io.Reader, fname string, defaultDiffic Compress: c.Logging.Parameters.Compress, } - result.Logger = internal.InitSlog(logLevel, out) + result.Logger = internal.InitSlog(logLevel, out, c.Logging.HideSource) } lg := result.Logger.With("at", "config-validate")