fleet/server/contexts/logging/logging_test.go
Victor Lyuboslavsky de55ecf778
Migrate HTTP request logging from go-kit/log to slog (#39729)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #38889 

# Checklist for submitter

- [x] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Refactor**
* Updated internal logging infrastructure to improve standardization and
maintainability. Logging functionality remains unchanged from an
end-user perspective.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-14 13:04:41 -06:00

45 lines
1.2 KiB
Go

package logging
import (
"context"
"errors"
"fmt"
"log/slog"
"testing"
"github.com/fleetdm/fleet/v4/server/platform/logging/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoggingErrs(t *testing.T) {
setupTest := func() (*testutils.TestHandler, *slog.Logger, *LoggingContext, context.Context) {
handler := testutils.NewTestHandler()
logger := slog.New(handler)
lc := &LoggingContext{}
ctx := NewContext(context.Background(), lc)
return handler, logger, lc, ctx
}
t.Run("one error", func(t *testing.T) {
handler, logger, lc, ctx := setupTest()
WithErr(ctx, fmt.Errorf("BLAH: %w", errors.New("AAAA")))
lc.Log(ctx, logger)
records := handler.Records()
require.Len(t, records, 1)
attrs := testutils.RecordAttrs(&records[0])
assert.Equal(t, "BLAH: AAAA", attrs["err"])
})
t.Run("two errors", func(t *testing.T) {
handler, logger, lc, ctx := setupTest()
WithErr(ctx, fmt.Errorf("BLAH: %w", errors.New("AAAA")))
WithErr(ctx, fmt.Errorf("FOO: %w", errors.New("BBBB")))
lc.Log(ctx, logger)
records := handler.Records()
require.Len(t, records, 1)
attrs := testutils.RecordAttrs(&records[0])
assert.Equal(t, "BLAH: AAAA || FOO: BBBB", attrs["err"])
})
}