Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slog: expose io.Writer #694

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions promslog/slog.go

This comment was marked as spam.

Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ func (f *AllowedFormat) Set(s string) error {

// Config is a struct containing configurable settings for the logger
type Config struct {
Level *AllowedLevel
Format *AllowedFormat
Style LogStyle
ioWriter io.Writer
Level *AllowedLevel
Format *AllowedFormat
Style LogStyle
Writer io.Writer
}

// New returns a new slog.Logger. Each logged line will be annotated
Expand All @@ -162,8 +162,8 @@ func New(config *Config) *slog.Logger {
_ = config.Level.Set("info")
}

if config.ioWriter == nil {
config.ioWriter = defaultWriter
if config.Writer == nil {
config.Writer = defaultWriter
}

logHandlerOpts := &slog.HandlerOptions{
Expand All @@ -176,7 +176,7 @@ func New(config *Config) *slog.Logger {
}

if config.Format != nil && config.Format.s == "json" {
return slog.New(slog.NewJSONHandler(config.ioWriter, logHandlerOpts))
return slog.New(slog.NewJSONHandler(config.Writer, logHandlerOpts))
}
return slog.New(slog.NewTextHandler(config.ioWriter, logHandlerOpts))
return slog.New(slog.NewTextHandler(config.Writer, logHandlerOpts))
}
2 changes: 1 addition & 1 deletion promslog/slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestDynamicLevels(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
buf.Reset() // Ensure buf is reset prior to tests
config := &Config{ioWriter: &buf, Style: tc.logStyle}
config := &Config{Writer: &buf, Style: tc.logStyle}
logger := New(config)

// Test that log level can be adjusted on-the-fly to debug and that a
Expand Down