Skip to content

audit: export LevelAudit slog level constant#141

Merged
rdimitrov merged 1 commit into
mainfrom
add-audit-level-constant
Jun 23, 2026
Merged

audit: export LevelAudit slog level constant#141
rdimitrov merged 1 commit into
mainfrom
add-audit-level-constant

Conversation

@rdimitrov

Copy link
Copy Markdown
Member

Summary

Adds an exported LevelAudit = slog.Level(2) constant to the audit package so all ToolHive components share a single definition of the custom audit log level instead of each hardcoding the numeric value.

Today both the toolhive operator (pkg/audit/auditor.go) and the registry server (internal/audit/logger.go) define slog.Level(2) independently. Exporting it here lets them converge on one source of truth.

The level sits between slog.LevelInfo (0) and slog.LevelWarn (4) so audit events can be filtered independently from regular application logs.

Changes

  • audit/event.go: export const LevelAudit = slog.Level(2) with doc comment
  • audit/event_test.go: add a TestConstants subtest locking the value and its ordering (Info < LevelAudit < Warn), since changing it would break consumers that filter on the numeric level; use the constant in TestAuditEventLogTo

Context

Enables stacklok/toolhive-registry-server#826 — the registry server will import audit.LevelAudit rather than redefining it locally, and render the level as "AUDIT" to match the operator.

🤖 Generated with Claude Code

Add a LevelAudit constant (slog.Level(2)) to the audit package so all
ToolHive components share one definition of the custom audit log level
rather than each hardcoding the numeric value. Both the toolhive
operator (pkg/audit) and the registry server (internal/audit) currently
define slog.Level(2) independently; exporting it here lets them converge
on a single source of truth.

The value sits between slog.LevelInfo and slog.LevelWarn so audit events
can be filtered independently from regular application logs. A test locks
the value and its ordering since changing it would break consumers that
filter on the numeric level.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@rdimitrov rdimitrov merged commit 969b359 into main Jun 23, 2026
5 checks passed
@rdimitrov rdimitrov deleted the add-audit-level-constant branch June 23, 2026 21:47
rdimitrov added a commit to stacklok/toolhive-registry-server that referenced this pull request Jun 23, 2026
## Summary

The audit logger emits events at a custom `slog` level between Info and
Warn, which serialized in JSON output as `"level":"INFO+2"`. That makes
audit entries awkward to filter in log aggregators (Loki, Elasticsearch,
Splunk) that expect named level strings, and it diverges from the
toolhive operator, which already renders the same level as `"AUDIT"`.

Operators running both toolhive and the registry server otherwise see
inconsistent `level` values for what is logically the same event level,
requiring two filter patterns.

## Changes

- `internal/audit/logger.go`: add a `ReplaceAttr` hook to `NewLogger`
that rewrites the custom audit level to the string `"AUDIT"` (matching
the operator). Drop the locally defined `auditLevel` var.
- `internal/audit/middleware.go`: use `audit.LevelAudit` from
`toolhive-core` instead of the local level, so all ToolHive components
share one definition rather than each hardcoding `slog.Level(2)`.
- `internal/audit/middleware_test.go`: add
`TestLogger_RendersAuditLevelAsString` asserting the rendered `level`
field is `"AUDIT"`.
- `go.mod`: bump `toolhive-core` to `v0.0.26`, which exports the new
`audit.LevelAudit` constant (stacklok/toolhive-core#141).

## Testing

- `go test ./internal/audit/...` ✅
- `golangci-lint run ./internal/audit/...` ✅

Fixes #826

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants