Skip to content

Commit

Permalink
add NetIPAddrs to logger method
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Oct 26, 2024
1 parent 27c11d1 commit 364d959
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,27 @@ func (e *Entry) NetIPAddr(key string, ip netip.Addr) *Entry {
return e
}

// NetIPAddrs adds IPv4 or IPv6 Addresses to the entry.
func (e *Entry) NetIPAddrs(key string, ips []netip.Addr) *Entry {
if e == nil {
return nil
}

e.buf = append(e.buf, ',', '"')
e.buf = append(e.buf, key...)
e.buf = append(e.buf, '"', ':', '[')
for i, ip := range ips {
if i > 0 {
e.buf = append(e.buf, ',')
}
e.buf = append(e.buf, '"')
e.buf = ip.AppendTo(e.buf)
e.buf = append(e.buf, '"')
}
e.buf = append(e.buf, ']')
return e
}

// NetIPAddrPort adds IPv4 or IPv6 with Port Address to the entry.
func (e *Entry) NetIPAddrPort(key string, ipPort netip.AddrPort) *Entry {
if e == nil {
Expand Down
2 changes: 2 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"net"
"net/netip"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -107,6 +108,7 @@ func TestLoggerInfo(t *testing.T) {
IPAddr("ip6", net.ParseIP("2001:4860:4860::8888")).
IPAddr("ip4", ipv4Addr).
IPPrefix("ip_prefix", *ipv4Net).
NetIPAddrs("netip_addr", []netip.Addr{netip.MustParseAddr("1.1.1.1"), netip.MustParseAddr("2001:4860:4860::8888")}).
MACAddr("mac", net.HardwareAddr{0x00, 0x00, 0x5e, 0x00, 0x53, 0x01}).
Xid("xid", NewXID()).
Errs("errors", []error{errors.New("error1"), nil, errors.New("error3")}).
Expand Down

0 comments on commit 364d959

Please sign in to comment.