log/修改包名 jsonlog->logjson; 修改 logjson.config 的初始化方式;
This commit is contained in:
98
log/logsdk/logjson/option.go
Normal file
98
log/logsdk/logjson/option.go
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
package logjson
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.blauwelle.com/go/crate/synchelper"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Option 配置日志处理对象
|
||||||
|
type Option interface {
|
||||||
|
apply(cfg *config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithBufferPool 配置缓冲池
|
||||||
|
func WithBufferPool(pool synchelper.BytesBufferPool) Option {
|
||||||
|
return optionFunc(func(cfg *config) {
|
||||||
|
cfg.pool = pool
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithOutput 配置输出
|
||||||
|
func WithOutput(w io.Writer) Option {
|
||||||
|
return optionFunc(func(cfg *config) {
|
||||||
|
cfg.output = w
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithTimeFormat 配置时间格式
|
||||||
|
func WithTimeFormat(format string) Option {
|
||||||
|
return optionFunc(func(cfg *config) {
|
||||||
|
cfg.timestampFormat = format
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithDisableTime 配置仅用时间输出
|
||||||
|
func WithDisableTime(disable bool) Option {
|
||||||
|
return optionFunc(func(cfg *config) {
|
||||||
|
cfg.disableTime = disable
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithDisableHTMLEscape 配置禁止 HTML 转义
|
||||||
|
func WithDisableHTMLEscape(disable bool) Option {
|
||||||
|
return optionFunc(func(cfg *config) {
|
||||||
|
cfg.disableHTMLEscape = disable
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithPrettyPrint 配置 JSON 多行缩进输出
|
||||||
|
func WithPrettyPrint(pretty bool) Option {
|
||||||
|
return optionFunc(func(cfg *config) {
|
||||||
|
cfg.prettyPrint = pretty
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func newConfig(opts ...Option) *config {
|
||||||
|
cfg := defaultConfig()
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt.apply(cfg)
|
||||||
|
}
|
||||||
|
if !cfg.hasPool {
|
||||||
|
cfg.pool = synchelper.NewBytesBufferPool(512, 4096)
|
||||||
|
}
|
||||||
|
if cfg.output == nil {
|
||||||
|
cfg.output = synchelper.NewSyncWriter(os.Stderr)
|
||||||
|
}
|
||||||
|
if cfg.timestampFormat == "" {
|
||||||
|
cfg.timestampFormat = time.RFC3339Nano
|
||||||
|
}
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
func defaultConfig() *config {
|
||||||
|
return &config{
|
||||||
|
hasPool: false,
|
||||||
|
disableTime: false,
|
||||||
|
disableHTMLEscape: false,
|
||||||
|
prettyPrint: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
pool synchelper.BytesBufferPool
|
||||||
|
output io.Writer
|
||||||
|
timestampFormat string
|
||||||
|
hasPool bool
|
||||||
|
disableTime bool
|
||||||
|
disableHTMLEscape bool
|
||||||
|
prettyPrint bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type optionFunc func(cfg *config)
|
||||||
|
|
||||||
|
func (fn optionFunc) apply(cfg *config) {
|
||||||
|
fn(cfg)
|
||||||
|
}
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
package jsonlog
|
package logjson
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.blauwelle.com/go/crate/log/logsdk"
|
"git.blauwelle.com/go/crate/log/logsdk"
|
||||||
"git.blauwelle.com/go/crate/runtimehelper"
|
"git.blauwelle.com/go/crate/runtimehelper"
|
||||||
@@ -17,10 +16,7 @@ var _ logsdk.EntryProcessor = &Processor{}
|
|||||||
// New 返回日志处理对象,
|
// New 返回日志处理对象,
|
||||||
// 返回的对象是 [logsdk.EntryProcessor].
|
// 返回的对象是 [logsdk.EntryProcessor].
|
||||||
func New(opts ...Option) *Processor {
|
func New(opts ...Option) *Processor {
|
||||||
cfg := defaultConfig()
|
cfg := newConfig(opts...)
|
||||||
for _, opt := range opts {
|
|
||||||
opt.apply(cfg)
|
|
||||||
}
|
|
||||||
return &Processor{
|
return &Processor{
|
||||||
bufferPool: cfg.pool,
|
bufferPool: cfg.pool,
|
||||||
output: cfg.output,
|
output: cfg.output,
|
||||||
@@ -82,53 +78,6 @@ func (processor *Processor) Process(entry logsdk.ReadonlyEntry) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option 配置日志处理对象
|
|
||||||
type Option interface {
|
|
||||||
apply(cfg *config)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithBufferPool 配置缓冲池
|
|
||||||
func WithBufferPool(pool synchelper.BytesBufferPool) Option {
|
|
||||||
return optionFunc(func(cfg *config) {
|
|
||||||
cfg.pool = pool
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithOutput 配置输出
|
|
||||||
func WithOutput(w io.Writer) Option {
|
|
||||||
return optionFunc(func(cfg *config) {
|
|
||||||
cfg.output = w
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithTimeFormat 配置时间格式
|
|
||||||
func WithTimeFormat(format string) Option {
|
|
||||||
return optionFunc(func(cfg *config) {
|
|
||||||
cfg.timestampFormat = format
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithDisableTime 配置仅用时间输出
|
|
||||||
func WithDisableTime(disable bool) Option {
|
|
||||||
return optionFunc(func(cfg *config) {
|
|
||||||
cfg.disableTime = disable
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithDisableHTMLEscape 配置禁止 HTML 转义
|
|
||||||
func WithDisableHTMLEscape(disable bool) Option {
|
|
||||||
return optionFunc(func(cfg *config) {
|
|
||||||
cfg.disableHTMLEscape = disable
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithPrettyPrint 配置 JSON 多行缩进输出
|
|
||||||
func WithPrettyPrint(pretty bool) Option {
|
|
||||||
return optionFunc(func(cfg *config) {
|
|
||||||
cfg.prettyPrint = pretty
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Entry 被用来 JSON 序列化
|
// Entry 被用来 JSON 序列化
|
||||||
type Entry struct {
|
type Entry struct {
|
||||||
Message string `json:"msg"`
|
Message string `json:"msg"`
|
||||||
@@ -138,29 +87,3 @@ type Entry struct {
|
|||||||
Fields []logsdk.Field `json:"fields,omitempty"`
|
Fields []logsdk.Field `json:"fields,omitempty"`
|
||||||
Level logsdk.Level `json:"level"`
|
Level logsdk.Level `json:"level"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultConfig() *config {
|
|
||||||
return &config{
|
|
||||||
pool: synchelper.NewBytesBufferPool(512, 2048),
|
|
||||||
output: synchelper.NewSyncWriter(os.Stderr),
|
|
||||||
timestampFormat: time.RFC3339Nano,
|
|
||||||
disableTime: false,
|
|
||||||
disableHTMLEscape: false,
|
|
||||||
prettyPrint: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type config struct {
|
|
||||||
pool synchelper.BytesBufferPool
|
|
||||||
output io.Writer
|
|
||||||
timestampFormat string
|
|
||||||
disableTime bool
|
|
||||||
disableHTMLEscape bool
|
|
||||||
prettyPrint bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type optionFunc func(cfg *config)
|
|
||||||
|
|
||||||
func (fn optionFunc) apply(cfg *config) {
|
|
||||||
fn(cfg)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user