Skip to content

Commit

Permalink
Merge pull request #694 from jkroepke/slog/writer
Browse files Browse the repository at this point in the history
slog: expose io.Writer
  • Loading branch information
ArthurSens authored Sep 14, 2024
2 parents fa21dfd + 5f9af24 commit 40d6251
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 8 additions & 8 deletions promslog/slog.go
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

0 comments on commit 40d6251

Please sign in to comment.