Skip to content

Commit

Permalink
feat: 添加 Recorder.LocaleString
Browse files Browse the repository at this point in the history
  • Loading branch information
caixw committed Nov 14, 2023
1 parent c9b8b44 commit 189bf44
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -3,7 +3,7 @@ module github.com/issue9/logs/v7
require (
github.com/issue9/assert/v3 v3.1.0
github.com/issue9/errwrap v0.3.1
github.com/issue9/localeutil v0.23.0
github.com/issue9/localeutil v0.24.0
github.com/issue9/sliceutil v0.15.0
github.com/issue9/term/v3 v3.2.4
golang.org/x/text v0.14.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -2,8 +2,8 @@ github.com/issue9/assert/v3 v3.1.0 h1:oxLFXS7QnBKI4lB31pRoYO96yErkWAJtR7iv+LNjAP
github.com/issue9/assert/v3 v3.1.0/go.mod h1:yft/uaskRpwQTyBT3n1zRl91SR1wNlO4fLZHzOa4bdM=
github.com/issue9/errwrap v0.3.1 h1:8g4lYJaGnoiXyZ1oZyH/7zPDGgw5RNiE9Q6ri9kE6Z8=
github.com/issue9/errwrap v0.3.1/go.mod h1:HLR0e5iimd2aJXM9YrThOsRj3/6lMtk77lVp7zyvJ4E=
github.com/issue9/localeutil v0.23.0 h1:UMxzF8wN42f6Rw6lkcbVIlSd/Zm9leH0syjDogXvYyg=
github.com/issue9/localeutil v0.23.0/go.mod h1:yRWCkWMfZY8lCSyT1usICiXsNkhW3cfQlE8kHAmc614=
github.com/issue9/localeutil v0.24.0 h1:EnqsgkhWXqQXU+RM9ISAYnBaxxZYi3ec+6Z+AlxGga4=
github.com/issue9/localeutil v0.24.0/go.mod h1:JvTb8B/2oVEZU1VHrBJXPHlE/1gZJFRMcF/ziKD8JJY=
github.com/issue9/sliceutil v0.15.0 h1:E6Xnl3FY5h0ZGNzyx1VEFAfGdParaq/BkX1QQR0uFwI=
github.com/issue9/sliceutil v0.15.0/go.mod h1:n9meV7AamDhmehOBuV4GrxW3yw7O1cZmLx3Xizg1bps=
github.com/issue9/term/v3 v3.2.4 h1:bCY0uk8lLaY6XLbZdYQVl5e0CqJkGxV8EYJxJGFv8xU=
Expand Down
7 changes: 7 additions & 0 deletions logger.go
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"

"github.com/issue9/localeutil"
"github.com/issue9/logs/v7/writers"
)

Expand Down Expand Up @@ -55,6 +56,12 @@ func (l *Logger) String(s string) {
}
}

func (l *Logger) LocaleString(s localeutil.Stringer) {
if l.IsEnable() {
l.logs.NewRecord().DepthLocaleString(3, s).Output(l)
}
}

func (l *Logger) Print(v ...any) {
if l.IsEnable() {
l.logs.NewRecord().DepthPrint(3, v...).Output(l)
Expand Down
33 changes: 27 additions & 6 deletions record.go
Expand Up @@ -39,6 +39,14 @@ type (
// NOTE: 此操作之后,当前对象不再可用!
Error(err error)

// LocaleString 输出一条本地化的信息
//
// 这是 Print 的特化版本,在已知类型为 [localeutil.Stringer] 时,
// 采用此方法会比 Print(s) 有更好的性能。
//
// NOTE: 此操作之后,当前对象不再可用!
LocaleString(localeutil.Stringer)

// String 将字符串作为一条日志输出
//
// 这是 Print 的特化版本,在已知类型为字符串时,
Expand Down Expand Up @@ -135,11 +143,7 @@ func (e *Record) initLocationCreated(depth int) *Record {
func (e *Record) with(name string, val any) *Record {
switch v := val.(type) {
case localeutil.Stringer:
if e.logs.printer != nil {
e.Attrs = append(e.Attrs, Attr{K: name, V: v.LocaleString(e.logs.printer)})
} else {
e.Attrs = append(e.Attrs, Attr{K: name, V: v})
}
e.Attrs = append(e.Attrs, Attr{K: name, V: v.LocaleString(e.logs.printer)})
case Marshaler:
e.Attrs = append(e.Attrs, Attr{K: name, V: v.MarshalLog()})
default:
Expand All @@ -165,7 +169,7 @@ func (e *Record) DepthError(depth int, err error) *Record {
if pp := e.logs.printer; pp != nil {
e.AppendMessage = func(b *Buffer) { b.AppendString(ee.LocaleString(pp)) }
} else { // e2 必然是实现了 error 接口的
e.AppendMessage = func(b *Buffer) { b.AppendString(ee.(error).Error()) }
e.AppendMessage = func(b *Buffer) { b.AppendString(err.Error()) }
}
default:
e.AppendMessage = func(b *Buffer) { b.AppendString(err.Error()) }
Expand All @@ -174,6 +178,16 @@ func (e *Record) DepthError(depth int, err error) *Record {
return e.initLocationCreated(depth)
}

// DepthLocaleString 输出 [localeutil.Stringer] 类型的内容到日志
//
// depth 表示调用,2 表示调用此方法的位置;
//
// 如果 [Logs.HasLocation] 为 false,那么 depth 将不起实际作用。
func (e *Record) DepthLocaleString(depth int, s localeutil.Stringer) *Record {
e.AppendMessage = func(b *Buffer) { b.AppendString(s.LocaleString(e.logs.printer)) }
return e.initLocationCreated(depth)
}

func appendError(p *localeutil.Printer, b *Buffer, ef xerrors.Formatter) {
err := ef.FormatError(b)
for err != nil {
Expand Down Expand Up @@ -273,6 +287,11 @@ func (e *withRecorder) String(s string) {
e.free()
}

func (e *withRecorder) LocaleString(s localeutil.Stringer) {
e.r.DepthLocaleString(3, s).Output(e.l)
e.free()
}

func (e *withRecorder) Print(v ...any) {
e.r.DepthPrint(3, v...).Output(e.l)
e.free()
Expand All @@ -296,6 +315,8 @@ func (l *disableRecorder) Error(error) {}

func (l *disableRecorder) String(string) {}

func (l *disableRecorder) LocaleString(localeutil.Stringer) {}

func (l *disableRecorder) Print(...any) {}

func (l *disableRecorder) Printf(string, ...any) {}
Expand Down

0 comments on commit 189bf44

Please sign in to comment.