log/删除所有的依赖库;

This commit is contained in:
2023-04-12 17:09:05 +08:00
parent 4e1fe211cb
commit 1f39ef8c3a
8 changed files with 134 additions and 35 deletions

View File

@@ -4,8 +4,6 @@ import (
"io"
"os"
"time"
"git.blauwelle.com/go/crate/synchelper"
)
// Option 配置日志处理对象
@@ -14,9 +12,9 @@ type Option interface {
}
// WithBufferPool 配置缓冲池
func WithBufferPool(pool synchelper.BytesBufferPool) Option {
func WithBufferPool(pool BytesBufferPool) Option {
return optionFunc(func(cfg *config) {
cfg.pool = pool
cfg.bytesBufferPool = pool
cfg.hasPool = true
})
}
@@ -62,10 +60,10 @@ func newConfig(opts ...Option) *config {
opt.apply(cfg)
}
if !cfg.hasPool {
cfg.pool = synchelper.NewBytesBufferPool(512, 4096)
cfg.bytesBufferPool = newBytesBufferPool(512, 4096)
}
if cfg.output == nil {
cfg.output = synchelper.NewSyncWriter(os.Stderr)
cfg.output = newSyncWriter(os.Stderr)
}
if cfg.timestampFormat == "" {
cfg.timestampFormat = time.RFC3339Nano
@@ -83,7 +81,7 @@ func defaultConfig() *config {
}
type config struct {
pool synchelper.BytesBufferPool
bytesBufferPool BytesBufferPool
output io.Writer
timestampFormat string
hasPool bool

View File

@@ -0,0 +1,38 @@
package logjson
import (
"bytes"
"sync"
)
type BytesBufferPool interface {
Get() *bytes.Buffer
Put(buffer *bytes.Buffer)
}
func newBytesBufferPool(initialSize, maximumSize int) *bytesBufferPool {
return &bytesBufferPool{
pool: sync.Pool{
New: func() any {
return bytes.NewBuffer(make([]byte, 0, initialSize))
},
},
maximumSize: maximumSize,
}
}
type bytesBufferPool struct {
pool sync.Pool
maximumSize int
}
func (pool *bytesBufferPool) Get() *bytes.Buffer {
return pool.pool.Get().(*bytes.Buffer)
}
func (pool *bytesBufferPool) Put(buf *bytes.Buffer) {
if buf.Cap() > pool.maximumSize {
return
}
pool.pool.Put(buf)
}

View File

@@ -7,8 +7,6 @@ import (
"os"
"git.blauwelle.com/go/crate/log/logsdk"
"git.blauwelle.com/go/crate/runtimehelper"
"git.blauwelle.com/go/crate/synchelper"
)
var _ logsdk.EntryProcessor = &Processor{}
@@ -18,7 +16,7 @@ var _ logsdk.EntryProcessor = &Processor{}
func New(opts ...Option) *Processor {
cfg := newConfig(opts...)
return &Processor{
bufferPool: cfg.pool,
bytesBufferPool: cfg.bytesBufferPool,
output: cfg.output,
timeFormat: cfg.timestampFormat,
disableTime: cfg.disableTime,
@@ -29,7 +27,7 @@ func New(opts ...Option) *Processor {
// Processor 日志处理对象, 把日志处理成 JSON.
type Processor struct {
bufferPool synchelper.BytesBufferPool
bytesBufferPool BytesBufferPool
output io.Writer
timeFormat string
disableTime bool
@@ -51,16 +49,16 @@ func (processor *Processor) Process(entry logsdk.ReadonlyEntry) {
if entry.Caller.IsValid() {
// 1次分配
// 直接取 &entry.Caller 会增加堆内存分配
m.Caller = &runtimehelper.Frame{
m.Caller = &logsdk.Frame{
Function: entry.Caller.Function,
File: entry.Caller.File,
Line: entry.Caller.Line,
}
}
buf := processor.bufferPool.Get()
buf := processor.bytesBufferPool.Get()
buf.Reset()
defer processor.bufferPool.Put(buf)
defer processor.bytesBufferPool.Put(buf)
encoder := json.NewEncoder(buf)
if processor.prettyPrint {
@@ -80,10 +78,10 @@ func (processor *Processor) Process(entry logsdk.ReadonlyEntry) {
// Entry 被用来 JSON 序列化
type Entry struct {
Message string `json:"msg"`
Time string `json:"time,omitempty"`
Caller *runtimehelper.Frame `json:"caller,omitempty"`
Stack []runtimehelper.Frame `json:"stack,omitempty"`
Fields []logsdk.KV `json:"fields,omitempty"`
Level logsdk.Level `json:"level"`
Message string `json:"msg"`
Time string `json:"time,omitempty"`
Caller *logsdk.Frame `json:"caller,omitempty"`
Stack []logsdk.Frame `json:"stack,omitempty"`
Fields []logsdk.KV `json:"fields,omitempty"`
Level logsdk.Level `json:"level"`
}

View File

@@ -0,0 +1,24 @@
package logjson
import (
"io"
"sync"
)
func newSyncWriter(writer io.Writer) io.Writer {
return &syncWriter{
writer: writer,
lock: sync.Mutex{},
}
}
type syncWriter struct {
writer io.Writer
lock sync.Mutex
}
func (w *syncWriter) Write(p []byte) (n int, err error) {
w.lock.Lock()
defer w.lock.Unlock()
return w.writer.Write(p)
}