Skip to content

Commit

Permalink
✨ feat: add new method WithValue, WithExtra for quick create log Record
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 29, 2023
1 parent bef642d commit 5047b62
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
18 changes: 18 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,17 @@ func (l *Logger) Record() *Record {
}

// WithField new record with field
//
// TIP: add field need config Formatter template fields.
func (l *Logger) WithField(name string, value any) *Record {
r := l.newRecord()
defer l.releaseRecord(r)
return r.WithField(name, value)
}

// WithFields new record with fields
//
// TIP: add field need config Formatter template fields.
func (l *Logger) WithFields(fields M) *Record {
r := l.newRecord()
defer l.releaseRecord(r)
Expand All @@ -419,6 +423,20 @@ func (l *Logger) WithData(data M) *Record {
return r.WithData(data)
}

// WithValue new record with data value
func (l *Logger) WithValue(key string, value any) *Record {
r := l.newRecord()
defer l.releaseRecord(r)
return r.AddValue(key, value)
}

// WithExtra new record with extra data
func (l *Logger) WithExtra(ext M) *Record {
r := l.newRecord()
defer l.releaseRecord(r)
return r.SetExtra(ext)
}

// WithTime new record with time.Time
func (l *Logger) WithTime(t time.Time) *Record {
r := l.newRecord()
Expand Down
4 changes: 4 additions & 0 deletions record.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,15 @@ func (r *Record) WithData(data M) *Record {
}

// WithField with a new field to record
//
// Note: add field need config Formatter template fields.
func (r *Record) WithField(name string, val any) *Record {
return r.WithFields(M{name: val})
}

// WithFields with new fields to record
//
// Note: add field need config Formatter template fields.
func (r *Record) WithFields(fields M) *Record {
nr := r.Copy()
if nr.Fields == nil {
Expand Down
16 changes: 15 additions & 1 deletion slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,31 @@ func AddProcessors(ps ...Processor) { std.AddProcessors(ps...) }

// -------------------------- New record with log data, fields -----------------------------

// WithExtra new record with extra data
func WithExtra(ext M) *Record {
return std.WithExtra(ext)
}

// WithData new record with data
func WithData(data M) *Record {
return std.WithData(data)
}

// WithField new record with field
// WithValue new record with data value
func WithValue(key string, value any) *Record {
return std.WithValue(key, value)
}

// WithField new record with field.
//
// TIP: add field need config Formatter template fields.
func WithField(name string, value any) *Record {
return std.WithField(name, value)
}

// WithFields new record with fields
//
// TIP: add field need config Formatter template fields.
func WithFields(fields M) *Record {
return std.WithFields(fields)
}
Expand Down
14 changes: 14 additions & 0 deletions slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,20 @@ func TestAddHandler(t *testing.T) {
slog.Infof("info %s", "message")
}

func TestWithExtra(t *testing.T) {
defer slog.Reset()

th := newTestHandler()
slog.AddHandler(th)

slog.WithExtra(slog.M{"ext1": "val1"}).
AddValue("key1", "val2").
Info("info message")
s := th.ResetGet()
assert.StrContains(t, s, `"ext1":"val1"`)
assert.StrContains(t, s, `{key1:val2}`)
}

func TestAddProcessor(t *testing.T) {
defer slog.Reset()

Expand Down

0 comments on commit 5047b62

Please sign in to comment.