log/add processor logtext;

This commit is contained in:
2024-01-17 15:58:30 +08:00
parent bef1a8cdc0
commit 8d24ba105d
7 changed files with 233 additions and 11 deletions

View File

@@ -2,7 +2,6 @@ package logjson
import (
"io"
"os"
"time"
)
@@ -60,10 +59,10 @@ func newConfig(opts ...Option) *config {
opt.apply(cfg)
}
if !cfg.hasPool {
cfg.bytesBufferPool = NewBytesBufferPool(bytesBufferInitialSize, bytesBufferMaximumSize)
cfg.bytesBufferPool = DefaultBytesBufferPool
}
if cfg.output == nil {
cfg.output = NewSyncWriter(os.Stderr)
cfg.output = DefaultStderrSyncWriter
}
if cfg.timestampFormat == "" {
cfg.timestampFormat = time.RFC3339Nano

View File

@@ -10,6 +10,8 @@ const (
bytesBufferMaximumSize = 4096
)
var DefaultBytesBufferPool = NewBytesBufferPool(bytesBufferInitialSize, bytesBufferMaximumSize)
type BytesBufferPool interface {
Get() *bytes.Buffer
Put(buffer *bytes.Buffer)

View File

@@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"git.blauwelle.com/go/crate/log/logsdk"
)
@@ -69,11 +68,11 @@ func (processor *Processor) Process(_ context.Context, entry logsdk.ReadonlyEntr
// Encode 2次分配
if err := encoder.Encode(m); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "JSON processor cannot encode log %#v: %s\n", m, err.Error())
_, _ = fmt.Fprintf(processor.output, "JSON processor cannot encode log %#v: %s\n", m, err.Error())
}
if _, err := buf.WriteTo(processor.output); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "JSON processor cannot write log: %s\n", err.Error())
_, _ = fmt.Fprintf(processor.output, "JSON processor cannot write log: %s\n", err.Error())
}
}

View File

@@ -2,9 +2,14 @@ package logjson
import (
"io"
"os"
"sync"
)
var (
DefaultStderrSyncWriter = NewSyncWriter(os.Stderr)
)
// NewSyncWriter 返回写互斥的 io.Writer
func NewSyncWriter(writer io.Writer) io.Writer {
return &syncWriter{