Skip to content

Commit

Permalink
add Encode function
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Apr 23, 2024
1 parent aa4b8cf commit bfeacdb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,21 @@ func (e *Entry) Hex(key string, val []byte) *Entry {
return e
}

// Encode encodes bytes using enc.AppendEncode to the entry.
func (e *Entry) Encode(key string, val []byte, enc interface {
AppendEncode(dst, src []byte) []byte
}) *Entry {
if e == nil {
return nil
}
e.buf = append(e.buf, ',', '"')
e.buf = append(e.buf, key...)
e.buf = append(e.buf, '"', ':', '"')
e.buf = enc.AppendEncode(e.buf, val)
e.buf = append(e.buf, '"')
return e
}

// Xid adds the field key with xid.ID as a base32 string to the entry.
func (e *Entry) Xid(key string, xid [12]byte) *Entry {
if e == nil {
Expand Down
9 changes: 9 additions & 0 deletions tsv.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,15 @@ func (e *TSVEntry) NetIPPrefix(pfx netip.Prefix) *TSVEntry {
return e
}

// Encode encodes bytes using enc.AppendEncode to the entry.
func (e *TSVEntry) Encode(key string, val []byte, enc interface {
AppendEncode(dst, src []byte) []byte
}) *TSVEntry {
e.buf = enc.AppendEncode(e.buf, val)
e.buf = append(e.buf, e.sep)
return e
}

// Msg sends the entry.
func (e *TSVEntry) Msg() {
if len(e.buf) != 0 {
Expand Down

0 comments on commit bfeacdb

Please sign in to comment.