Skip to content

Commit

Permalink
升级依赖版本&&重构代码
Browse files Browse the repository at this point in the history
  • Loading branch information
JellyTony committed Mar 24, 2024
1 parent de50433 commit 1b19d76
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions logtest/logtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package logtest

import (
"bytes"
"encoding/json"
"io"
"testing"

"github.com/nextmicro/logger"
)

type Buffer struct {
buf *bytes.Buffer
t *testing.T
}

func Discard(t *testing.T) {
logger.DefaultLogger = logger.New(logger.WithWriter(io.Discard))
}

func NewCollector(t *testing.T) *Buffer {
var buf bytes.Buffer
logger.DefaultLogger = logger.New(logger.WithWriter(&buf))

t.Cleanup(func() {
logger.Sync()
})

return &Buffer{
buf: &buf,
t: t,
}
}

func (b *Buffer) Bytes() []byte {
return b.buf.Bytes()
}

func (b *Buffer) Content() string {
var m map[string]interface{}
if err := json.Unmarshal(b.buf.Bytes(), &m); err != nil {
return ""
}

content, ok := m["content"]
if !ok {
return ""
}

switch val := content.(type) {
case string:
return val
default:
// err is impossible to be not nil, unmarshaled from b.buf.Bytes()
bs, _ := json.Marshal(content)
return string(bs)
}
}

func (b *Buffer) Reset() {
b.buf.Reset()
}

func (b *Buffer) String() string {
return b.buf.String()
}

0 comments on commit 1b19d76

Please sign in to comment.