Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fac9762f72 | ||
|
|
0dc4c80cc2 |
@@ -3,7 +3,7 @@ name: default
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: test
|
- name: test
|
||||||
image: golang:1.21.5
|
image: golang:1.17.8
|
||||||
volumes:
|
volumes:
|
||||||
- name: deps
|
- name: deps
|
||||||
path: /go/src/mod
|
path: /go/src/mod
|
||||||
@@ -11,7 +11,7 @@ steps:
|
|||||||
- go test
|
- go test
|
||||||
|
|
||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
image: golangci/golangci-lint:v1.55
|
image: golangci/golangci-lint:v1.44
|
||||||
commands:
|
commands:
|
||||||
- golangci-lint run
|
- golangci-lint run
|
||||||
|
|
||||||
|
|||||||
@@ -23,9 +23,8 @@ linters-settings:
|
|||||||
min-name-length: 2
|
min-name-length: 2
|
||||||
ignore-names:
|
ignore-names:
|
||||||
- err
|
- err
|
||||||
- n
|
- "n"
|
||||||
- i
|
- i
|
||||||
- w
|
|
||||||
tagliatelle:
|
tagliatelle:
|
||||||
case:
|
case:
|
||||||
use-field-name: true
|
use-field-name: true
|
||||||
@@ -38,20 +37,6 @@ linters-settings:
|
|||||||
|
|
||||||
linters:
|
linters:
|
||||||
enable-all: true
|
enable-all: true
|
||||||
disable:
|
|
||||||
# deprecated
|
|
||||||
- interfacer
|
|
||||||
- structcheck
|
|
||||||
- varcheck
|
|
||||||
- golint
|
|
||||||
- deadcode
|
|
||||||
- scopelint
|
|
||||||
- exhaustivestruct
|
|
||||||
- ifshort
|
|
||||||
- nosnakecase
|
|
||||||
- maligned
|
|
||||||
|
|
||||||
- depguard # need configure
|
|
||||||
|
|
||||||
issues:
|
issues:
|
||||||
# Excluding configuration per-path, per-linter, per-text and per-source
|
# Excluding configuration per-path, per-linter, per-text and per-source
|
||||||
@@ -59,10 +44,5 @@ issues:
|
|||||||
- path: _test\.go
|
- path: _test\.go
|
||||||
linters:
|
linters:
|
||||||
- gomnd
|
- gomnd
|
||||||
|
- exhaustivestruct
|
||||||
- ireturn
|
- ireturn
|
||||||
- exhaustruct
|
|
||||||
- gochecknoglobals
|
|
||||||
- path: _example_test\.go
|
|
||||||
linters:
|
|
||||||
- lll
|
|
||||||
- goerr113
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"gitoa.ru/go-4devs/log/field"
|
"gitoa.ru/go-4devs/log/field"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// nolint: gochecknoglobals
|
||||||
var (
|
var (
|
||||||
errExample = errors.New("fail")
|
errExample = errors.New("fail")
|
||||||
_messages = fakeMessages(1000)
|
_messages = fakeMessages(1000)
|
||||||
@@ -117,7 +118,7 @@ func fakeSugarFields() []interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewLogger() log.Logger {
|
func NewLogger() log.Logger {
|
||||||
return log.New(log.WithWriter(io.Discard))
|
return log.New(log.WithWriter(ioutil.Discard))
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkDisabledWithoutFields(b *testing.B) {
|
func BenchmarkDisabledWithoutFields(b *testing.B) {
|
||||||
|
|||||||
@@ -2,14 +2,14 @@ package entry
|
|||||||
|
|
||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
//nolint:gochecknoglobals
|
// nolint: gochecknoglobals
|
||||||
var pool = sync.Pool{
|
var pool = sync.Pool{
|
||||||
New: func() interface{} {
|
New: func() interface{} {
|
||||||
return New()
|
return New()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:forcetypeassert
|
//nolint: forcetypeassert
|
||||||
func Get() *Entry {
|
func Get() *Entry {
|
||||||
e := pool.Get().(*Entry)
|
e := pool.Get().(*Entry)
|
||||||
e.Reset()
|
e.Reset()
|
||||||
|
|||||||
302
field/encoder.go
302
field/encoder.go
@@ -1,281 +1,31 @@
|
|||||||
//nolint:gomnd
|
|
||||||
package field
|
package field
|
||||||
|
|
||||||
import (
|
import "time"
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"time"
|
|
||||||
"unicode"
|
|
||||||
"unicode/utf8"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Encoder interface {
|
type Encoder interface {
|
||||||
AppendField(dst []byte, field Field) []byte
|
// Built-in types.
|
||||||
AppendValue(dst []byte, val Value) []byte
|
AddArray(key string, value Value)
|
||||||
}
|
AddAny(key string, value Value)
|
||||||
|
AddNil(key string)
|
||||||
func WithAppendString(fn func(dst []byte, in string) []byte) func(*BaseEncoder) {
|
AddBool(key string, value bool)
|
||||||
return func(be *BaseEncoder) {
|
AddBinary(key string, value []byte)
|
||||||
be.AppendString = fn
|
AddInt(key string, value int)
|
||||||
}
|
AddInt8(key string, value int8)
|
||||||
}
|
AddInt16(key string, value int16)
|
||||||
|
AddInt32(key string, value int32)
|
||||||
func WithNullValue(in string) func(*BaseEncoder) {
|
AddInt64(key string, value int64)
|
||||||
return func(be *BaseEncoder) {
|
AddUint(key string, value uint)
|
||||||
be.nullValue = []byte(in)
|
AddUint8(key string, value uint8)
|
||||||
}
|
AddUint16(key string, value uint16)
|
||||||
}
|
AddUint32(key string, value uint32)
|
||||||
|
AddUint64(key string, value uint64)
|
||||||
func WithDelimeter(in byte) func(*BaseEncoder) {
|
AddUintptr(key string, value uintptr)
|
||||||
return func(be *BaseEncoder) {
|
AddTime(key string, value time.Time)
|
||||||
be.delimeter = in
|
AddDuration(key string, value time.Duration)
|
||||||
}
|
AddFloat32(key string, value float32)
|
||||||
}
|
AddFloat64(key string, value float64)
|
||||||
|
AddComplex64(key string, value complex64)
|
||||||
func WithGropuConfig(start, end, deli byte) func(*BaseEncoder) {
|
AddComplex128(key string, value complex128)
|
||||||
return func(be *BaseEncoder) {
|
AddString(key, value string)
|
||||||
be.group = groupConfig{
|
AddError(key string, value error)
|
||||||
start: start,
|
|
||||||
end: end,
|
|
||||||
deli: deli,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func WithDefaultValue(fn func(dst []byte, e Encoder, val Value) []byte) func(*BaseEncoder) {
|
|
||||||
return func(be *BaseEncoder) {
|
|
||||||
be.DefaultValue = fn
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewEncoder(opts ...func(*BaseEncoder)) BaseEncoder {
|
|
||||||
be := BaseEncoder{
|
|
||||||
nullValue: []byte("null"),
|
|
||||||
group: groupConfig{
|
|
||||||
start: '{',
|
|
||||||
end: '}',
|
|
||||||
deli: ',',
|
|
||||||
},
|
|
||||||
array: groupConfig{
|
|
||||||
start: '[',
|
|
||||||
end: ']',
|
|
||||||
deli: ',',
|
|
||||||
},
|
|
||||||
timeFormat: time.RFC3339,
|
|
||||||
AppendString: AppendString,
|
|
||||||
delimeter: '=',
|
|
||||||
DefaultValue: func(dst []byte, e Encoder, val Value) []byte {
|
|
||||||
return e.AppendValue(dst, StringValue(fmt.Sprintf("%+v", val.Any())))
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, opt := range opts {
|
|
||||||
opt(&be)
|
|
||||||
}
|
|
||||||
|
|
||||||
return be
|
|
||||||
}
|
|
||||||
|
|
||||||
type groupConfig struct {
|
|
||||||
start byte
|
|
||||||
end byte
|
|
||||||
deli byte
|
|
||||||
}
|
|
||||||
|
|
||||||
type BaseEncoder struct {
|
|
||||||
nullValue []byte
|
|
||||||
group groupConfig
|
|
||||||
array groupConfig
|
|
||||||
timeFormat string
|
|
||||||
AppendString func(dst []byte, in string) []byte
|
|
||||||
delimeter byte
|
|
||||||
DefaultValue func(dst []byte, e Encoder, val Value) []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendValue(dst []byte, val Value) []byte {
|
|
||||||
return b.appendValue(dst, val, "", 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendDelimiter(dst []byte, deli byte) []byte {
|
|
||||||
if deli == 0 {
|
|
||||||
return dst
|
|
||||||
}
|
|
||||||
|
|
||||||
return append(dst, deli)
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:gocyclo,cyclop
|
|
||||||
func (b BaseEncoder) appendValue(dst []byte, val Value, prefix string, deli byte) []byte {
|
|
||||||
switch val.Kind {
|
|
||||||
case KindGroup:
|
|
||||||
return b.appendGroup(dst, val.AsGroup(), prefix)
|
|
||||||
case KindClosure:
|
|
||||||
return b.appendValue(dst, AnyValue(val.Resolve()), prefix, deli)
|
|
||||||
case KindArray:
|
|
||||||
return b.AppendArray(b.AppendDelimiter(dst, deli), val.AsArray())
|
|
||||||
case KindNil:
|
|
||||||
return b.AppendNull(b.AppendDelimiter(dst, deli))
|
|
||||||
case KindBool:
|
|
||||||
return b.AppendBool(b.AppendDelimiter(dst, deli), val.AsBool())
|
|
||||||
case KindBinary:
|
|
||||||
return b.AppendBytes(b.AppendDelimiter(dst, deli), val.AsBinary())
|
|
||||||
case KindComplex128:
|
|
||||||
return b.AppendComplex(b.AppendDelimiter(dst, deli), val.AsComplex128())
|
|
||||||
case KindInt64:
|
|
||||||
return b.AppendInt(b.AppendDelimiter(dst, deli), val.AsInt64())
|
|
||||||
case KindFloat32:
|
|
||||||
return b.AppendFloat(b.AppendDelimiter(dst, deli), float64(val.AsFloat32()), 32)
|
|
||||||
case KindFloat64:
|
|
||||||
return b.AppendFloat(b.AppendDelimiter(dst, deli), val.AsFloat64(), 64)
|
|
||||||
case KindUint64:
|
|
||||||
return b.AppendUint(b.AppendDelimiter(dst, deli), val.AsUint64())
|
|
||||||
case KindError:
|
|
||||||
return b.AppendString(b.AppendDelimiter(dst, deli), val.AsError().Error())
|
|
||||||
case KindString:
|
|
||||||
return b.AppendString(b.AppendDelimiter(dst, deli), val.AsString())
|
|
||||||
case KindDuration:
|
|
||||||
return b.AppendDuration(b.AppendDelimiter(dst, deli), val.AsDuration())
|
|
||||||
case KindTime:
|
|
||||||
return b.AppendTime(b.AppendDelimiter(dst, deli), val.AsTime())
|
|
||||||
case KindAny:
|
|
||||||
return b.DefaultValue(b.AppendDelimiter(dst, deli), b, val)
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.DefaultValue(b.AppendDelimiter(dst, deli), b, val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendDuration(dst []byte, d time.Duration) []byte {
|
|
||||||
return b.AppendString(dst, d.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendTime(dst []byte, t time.Time) []byte {
|
|
||||||
return b.AppendString(dst, t.Format(b.timeFormat))
|
|
||||||
}
|
|
||||||
|
|
||||||
func AppendString(dst []byte, in string) []byte {
|
|
||||||
if needsQuoting(in) {
|
|
||||||
return strconv.AppendQuote(dst, in)
|
|
||||||
}
|
|
||||||
|
|
||||||
return append(dst, in...)
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:cyclop
|
|
||||||
func needsQuoting(in string) bool {
|
|
||||||
if len(in) == 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(in); {
|
|
||||||
char := in[i]
|
|
||||||
if char < utf8.RuneSelf {
|
|
||||||
// Quote anything except a backslash that would need quoting in a
|
|
||||||
// JSON string, as well as space and '='
|
|
||||||
if char != '\\' && (char == ' ' || char == '=' || !safeSet[char]) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
i++
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
decodeRune, size := utf8.DecodeRuneInString(in[i:])
|
|
||||||
if decodeRune == utf8.RuneError || unicode.IsSpace(decodeRune) || !unicode.IsPrint(decodeRune) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
i += size
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendField(dst []byte, field Field) []byte {
|
|
||||||
prefix := ""
|
|
||||||
|
|
||||||
if len(dst) != 0 {
|
|
||||||
prew := dst[len(dst)-1]
|
|
||||||
if prew != '{' && prew != '.' {
|
|
||||||
prefix = string(b.group.deli)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.appendField(dst, field, prefix, b.delimeter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) appendField(dst []byte, field Field, prefix string, deli byte) []byte {
|
|
||||||
dst = b.AppendKey(dst, field.Key, prefix)
|
|
||||||
|
|
||||||
return b.appendValue(dst, field.Value, field.Key+".", deli)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendKey(dst []byte, key string, prefix string) []byte {
|
|
||||||
if prefix != "" {
|
|
||||||
dst = append(dst, prefix...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.AppendString(dst, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendComplex(dst []byte, c complex128) []byte {
|
|
||||||
cmplx := strconv.FormatComplex(c, 'g', -1, 128)
|
|
||||||
|
|
||||||
return b.AppendString(dst, cmplx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendFloat(dst []byte, f float64, bitSize int) []byte {
|
|
||||||
return strconv.AppendFloat(dst, f, 'g', -1, bitSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendUint(dst []byte, u uint64) []byte {
|
|
||||||
return strconv.AppendUint(dst, u, 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendNull(dst []byte) []byte {
|
|
||||||
return append(dst, b.nullValue...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendInt(dst []byte, val int64) []byte {
|
|
||||||
return strconv.AppendInt(dst, val, 10)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendBool(dst []byte, val bool) []byte {
|
|
||||||
return strconv.AppendBool(dst, val)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendGroup(dst []byte, fields []Field) []byte {
|
|
||||||
dst = append(dst, b.group.start)
|
|
||||||
dst = b.appendGroup(dst, fields, "")
|
|
||||||
|
|
||||||
return append(dst, b.group.end)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) appendGroup(dst []byte, fields []Field, prefix string) []byte {
|
|
||||||
if len(fields) > 0 {
|
|
||||||
dst = b.appendField(dst, fields[0], ".", b.delimeter)
|
|
||||||
for _, field := range fields[1:] {
|
|
||||||
dst = b.appendField(append(dst, b.group.deli), field, prefix, b.delimeter)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return dst
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendArray(dst []byte, in []Value) []byte {
|
|
||||||
dst = append(dst, b.array.start)
|
|
||||||
if len(in) > 0 {
|
|
||||||
dst = b.appendValue(dst, in[0], "", 0)
|
|
||||||
for _, value := range in[1:] {
|
|
||||||
dst = b.appendValue(append(dst, b.array.deli), value, "", 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return append(dst, b.array.end)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b BaseEncoder) AppendBytes(dst, in []byte) []byte {
|
|
||||||
dst = append(dst, '"')
|
|
||||||
dst = append(dst, in...)
|
|
||||||
|
|
||||||
return append(dst, '"')
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
package field
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewEncoderJSON(opts ...func(*BaseEncoder)) BaseEncoder {
|
|
||||||
opts = append([]func(*BaseEncoder){
|
|
||||||
WithAppendString(strconv.AppendQuote),
|
|
||||||
WithDelimeter(':'),
|
|
||||||
WithDefaultValue(func(dst []byte, e Encoder, val Value) []byte {
|
|
||||||
js, err := json.Marshal(val.Any())
|
|
||||||
if err != nil {
|
|
||||||
return e.AppendValue(dst, ErrorValue(err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return append(dst, js...)
|
|
||||||
}),
|
|
||||||
}, opts...)
|
|
||||||
|
|
||||||
return NewEncoder(opts...)
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
package field_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/log/field"
|
|
||||||
"gitoa.ru/go-4devs/log/internal/buffer"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestEncoderJSONAppendField_string(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
const expect = `"array":["value","other"],"str":"value","nullableStr":"value","nullstr":null`
|
|
||||||
|
|
||||||
encode := field.NewEncoderJSON()
|
|
||||||
|
|
||||||
buf := buffer.New()
|
|
||||||
defer func() {
|
|
||||||
buf.Free()
|
|
||||||
}()
|
|
||||||
|
|
||||||
val := "value"
|
|
||||||
strs := field.Strings("array", val, "other")
|
|
||||||
*buf = encode.AppendField(*buf, strs)
|
|
||||||
|
|
||||||
str := field.String("str", val)
|
|
||||||
*buf = encode.AppendField(*buf, str)
|
|
||||||
|
|
||||||
strp := field.Stringp("nullableStr", &val)
|
|
||||||
*buf = encode.AppendField(*buf, strp)
|
|
||||||
|
|
||||||
nullStr := field.Stringp("nullstr", nil)
|
|
||||||
*buf = encode.AppendField(*buf, nullStr)
|
|
||||||
|
|
||||||
if buf.String() != expect {
|
|
||||||
t.Errorf("json string expect:%v got:%s", expect, buf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package field
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewEncoderText(opts ...func(*BaseEncoder)) BaseEncoder {
|
|
||||||
opts = append([]func(*BaseEncoder){
|
|
||||||
WithGropuConfig(0, 0, ' '),
|
|
||||||
WithNullValue("<nil>"),
|
|
||||||
WithDefaultValue(func(dst []byte, _ Encoder, val Value) []byte {
|
|
||||||
return fmt.Appendf(dst, "%+v", val.Any())
|
|
||||||
}),
|
|
||||||
}, opts...)
|
|
||||||
|
|
||||||
return NewEncoder(opts...)
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package field
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
var ErrUndefined = errors.New("indefined")
|
|
||||||
448
field/field.go
448
field/field.go
@@ -5,499 +5,329 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Any(key string, value any) Field {
|
func Any(key string, value interface{}) Field {
|
||||||
return Field{
|
return Key(key).Any(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func String(key, value string) Field {
|
func String(key, value string) Field {
|
||||||
return Field{
|
return Key(key).String(value)
|
||||||
Key: key,
|
|
||||||
Value: StringValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Stringp(key string, value *string) Field {
|
func Stringp(key string, value *string) Field {
|
||||||
return Field{
|
return Key(key).Stringp(value)
|
||||||
Key: key,
|
|
||||||
Value: StringpValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Strings(key string, value ...string) Field {
|
func Strings(key string, value ...string) Field {
|
||||||
return Field{
|
return Key(key).Strings(value...)
|
||||||
Key: key,
|
|
||||||
Value: StringsValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Bool(key string, value bool) Field {
|
func Bool(key string, value bool) Field {
|
||||||
return Field{
|
return Key(key).Bool(value)
|
||||||
Key: key,
|
|
||||||
Value: BoolValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Bools(key string, value ...bool) Field {
|
func Bools(key string, value ...bool) Field {
|
||||||
return Field{
|
return Key(key).Bools(value...)
|
||||||
Key: key,
|
|
||||||
Value: BoolsValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Boolp(key string, value *bool) Field {
|
func Boolp(key string, value *bool) Field {
|
||||||
return Field{
|
return Key(key).Boolp(value)
|
||||||
Key: key,
|
|
||||||
Value: BoolpValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint(key string, value uint) Field {
|
func Uint(key string, value uint) Field {
|
||||||
return Field{
|
return Key(key).Uint(value)
|
||||||
Key: key,
|
|
||||||
Value: Uint64Value(uint64(value)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uints(key string, value ...uint) Field {
|
func Uints(key string, value ...uint) Field {
|
||||||
return Field{
|
return Key(key).Uints(value...)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uintp(key string, value *uint) Field {
|
func Uintp(key string, value *uint) Field {
|
||||||
return Field{
|
return Key(key).Uintp(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint8(key string, value uint8) Field {
|
func Uint8(key string, value uint8) Field {
|
||||||
return Field{
|
return Key(key).Uint8(value)
|
||||||
Key: key,
|
|
||||||
Value: Uint64Value(uint64(value)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint8s(key string, value ...uint8) Field {
|
func Uint8s(key string, value ...uint8) Field {
|
||||||
return Field{
|
return Key(key).Uint8s(value...)
|
||||||
Key: key,
|
|
||||||
Value: Uint8sValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint8p(key string, value *uint8) Field {
|
func Uint8p(key string, value *uint8) Field {
|
||||||
return Field{
|
return Key(key).Uint8p(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint16(key string, value uint16) Field {
|
func Uint16(key string, value uint16) Field {
|
||||||
return Field{
|
return Key(key).Uint16(value)
|
||||||
Key: key,
|
|
||||||
Value: Uint64Value(uint64(value)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint16s(key string, value ...uint16) Field {
|
func Uint16s(key string, value ...uint16) Field {
|
||||||
return Field{
|
return Key(key).Uint16s(value...)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint16p(key string, value *uint16) Field {
|
func Uint16p(key string, value *uint16) Field {
|
||||||
return Field{
|
return Key(key).Uint16p(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint32(key string, value uint32) Field {
|
func Uint32(key string, value uint32) Field {
|
||||||
return Field{
|
return Key(key).Uint32(value)
|
||||||
Key: key,
|
|
||||||
Value: Uint64Value(uint64(value)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint32s(key string, value ...uint32) Field {
|
func Uint32s(key string, value ...uint32) Field {
|
||||||
return Field{
|
return Key(key).Uint32s(value...)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint32p(key string, value *uint32) Field {
|
func Uint32p(key string, value *uint32) Field {
|
||||||
return Field{
|
return Key(key).Uint32p(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint64(key string, value uint64) Field {
|
func Uint64(key string, value uint64) Field {
|
||||||
return Field{
|
return Key(key).Uint64(value)
|
||||||
Key: key,
|
|
||||||
Value: Uint64Value(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint64s(key string, value ...uint64) Field {
|
func Uint64s(key string, value ...uint64) Field {
|
||||||
return Field{
|
return Key(key).Uint64s(value...)
|
||||||
Key: key,
|
|
||||||
Value: Uint64sValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uint64p(key string, value *uint64) Field {
|
func Uint64p(key string, value *uint64) Field {
|
||||||
return Field{
|
return Key(key).Uint64p(value)
|
||||||
Key: key,
|
|
||||||
Value: Uint64pValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int(key string, value int) Field {
|
func Int(key string, value int) Field {
|
||||||
return Field{
|
return Key(key).Int(value)
|
||||||
Key: key,
|
|
||||||
Value: Int64Value(int64(value)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Ints(key string, value ...int) Field {
|
func Ints(key string, value ...int) Field {
|
||||||
return Field{
|
return Key(key).Ints(value...)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Intp(key string, value *int) Field {
|
func Intp(key string, value *int) Field {
|
||||||
return Field{
|
return Key(key).Intp(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int8(key string, value int8) Field {
|
func Int8(key string, value int8) Field {
|
||||||
return Field{
|
return Key(key).Int8(value)
|
||||||
Key: key,
|
|
||||||
Value: Int64Value(int64(value)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int8s(key string, value ...int8) Field {
|
func Int8s(key string, value ...int8) Field {
|
||||||
return Field{
|
return Key(key).Int8s(value...)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int8p(key string, value *int8) Field {
|
func Int8p(key string, value *int8) Field {
|
||||||
return Field{
|
return Key(key).Int8p(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int16(key string, value int16) Field {
|
func Int16(key string, value int16) Field {
|
||||||
return Field{
|
return Key(key).Int16(value)
|
||||||
Key: key,
|
|
||||||
Value: Int64Value(int64(value)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int16s(key string, value ...int16) Field {
|
func Int16s(key string, value ...int16) Field {
|
||||||
return Field{
|
return Key(key).Int16s(value...)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int16p(key string, value *int16) Field {
|
func Int16p(key string, value *int16) Field {
|
||||||
return Field{
|
return Key(key).Int16p(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int32(key string, value int32) Field {
|
func Int32(key string, value int32) Field {
|
||||||
return Field{
|
return Key(key).Int32(value)
|
||||||
Key: key,
|
|
||||||
Value: Int64Value(int64(value)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int32s(key string, value ...int32) Field {
|
func Int32s(key string, value ...int32) Field {
|
||||||
return Field{
|
return Key(key).Int32s(value...)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int32p(key string, value *int32) Field {
|
func Int32p(key string, value *int32) Field {
|
||||||
return Field{
|
return Key(key).Int32p(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int64(key string, value int64) Field {
|
func Int64(key string, value int64) Field {
|
||||||
return Field{
|
return Key(key).Int64(value)
|
||||||
Key: key,
|
|
||||||
Value: Int64Value(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int64s(key string, value ...int64) Field {
|
func Int64s(key string, value ...int64) Field {
|
||||||
return Field{
|
return Key(key).Int64s(value...)
|
||||||
Key: key,
|
|
||||||
Value: Int64sValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Int64p(key string, value *int64) Field {
|
func Int64p(key string, value *int64) Field {
|
||||||
return Field{
|
return Key(key).Int64p(value)
|
||||||
Key: key,
|
|
||||||
Value: Int64pValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Float32(key string, value float32) Field {
|
func Float32(key string, value float32) Field {
|
||||||
return Field{
|
return Key(key).Float32(value)
|
||||||
Key: key,
|
|
||||||
Value: Float64Value(float64(value)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Float32s(key string, value ...float32) Field {
|
func Float32s(key string, value ...float32) Field {
|
||||||
return Field{
|
return Key(key).Float32s(value...)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Float32p(key string, value *float32) Field {
|
func Float32p(key string, value *float32) Field {
|
||||||
return Field{
|
return Key(key).Float32p(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Float64(key string, value float64) Field {
|
func Float64(key string, value float64) Field {
|
||||||
return Field{
|
return Key(key).Float64(value)
|
||||||
Key: key,
|
|
||||||
Value: Float64Value(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Float64s(key string, value ...float64) Field {
|
func Float64s(key string, value ...float64) Field {
|
||||||
return Field{
|
return Key(key).Float64s(value...)
|
||||||
Key: key,
|
|
||||||
Value: Float64sValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Float64p(key string, value *float64) Field {
|
func Float64p(key string, value *float64) Field {
|
||||||
return Field{
|
return Key(key).Float64p(value)
|
||||||
Key: key,
|
|
||||||
Value: Float64pValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Complex64(key string, value complex64) Field {
|
func Complex64(key string, value complex64) Field {
|
||||||
return Field{
|
return Key(key).Complex64(value)
|
||||||
Key: key,
|
|
||||||
Value: Complex128Value(complex128(value)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Complex64s(key string, value ...complex64) Field {
|
func Complex64s(key string, value ...complex64) Field {
|
||||||
return Field{
|
return Key(key).Complex64s(value...)
|
||||||
Key: key,
|
|
||||||
Value: Complex64sValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Complex64p(key string, value *complex64) Field {
|
func Complex64p(key string, value *complex64) Field {
|
||||||
return Field{
|
return Key(key).Complex64p(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Complex128(key string, value complex128) Field {
|
|
||||||
return Field{
|
|
||||||
Key: key,
|
|
||||||
Value: Complex128Value(value),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Complex128s(key string, value ...complex128) Field {
|
|
||||||
return Field{
|
|
||||||
Key: key,
|
|
||||||
Value: Complex128sValue(value),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Complex128p(key string, value *complex128) Field {
|
|
||||||
return Field{
|
|
||||||
Key: key,
|
|
||||||
Value: Complex128pValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uintptr(key string, value uintptr) Field {
|
func Uintptr(key string, value uintptr) Field {
|
||||||
return Field{
|
return Key(key).Uintptr(value)
|
||||||
Key: key,
|
|
||||||
Value: Uint64Value(uint64(value)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uintptrs(key string, value ...uintptr) Field {
|
func Uintptrs(key string, value ...uintptr) Field {
|
||||||
return Field{
|
return Key(key).Uintptrs(value...)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Uintptrp(key string, value *uintptr) Field {
|
func Uintptrp(key string, value *uintptr) Field {
|
||||||
return Field{
|
return Key(key).Uintptrp(value)
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Bytes(key string, value []byte) Field {
|
func Bytes(key string, value []byte) Field {
|
||||||
return Field{
|
return Key(key).Bytes(value)
|
||||||
Key: key,
|
|
||||||
Value: BytesValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Duration(key string, value time.Duration) Field {
|
func Duration(key string, value time.Duration) Field {
|
||||||
return Field{
|
return Key(key).Dureation(value)
|
||||||
Key: key,
|
|
||||||
Value: DurationValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Durations(key string, value ...time.Duration) Field {
|
func Durations(key string, value ...time.Duration) Field {
|
||||||
return Field{
|
return Key(key).Dureations(value)
|
||||||
Key: key,
|
|
||||||
Value: DurationsValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Durationp(key string, value *time.Duration) Field {
|
func Durationp(key string, value *time.Duration) Field {
|
||||||
return Field{
|
return Key(key).Dureationp(value)
|
||||||
Key: key,
|
|
||||||
Value: DurationpValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Time(key string, value time.Time) Field {
|
func Time(key string, value time.Time) Field {
|
||||||
return Field{
|
return Key(key).Time(value)
|
||||||
Key: key,
|
|
||||||
Value: TimeValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Times(key string, value ...time.Time) Field {
|
func Times(key string, value ...time.Time) Field {
|
||||||
return Field{
|
return Key(key).Times(value...)
|
||||||
Key: key,
|
|
||||||
Value: TimesValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Timep(key string, value *time.Time) Field {
|
func Timep(key string, value *time.Time) Field {
|
||||||
return Field{
|
return Key(key).Timep(value)
|
||||||
Key: key,
|
|
||||||
Value: TimepValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FormatTime(key, format string, value time.Time) Field {
|
func FormatTime(key, format string, value time.Time) Field {
|
||||||
return Field{
|
return Key(key).FormatTime(format, value)
|
||||||
Key: key,
|
|
||||||
Value: ClosureValue(func() any {
|
|
||||||
return value.Format(format)
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FormatTimes(key, format string, value ...time.Time) Field {
|
func FormatTimes(key, foramt string, value ...time.Time) Field {
|
||||||
return Field{
|
return Key(key).FormatTimes(foramt, value...)
|
||||||
Key: key,
|
|
||||||
Value: ClosureValue(func() any {
|
|
||||||
times := make([]any, len(value))
|
|
||||||
for idx, val := range value {
|
|
||||||
times[idx] = val.Format(format)
|
|
||||||
}
|
|
||||||
|
|
||||||
return times
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FormatTimep(key, format string, value *time.Time) Field {
|
func FormatTimep(key, foramt string, value *time.Time) Field {
|
||||||
isNill := value == nil
|
return Key(key).FormatTimep(foramt, value)
|
||||||
|
|
||||||
return Field{
|
|
||||||
Key: key,
|
|
||||||
Value: ClosureValue(func() any {
|
|
||||||
if isNill {
|
|
||||||
return NilValue()
|
|
||||||
}
|
|
||||||
|
|
||||||
return value.Format(format)
|
|
||||||
}),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Error(key string, value error) Field {
|
func Error(key string, value error) Field {
|
||||||
return Field{
|
return Key(key).Error(value)
|
||||||
Key: key,
|
|
||||||
Value: ErrorValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Errors(key string, value ...error) Field {
|
func Errors(key string, value ...error) Field {
|
||||||
return Field{
|
return Key(key).Errors(value...)
|
||||||
Key: key,
|
|
||||||
Value: ErrorsValue(value),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Groups(key string, value ...Field) Field {
|
|
||||||
return Field{
|
|
||||||
Key: key,
|
|
||||||
Value: GroupValue(value...),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Valuer(key string, value LogValuer) Field {
|
|
||||||
return Field{
|
|
||||||
Key: key,
|
|
||||||
Value: AnyValue(value),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ValuerFn(key string, value ClosureFn) Field {
|
|
||||||
return Field{
|
|
||||||
Key: key,
|
|
||||||
Value: ClosureValue(value),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Field struct.
|
// Field struct.
|
||||||
type Field struct {
|
type Field struct {
|
||||||
Key string
|
key Key
|
||||||
Value Value
|
value Value
|
||||||
|
}
|
||||||
|
|
||||||
|
//nolint: gocyclo,cyclop
|
||||||
|
func (f Field) AddTo(enc Encoder) {
|
||||||
|
key := string(f.key)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case f.value.IsArray():
|
||||||
|
enc.AddAny(key, f.value)
|
||||||
|
case f.value.IsNil():
|
||||||
|
enc.AddNil(key)
|
||||||
|
case f.value.IsBool():
|
||||||
|
enc.AddBool(key, f.value.asBool())
|
||||||
|
case f.value.IsBinary():
|
||||||
|
enc.AddBinary(key, f.value.asBinary())
|
||||||
|
case f.value.IsInt():
|
||||||
|
enc.AddInt(key, f.value.asInt())
|
||||||
|
case f.value.IsInt8():
|
||||||
|
enc.AddInt8(key, f.value.asInt8())
|
||||||
|
case f.value.IsInt16():
|
||||||
|
enc.AddInt16(key, f.value.asInt16())
|
||||||
|
case f.value.IsInt32():
|
||||||
|
enc.AddInt32(key, f.value.asInt32())
|
||||||
|
case f.value.IsInt64():
|
||||||
|
enc.AddInt64(key, f.value.asInt64())
|
||||||
|
case f.value.IsUint():
|
||||||
|
enc.AddUint(key, f.value.asUint())
|
||||||
|
case f.value.IsUint8():
|
||||||
|
enc.AddUint8(key, f.value.asUint8())
|
||||||
|
case f.value.IsUint16():
|
||||||
|
enc.AddUint16(key, f.value.asUint16())
|
||||||
|
case f.value.IsUint32():
|
||||||
|
enc.AddUint32(key, f.value.asUint32())
|
||||||
|
case f.value.IsUint64():
|
||||||
|
enc.AddUint64(key, f.value.asUint64())
|
||||||
|
case f.value.IsUintptr():
|
||||||
|
enc.AddUintptr(key, f.value.asUintptr())
|
||||||
|
case f.value.IsTime():
|
||||||
|
enc.AddTime(key, f.value.asTime())
|
||||||
|
case f.value.IsDuration():
|
||||||
|
enc.AddDuration(key, f.value.asDuration())
|
||||||
|
case f.value.IsFloat32():
|
||||||
|
enc.AddFloat32(key, f.value.asFloat32())
|
||||||
|
case f.value.IsFloat64():
|
||||||
|
enc.AddFloat64(key, f.value.asFloat64())
|
||||||
|
case f.value.IsComplex64():
|
||||||
|
enc.AddComplex64(key, f.value.asComplex64())
|
||||||
|
case f.value.IsComplex128():
|
||||||
|
enc.AddComplex128(key, f.value.asComplex128())
|
||||||
|
case f.value.IsString():
|
||||||
|
enc.AddString(key, f.value.asString())
|
||||||
|
case f.value.IsError():
|
||||||
|
enc.AddError(key, f.value.asError())
|
||||||
|
default:
|
||||||
|
enc.AddAny(key, f.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Field) Type() Type {
|
||||||
|
return f.value.vtype
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Field) Key() Key {
|
||||||
|
return f.key
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Field) Value() Value {
|
||||||
|
return f.value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f Field) AsInterface() interface{} {
|
||||||
|
return f.value.AsInterface()
|
||||||
}
|
}
|
||||||
|
|
||||||
// String implent stringer.
|
// String implent stringer.
|
||||||
func (f Field) String() string {
|
func (f Field) String() string {
|
||||||
return fmt.Sprintf("%s=%+v", f.Key, f.Value)
|
return fmt.Sprintf("%s=%+v", f.key, f.value.AsInterface())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,22 +2,7 @@ package field
|
|||||||
|
|
||||||
type Fields []Field
|
type Fields []Field
|
||||||
|
|
||||||
func (f Fields) Fields(fn func(Field) bool) {
|
type MapField map[Key]Value
|
||||||
for idx := range f {
|
|
||||||
if !fn(f[idx]) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f Fields) Any() any {
|
|
||||||
fields := make(map[string]any)
|
|
||||||
for idx := range f {
|
|
||||||
fields[f[idx].Key] = f[idx].Value.Any()
|
|
||||||
}
|
|
||||||
|
|
||||||
return fields
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f Fields) Append(fields ...Field) Fields {
|
func (f Fields) Append(fields ...Field) Fields {
|
||||||
f = append(f, fields...)
|
f = append(f, fields...)
|
||||||
@@ -32,3 +17,13 @@ func (f Fields) Set(idx int, field Field) {
|
|||||||
func (f Fields) Len() int {
|
func (f Fields) Len() int {
|
||||||
return len(f)
|
return len(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f Fields) AsMap() MapField {
|
||||||
|
fields := make(MapField, len(f))
|
||||||
|
|
||||||
|
for _, field := range f {
|
||||||
|
fields[field.Key()] = field.Value()
|
||||||
|
}
|
||||||
|
|
||||||
|
return fields
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package field_test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"gitoa.ru/go-4devs/log/field"
|
"gitoa.ru/go-4devs/log/field"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -12,7 +13,5 @@ func TestFields_Append(t *testing.T) {
|
|||||||
fields := field.Fields{field.Any("any", "value")}
|
fields := field.Fields{field.Any("any", "value")}
|
||||||
fields = fields.Append(field.String("string", "value"))
|
fields = fields.Append(field.String("string", "value"))
|
||||||
|
|
||||||
if len(fields) != 2 {
|
require.Len(t, fields, 2)
|
||||||
t.Fatalf("require 2 field got %v", len(fields))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
570
field/key.go
Normal file
570
field/key.go
Normal file
@@ -0,0 +1,570 @@
|
|||||||
|
package field
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Key string
|
||||||
|
|
||||||
|
//nolint: gocyclo,funlen,cyclop
|
||||||
|
func (k Key) Any(value interface{}) Field {
|
||||||
|
switch val := value.(type) {
|
||||||
|
case string:
|
||||||
|
return k.String(val)
|
||||||
|
case *string:
|
||||||
|
return k.Stringp(val)
|
||||||
|
case []string:
|
||||||
|
return k.Strings(val...)
|
||||||
|
case bool:
|
||||||
|
return k.Bool(val)
|
||||||
|
case *bool:
|
||||||
|
return k.Boolp(val)
|
||||||
|
case []bool:
|
||||||
|
return k.Bools(val...)
|
||||||
|
case int8:
|
||||||
|
return k.Int8(val)
|
||||||
|
case []int8:
|
||||||
|
return k.Int8s(val...)
|
||||||
|
case *int8:
|
||||||
|
return k.Int8p(val)
|
||||||
|
case int16:
|
||||||
|
return k.Int16(val)
|
||||||
|
case []int16:
|
||||||
|
return k.Int16s(val...)
|
||||||
|
case *int16:
|
||||||
|
return k.Int16p(val)
|
||||||
|
case int32:
|
||||||
|
return k.Int32(val)
|
||||||
|
case []int32:
|
||||||
|
return k.Int32s(val...)
|
||||||
|
case *int32:
|
||||||
|
return k.Int32p(val)
|
||||||
|
case int64:
|
||||||
|
return k.Int64(val)
|
||||||
|
case []int64:
|
||||||
|
return k.Int64s(val...)
|
||||||
|
case *int64:
|
||||||
|
return k.Int64p(val)
|
||||||
|
case uint:
|
||||||
|
return k.Uint(val)
|
||||||
|
case []uint:
|
||||||
|
return k.Uints(val...)
|
||||||
|
case *uint:
|
||||||
|
return k.Uintp(val)
|
||||||
|
case uint8:
|
||||||
|
return k.Uint8(val)
|
||||||
|
case *uint8:
|
||||||
|
return k.Uint8p(val)
|
||||||
|
case uint16:
|
||||||
|
return k.Uint16(val)
|
||||||
|
case []uint16:
|
||||||
|
return k.Uint16s(val...)
|
||||||
|
case *uint16:
|
||||||
|
return k.Uint16p(val)
|
||||||
|
case uint32:
|
||||||
|
return k.Uint32(val)
|
||||||
|
case []uint32:
|
||||||
|
return k.Uint32s(val...)
|
||||||
|
case *uint32:
|
||||||
|
return k.Uint32p(val)
|
||||||
|
case uint64:
|
||||||
|
return k.Uint64(val)
|
||||||
|
case []uint64:
|
||||||
|
return k.Uint64s(val...)
|
||||||
|
case *uint64:
|
||||||
|
return k.Uint64p(val)
|
||||||
|
case float32:
|
||||||
|
return k.Float32(val)
|
||||||
|
case []float32:
|
||||||
|
return k.Float32s(val...)
|
||||||
|
case *float32:
|
||||||
|
return k.Float32p(val)
|
||||||
|
case float64:
|
||||||
|
return k.Float64(val)
|
||||||
|
case []float64:
|
||||||
|
return k.Float64s(val...)
|
||||||
|
case *float64:
|
||||||
|
return k.Float64p(val)
|
||||||
|
case complex64:
|
||||||
|
return k.Complex64(val)
|
||||||
|
case []complex64:
|
||||||
|
return k.Complex64s(val...)
|
||||||
|
case *complex64:
|
||||||
|
return k.Complex64p(val)
|
||||||
|
case uintptr:
|
||||||
|
return k.Uintptr(val)
|
||||||
|
case []uintptr:
|
||||||
|
return k.Uintptrs(val...)
|
||||||
|
case *uintptr:
|
||||||
|
return k.Uintptrp(val)
|
||||||
|
case []byte:
|
||||||
|
return k.Bytes(val)
|
||||||
|
case time.Duration:
|
||||||
|
return k.Dureation(val)
|
||||||
|
case []time.Duration:
|
||||||
|
return k.Dureations(val)
|
||||||
|
case *time.Duration:
|
||||||
|
return k.Dureationp(val)
|
||||||
|
case time.Time:
|
||||||
|
return k.Time(val)
|
||||||
|
case []time.Time:
|
||||||
|
return k.Times(val...)
|
||||||
|
case *time.Time:
|
||||||
|
return k.Timep(val)
|
||||||
|
case error:
|
||||||
|
return k.Error(val)
|
||||||
|
case []error:
|
||||||
|
return k.Errors(val...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: Value{
|
||||||
|
value: value,
|
||||||
|
vtype: TypeAny,
|
||||||
|
numeric: 0,
|
||||||
|
stringly: "",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) String(value string) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: stringValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Strings(value ...string) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: stringsValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Stringp(value *string) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: stringpValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Bool(value bool) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: boolValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Bools(value ...bool) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: boolsValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Boolp(value *bool) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: boolpValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int(value int) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: intValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Ints(value ...int) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: intsValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Intp(value *int) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: intpValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int8(value int8) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int8Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int8s(value ...int8) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int8sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int8p(value *int8) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int8pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int16(value int16) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int16Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int16s(value ...int16) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int16sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int16p(value *int16) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int16pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int32(value int32) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int32Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int32s(value ...int32) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int32sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int32p(value *int32) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int32pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int64(value int64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int64Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int64s(value ...int64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int64sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Int64p(value *int64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: int64pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint(value uint) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uintValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uints(value ...uint) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uintsValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uintp(value *uint) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uintpValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint8(value uint8) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint8Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint8s(value ...uint8) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint8sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint8p(value *uint8) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint8pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint16(value uint16) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint16Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint16s(value ...uint16) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint16sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint16p(value *uint16) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint16pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint32(value uint32) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint32Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint32s(value ...uint32) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint32sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint32p(value *uint32) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint32pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint64(value uint64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint64Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint64s(value ...uint64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint64sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uint64p(value *uint64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uint64pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Float32(value float32) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: float32Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Float32s(value ...float32) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: float32sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Float32p(value *float32) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: float32pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Float64(value float64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: float64Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Float64s(value ...float64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: float64sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Float64p(value *float64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: float64pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Complex64(value complex64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: complex64Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Complex64s(value ...complex64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: complex64sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Complex64p(value *complex64) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: complex64pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Complex128(value complex128) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: complex128Value(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Complex128s(value []complex128) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: complex128sValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Complex128p(value *complex128) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: complex128pValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uintptr(value uintptr) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uintptrValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uintptrs(value ...uintptr) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uintptrsValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Uintptrp(value *uintptr) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: uintptrpValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Bytes(value []byte) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: bytesValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Dureation(value time.Duration) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: durationValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Dureations(value []time.Duration) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: durationsValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Dureationp(value *time.Duration) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: durationpValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Time(value time.Time) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: timeValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Times(value ...time.Time) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: timesValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Timep(value *time.Time) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: timepValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) FormatTime(format string, value time.Time) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: formatTimeValue(format, value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) FormatTimes(format string, value ...time.Time) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: formatTimesValue(format, value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) FormatTimep(format string, value *time.Time) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: formatTimepValue(format, value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Error(value error) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: errorValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (k Key) Errors(value ...error) Field {
|
||||||
|
return Field{
|
||||||
|
key: k,
|
||||||
|
value: errorsValue(value),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
package field
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
//go:generate stringer -type=Kind -linecomment -output=kind_string.go
|
|
||||||
|
|
||||||
type Kind int
|
|
||||||
|
|
||||||
const (
|
|
||||||
KindAny Kind = iota // any
|
|
||||||
KindArray // array
|
|
||||||
KindNil // nil
|
|
||||||
KindString // string
|
|
||||||
KindBool // bool
|
|
||||||
KindInt64 // int64
|
|
||||||
KindUint64 // uint64
|
|
||||||
KindFloat32 // float32
|
|
||||||
KindFloat64 // float64
|
|
||||||
KindComplex128 // complex128
|
|
||||||
KindBinary // bytes
|
|
||||||
KindDuration // duration
|
|
||||||
KindTime // time
|
|
||||||
KindError // error
|
|
||||||
KindGroup // group
|
|
||||||
KindClosure // closure
|
|
||||||
)
|
|
||||||
|
|
||||||
func (l Kind) MarshalJSON() ([]byte, error) {
|
|
||||||
return []byte("\"" + l.String() + "\""), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Kind) UnmarshalJSON(in []byte) error {
|
|
||||||
return l.UnmarshalText(in[1 : len(in)-1])
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l Kind) MarshalText() ([]byte, error) {
|
|
||||||
return []byte(l.String()), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:gocyclo,cyclop
|
|
||||||
func (l *Kind) UnmarshalText(in []byte) error {
|
|
||||||
switch string(in) {
|
|
||||||
case KindAny.String():
|
|
||||||
*l = KindAny
|
|
||||||
case KindArray.String():
|
|
||||||
*l = KindArray
|
|
||||||
case KindNil.String():
|
|
||||||
*l = KindNil
|
|
||||||
case KindString.String():
|
|
||||||
*l = KindString
|
|
||||||
case KindBool.String():
|
|
||||||
*l = KindBool
|
|
||||||
case KindInt64.String():
|
|
||||||
*l = KindInt64
|
|
||||||
case KindUint64.String():
|
|
||||||
*l = KindUint64
|
|
||||||
case KindFloat32.String():
|
|
||||||
*l = KindFloat32
|
|
||||||
case KindFloat64.String():
|
|
||||||
*l = KindFloat64
|
|
||||||
case KindComplex128.String():
|
|
||||||
*l = KindComplex128
|
|
||||||
case KindBinary.String():
|
|
||||||
*l = KindBinary
|
|
||||||
case KindDuration.String():
|
|
||||||
*l = KindDuration
|
|
||||||
case KindTime.String():
|
|
||||||
*l = KindTime
|
|
||||||
case KindError.String():
|
|
||||||
*l = KindError
|
|
||||||
case KindGroup.String():
|
|
||||||
*l = KindGroup
|
|
||||||
case KindClosure.String():
|
|
||||||
*l = KindClosure
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Errorf("%w:filed(%v)", ErrUndefined, string(in))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l Kind) MarshalBinary() ([]byte, error) {
|
|
||||||
return []byte(l.String()), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Kind) UnmarshalBinary(in []byte) error {
|
|
||||||
return l.UnmarshalText(in)
|
|
||||||
}
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
// Code generated by "stringer -type=Kind -linecomment -output=kind_string.go"; DO NOT EDIT.
|
|
||||||
|
|
||||||
package field
|
|
||||||
|
|
||||||
import "strconv"
|
|
||||||
|
|
||||||
func _() {
|
|
||||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
|
||||||
// Re-run the stringer command to generate them again.
|
|
||||||
var x [1]struct{}
|
|
||||||
_ = x[KindAny-0]
|
|
||||||
_ = x[KindArray-1]
|
|
||||||
_ = x[KindNil-2]
|
|
||||||
_ = x[KindString-3]
|
|
||||||
_ = x[KindBool-4]
|
|
||||||
_ = x[KindInt64-5]
|
|
||||||
_ = x[KindUint64-6]
|
|
||||||
_ = x[KindFloat32-7]
|
|
||||||
_ = x[KindFloat64-8]
|
|
||||||
_ = x[KindComplex128-9]
|
|
||||||
_ = x[KindBinary-10]
|
|
||||||
_ = x[KindDuration-11]
|
|
||||||
_ = x[KindTime-12]
|
|
||||||
_ = x[KindError-13]
|
|
||||||
_ = x[KindGroup-14]
|
|
||||||
_ = x[KindClosure-15]
|
|
||||||
}
|
|
||||||
|
|
||||||
const _Kind_name = "anyarraynilstringboolint64uint64float32float64complex128bytesdurationtimeerrorgroupclosure"
|
|
||||||
|
|
||||||
var _Kind_index = [...]uint8{0, 3, 8, 11, 17, 21, 26, 32, 39, 46, 56, 61, 69, 73, 78, 83, 90}
|
|
||||||
|
|
||||||
func (i Kind) String() string {
|
|
||||||
if i < 0 || i >= Kind(len(_Kind_index)-1) {
|
|
||||||
return "Kind(" + strconv.FormatInt(int64(i), 10) + ")"
|
|
||||||
}
|
|
||||||
return _Kind_name[_Kind_index[i]:_Kind_index[i+1]]
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
package field
|
|
||||||
|
|
||||||
type LogValuer interface {
|
|
||||||
LogValue() any
|
|
||||||
}
|
|
||||||
|
|
||||||
type ClosureFn func() any
|
|
||||||
|
|
||||||
func (v ClosureFn) LogValue() any {
|
|
||||||
return v()
|
|
||||||
}
|
|
||||||
@@ -1,112 +0,0 @@
|
|||||||
package field
|
|
||||||
|
|
||||||
import "unicode/utf8"
|
|
||||||
|
|
||||||
// Copied from encoding/json/tables.go.
|
|
||||||
//
|
|
||||||
// safeSet holds the value true if the ASCII character with the given array
|
|
||||||
// position can be represented inside a JSON string without any further
|
|
||||||
// escaping.
|
|
||||||
//
|
|
||||||
// All values are true except for the ASCII control characters (0-31), the
|
|
||||||
// double quote ("), and the backslash character ("\").
|
|
||||||
//
|
|
||||||
//nolint:gochecknoglobals
|
|
||||||
var safeSet = [utf8.RuneSelf]bool{
|
|
||||||
' ': true,
|
|
||||||
'!': true,
|
|
||||||
'"': false,
|
|
||||||
'#': true,
|
|
||||||
'$': true,
|
|
||||||
'%': true,
|
|
||||||
'&': true,
|
|
||||||
'\'': true,
|
|
||||||
'(': true,
|
|
||||||
')': true,
|
|
||||||
'*': true,
|
|
||||||
'+': true,
|
|
||||||
',': true,
|
|
||||||
'-': true,
|
|
||||||
'.': true,
|
|
||||||
'/': true,
|
|
||||||
'0': true,
|
|
||||||
'1': true,
|
|
||||||
'2': true,
|
|
||||||
'3': true,
|
|
||||||
'4': true,
|
|
||||||
'5': true,
|
|
||||||
'6': true,
|
|
||||||
'7': true,
|
|
||||||
'8': true,
|
|
||||||
'9': true,
|
|
||||||
':': true,
|
|
||||||
';': true,
|
|
||||||
'<': true,
|
|
||||||
'=': true,
|
|
||||||
'>': true,
|
|
||||||
'?': true,
|
|
||||||
'@': true,
|
|
||||||
'A': true,
|
|
||||||
'B': true,
|
|
||||||
'C': true,
|
|
||||||
'D': true,
|
|
||||||
'E': true,
|
|
||||||
'F': true,
|
|
||||||
'G': true,
|
|
||||||
'H': true,
|
|
||||||
'I': true,
|
|
||||||
'J': true,
|
|
||||||
'K': true,
|
|
||||||
'L': true,
|
|
||||||
'M': true,
|
|
||||||
'N': true,
|
|
||||||
'O': true,
|
|
||||||
'P': true,
|
|
||||||
'Q': true,
|
|
||||||
'R': true,
|
|
||||||
'S': true,
|
|
||||||
'T': true,
|
|
||||||
'U': true,
|
|
||||||
'V': true,
|
|
||||||
'W': true,
|
|
||||||
'X': true,
|
|
||||||
'Y': true,
|
|
||||||
'Z': true,
|
|
||||||
'[': true,
|
|
||||||
'\\': false,
|
|
||||||
']': true,
|
|
||||||
'^': true,
|
|
||||||
'_': true,
|
|
||||||
'`': true,
|
|
||||||
'a': true,
|
|
||||||
'b': true,
|
|
||||||
'c': true,
|
|
||||||
'd': true,
|
|
||||||
'e': true,
|
|
||||||
'f': true,
|
|
||||||
'g': true,
|
|
||||||
'h': true,
|
|
||||||
'i': true,
|
|
||||||
'j': true,
|
|
||||||
'k': true,
|
|
||||||
'l': true,
|
|
||||||
'm': true,
|
|
||||||
'n': true,
|
|
||||||
'o': true,
|
|
||||||
'p': true,
|
|
||||||
'q': true,
|
|
||||||
'r': true,
|
|
||||||
's': true,
|
|
||||||
't': true,
|
|
||||||
'u': true,
|
|
||||||
'v': true,
|
|
||||||
'w': true,
|
|
||||||
'x': true,
|
|
||||||
'y': true,
|
|
||||||
'z': true,
|
|
||||||
'{': true,
|
|
||||||
'|': true,
|
|
||||||
'}': true,
|
|
||||||
'~': true,
|
|
||||||
'\u007f': true,
|
|
||||||
}
|
|
||||||
126
field/type.go
Normal file
126
field/type.go
Normal file
@@ -0,0 +1,126 @@
|
|||||||
|
package field
|
||||||
|
|
||||||
|
type Type uint32
|
||||||
|
|
||||||
|
const (
|
||||||
|
TypeAny Type = 1 << iota // any
|
||||||
|
TypeArray // array
|
||||||
|
TypeNil // nil
|
||||||
|
TypeString // string
|
||||||
|
TypeBool // bool
|
||||||
|
TypeInt // int
|
||||||
|
TypeInt8 // int8
|
||||||
|
TypeInt16 // int16
|
||||||
|
TypeInt32 // int32
|
||||||
|
TypeInt64 // int64
|
||||||
|
TypeUint // uint
|
||||||
|
TypeUint8 // uint8
|
||||||
|
TypeUint16 // uint16
|
||||||
|
TypeUint32 // uint32
|
||||||
|
TypeUint64 // uint64
|
||||||
|
TypeFloat32 // float32
|
||||||
|
TypeFloat64 // float64
|
||||||
|
TypeComplex64 // complex64
|
||||||
|
TypeComplex128 // complex128
|
||||||
|
TypeUintptr // uintptr
|
||||||
|
TypeBinary // bytes
|
||||||
|
TypeDuration // duration
|
||||||
|
TypeTime // time
|
||||||
|
TypeError // error
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t Type) IsAny() bool {
|
||||||
|
return t&TypeAny > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsArray() bool {
|
||||||
|
return t&TypeArray > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsNil() bool {
|
||||||
|
return t&TypeNil > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsBool() bool {
|
||||||
|
return t&TypeBool > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsString() bool {
|
||||||
|
return t&TypeString > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsInt() bool {
|
||||||
|
return t&TypeInt > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsInt8() bool {
|
||||||
|
return t&TypeInt8 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsInt16() bool {
|
||||||
|
return t&TypeInt16 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsInt32() bool {
|
||||||
|
return t&TypeInt32 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsInt64() bool {
|
||||||
|
return t&TypeInt64 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsUint() bool {
|
||||||
|
return t&TypeUint > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsUint8() bool {
|
||||||
|
return t&TypeUint8 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsUint16() bool {
|
||||||
|
return t&TypeUint16 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsUint32() bool {
|
||||||
|
return t&TypeUint32 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsUint64() bool {
|
||||||
|
return t&TypeUint64 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsFloat32() bool {
|
||||||
|
return t&TypeFloat32 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsFloat64() bool {
|
||||||
|
return t&TypeFloat64 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsComplex64() bool {
|
||||||
|
return t&TypeComplex64 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsComplex128() bool {
|
||||||
|
return t&TypeComplex128 > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsUintptr() bool {
|
||||||
|
return t&TypeUintptr > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsBinary() bool {
|
||||||
|
return t&TypeBinary > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsDuration() bool {
|
||||||
|
return t&TypeDuration > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsTime() bool {
|
||||||
|
return t&TypeTime > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Type) IsError() bool {
|
||||||
|
return t&TypeError > 0
|
||||||
|
}
|
||||||
1397
field/value.go
1397
field/value.go
File diff suppressed because it is too large
Load Diff
@@ -10,8 +10,8 @@ import (
|
|||||||
|
|
||||||
//nolint:gochecknoglobals
|
//nolint:gochecknoglobals
|
||||||
var global = With(New(),
|
var global = With(New(),
|
||||||
WithCaller(KeySource, 1, false),
|
WithCaller("caller", 1, false),
|
||||||
WithLevel(KeyLevel, level.Debug),
|
WithLevel("level", level.Debug),
|
||||||
WithExit(level.Alert),
|
WithExit(level.Alert),
|
||||||
WithPanic(level.Emergency),
|
WithPanic(level.Emergency),
|
||||||
)
|
)
|
||||||
|
|||||||
21
go.mod
21
go.mod
@@ -1,3 +1,22 @@
|
|||||||
module gitoa.ru/go-4devs/log
|
module gitoa.ru/go-4devs/log
|
||||||
|
|
||||||
go 1.20
|
go 1.17
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/sirupsen/logrus v1.8.1
|
||||||
|
github.com/stretchr/testify v1.7.0
|
||||||
|
go.opentelemetry.io/otel v0.20.0
|
||||||
|
go.opentelemetry.io/otel/sdk v0.20.0
|
||||||
|
go.opentelemetry.io/otel/trace v0.20.0
|
||||||
|
go.uber.org/zap v1.21.0
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
go.opentelemetry.io/otel/metric v0.20.0 // indirect
|
||||||
|
go.uber.org/atomic v1.7.0 // indirect
|
||||||
|
go.uber.org/multierr v1.6.0 // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
||||||
|
)
|
||||||
|
|||||||
81
go.sum
81
go.sum
@@ -0,0 +1,81 @@
|
|||||||
|
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
|
||||||
|
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||||
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||||
|
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||||
|
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||||
|
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||||
|
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||||
|
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||||
|
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
|
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||||
|
go.opentelemetry.io/otel v0.20.0 h1:eaP0Fqu7SXHwvjiqDq83zImeehOHX8doTvU9AwXON8g=
|
||||||
|
go.opentelemetry.io/otel v0.20.0/go.mod h1:Y3ugLH2oa81t5QO+Lty+zXf8zC9L26ax4Nzoxm/dooo=
|
||||||
|
go.opentelemetry.io/otel/metric v0.20.0 h1:4kzhXFP+btKm4jwxpjIqjs41A7MakRFUS86bqLHTIw8=
|
||||||
|
go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU=
|
||||||
|
go.opentelemetry.io/otel/oteltest v0.20.0 h1:HiITxCawalo5vQzdHfKeZurV8x7ljcqAgiWzF6Vaeaw=
|
||||||
|
go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw=
|
||||||
|
go.opentelemetry.io/otel/sdk v0.20.0 h1:JsxtGXd06J8jrnya7fdI/U/MR6yXA5DtbZy+qoHQlr8=
|
||||||
|
go.opentelemetry.io/otel/sdk v0.20.0/go.mod h1:g/IcepuwNsoiX5Byy2nNV0ySUF1em498m7hBWC279Yc=
|
||||||
|
go.opentelemetry.io/otel/trace v0.20.0 h1:1DL6EXUdcg95gukhuRRvLDO/4X5THh/5dIV52lqtnbw=
|
||||||
|
go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw=
|
||||||
|
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||||
|
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||||
|
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723 h1:sHOAIxRGBp443oHZIPB+HsUGaksVCXVQENPxwTfQdH4=
|
||||||
|
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||||
|
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||||
|
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||||
|
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||||
|
go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI=
|
||||||
|
go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI=
|
||||||
|
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
|
||||||
|
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
|
||||||
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
|
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
|
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||||
|
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||||
|
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
|
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||||
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
|
||||||
|
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||||
|
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||||
|
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||||
|
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||||
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
44
handler/logrus/logger.go
Normal file
44
handler/logrus/logger.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package logrus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"gitoa.ru/go-4devs/log"
|
||||||
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
|
"gitoa.ru/go-4devs/log/level"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Standard create new standart logrus handler.
|
||||||
|
func Standard() log.Logger {
|
||||||
|
return New(logrus.StandardLogger())
|
||||||
|
}
|
||||||
|
|
||||||
|
// New create new logrus handler.
|
||||||
|
func New(log *logrus.Logger) log.Logger {
|
||||||
|
return func(ctx context.Context, data *entry.Entry) (int, error) {
|
||||||
|
lrgFields := make(logrus.Fields, data.Fields().Len())
|
||||||
|
for _, field := range data.Fields() {
|
||||||
|
lrgFields[string(field.Key())] = field.AsInterface()
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := log.WithContext(ctx).WithFields(lrgFields)
|
||||||
|
|
||||||
|
switch data.Level() {
|
||||||
|
case level.Emergency:
|
||||||
|
entry.Panic(data.Message())
|
||||||
|
case level.Alert:
|
||||||
|
entry.Fatal(data.Message())
|
||||||
|
case level.Critical, level.Error:
|
||||||
|
entry.Error(data.Message())
|
||||||
|
case level.Warning:
|
||||||
|
entry.Warn(data.Message())
|
||||||
|
case level.Notice, level.Info:
|
||||||
|
entry.Info(data.Message())
|
||||||
|
case level.Debug:
|
||||||
|
entry.Debug(data.Message())
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
38
handler/logrus/logger_test.go
Normal file
38
handler/logrus/logger_test.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package logrus_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
lgr "github.com/sirupsen/logrus"
|
||||||
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
|
"gitoa.ru/go-4devs/log/handler/logrus"
|
||||||
|
"gitoa.ru/go-4devs/log/level"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNew(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
|
||||||
|
lgrus := lgr.New()
|
||||||
|
lgrus.SetLevel(lgr.DebugLevel)
|
||||||
|
lgrus.SetOutput(buf)
|
||||||
|
lgrus.SetFormatter(&lgr.TextFormatter{
|
||||||
|
DisableTimestamp: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
handler := logrus.New(lgrus)
|
||||||
|
expect := "level=info msg=\"handle logrus message\"\n"
|
||||||
|
|
||||||
|
if _, err := handler(ctx, entry.New(entry.WithLevel(level.Info), entry.WithMessage("handle logrus message"))); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !strings.HasSuffix(buf.String(), expect) {
|
||||||
|
t.Errorf("invalid suffix\n got: %s\nexpect:%s\n", buf.String(), expect)
|
||||||
|
}
|
||||||
|
}
|
||||||
56
handler/otel/helpers.go
Normal file
56
handler/otel/helpers.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package otel
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
|
"gitoa.ru/go-4devs/log/level"
|
||||||
|
"go.opentelemetry.io/otel/attribute"
|
||||||
|
"go.opentelemetry.io/otel/trace"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
fieldSeverityNumber = "SeverityNumber"
|
||||||
|
fieldSeverityText = "SeverityText"
|
||||||
|
levelFields = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
func levels(lvl level.Level) Level {
|
||||||
|
switch lvl {
|
||||||
|
case level.Emergency:
|
||||||
|
return levelError3
|
||||||
|
case level.Alert:
|
||||||
|
return levelFatal
|
||||||
|
case level.Critical:
|
||||||
|
return levelError2
|
||||||
|
case level.Error:
|
||||||
|
return levelError
|
||||||
|
case level.Warning:
|
||||||
|
return levelWarn
|
||||||
|
case level.Notice:
|
||||||
|
return levelInfo2
|
||||||
|
case level.Info:
|
||||||
|
return levelInfo
|
||||||
|
case level.Debug:
|
||||||
|
return levelDebug
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func addEvent(ctx context.Context, data *entry.Entry) {
|
||||||
|
span := trace.SpanFromContext(ctx)
|
||||||
|
attrs := make([]attribute.KeyValue, 0, data.Fields().Len()+levelFields)
|
||||||
|
|
||||||
|
lvl := levels(data.Level())
|
||||||
|
attrs = append(attrs,
|
||||||
|
attribute.String(fieldSeverityText, lvl.String()),
|
||||||
|
attribute.Int(fieldSeverityNumber, int(lvl)),
|
||||||
|
)
|
||||||
|
|
||||||
|
for _, field := range data.Fields() {
|
||||||
|
attrs = append(attrs, attribute.String(string(field.Key()), field.Value().String()))
|
||||||
|
}
|
||||||
|
|
||||||
|
span.AddEvent(data.Message(), trace.WithAttributes(attrs...))
|
||||||
|
}
|
||||||
16
handler/otel/level.go
Normal file
16
handler/otel/level.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package otel
|
||||||
|
|
||||||
|
//go:generate stringer -type=Level -linecomment -output=level_string.go
|
||||||
|
|
||||||
|
type Level int
|
||||||
|
|
||||||
|
const (
|
||||||
|
levelDebug Level = 5 // DEBUG
|
||||||
|
levelInfo Level = 9 // INFO
|
||||||
|
levelInfo2 Level = 10 // INFO2
|
||||||
|
levelWarn Level = 13 // WARN
|
||||||
|
levelError Level = 17 // ERROR
|
||||||
|
levelError2 Level = 18 // ERROR2
|
||||||
|
levelError3 Level = 19 // ERROR3
|
||||||
|
levelFatal Level = 21 // FATAL
|
||||||
|
)
|
||||||
51
handler/otel/level_string.go
Normal file
51
handler/otel/level_string.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
// Code generated by "stringer -type=Level -linecomment -output=level_string.go"; DO NOT EDIT.
|
||||||
|
|
||||||
|
package otel
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
|
// Re-run the stringer command to generate them again.
|
||||||
|
var x [1]struct{}
|
||||||
|
_ = x[levelDebug-5]
|
||||||
|
_ = x[levelInfo-9]
|
||||||
|
_ = x[levelInfo2-10]
|
||||||
|
_ = x[levelWarn-13]
|
||||||
|
_ = x[levelError-17]
|
||||||
|
_ = x[levelError2-18]
|
||||||
|
_ = x[levelError3-19]
|
||||||
|
_ = x[levelFatal-21]
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
_Level_name_0 = "DEBUG"
|
||||||
|
_Level_name_1 = "INFOINFO2"
|
||||||
|
_Level_name_2 = "WARN"
|
||||||
|
_Level_name_3 = "ERRORERROR2ERROR3"
|
||||||
|
_Level_name_4 = "FATAL"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_Level_index_1 = [...]uint8{0, 4, 9}
|
||||||
|
_Level_index_3 = [...]uint8{0, 5, 11, 17}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (i Level) String() string {
|
||||||
|
switch {
|
||||||
|
case i == 5:
|
||||||
|
return _Level_name_0
|
||||||
|
case 9 <= i && i <= 10:
|
||||||
|
i -= 9
|
||||||
|
return _Level_name_1[_Level_index_1[i]:_Level_index_1[i+1]]
|
||||||
|
case i == 13:
|
||||||
|
return _Level_name_2
|
||||||
|
case 17 <= i && i <= 19:
|
||||||
|
i -= 17
|
||||||
|
return _Level_name_3[_Level_index_3[i]:_Level_index_3[i+1]]
|
||||||
|
case i == 21:
|
||||||
|
return _Level_name_4
|
||||||
|
default:
|
||||||
|
return "Level(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||||
|
}
|
||||||
|
}
|
||||||
16
handler/otel/logger.go
Normal file
16
handler/otel/logger.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package otel
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/log"
|
||||||
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
|
)
|
||||||
|
|
||||||
|
func New() log.Logger {
|
||||||
|
return func(ctx context.Context, e *entry.Entry) (int, error) {
|
||||||
|
addEvent(ctx, e)
|
||||||
|
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
16
handler/otel/middleware.go
Normal file
16
handler/otel/middleware.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package otel
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/log"
|
||||||
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Middleware() log.Middleware {
|
||||||
|
return func(ctx context.Context, e *entry.Entry, handler log.Logger) (int, error) {
|
||||||
|
addEvent(ctx, e)
|
||||||
|
|
||||||
|
return handler(ctx, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
63
handler/zap/logger.go
Normal file
63
handler/zap/logger.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
package zap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/log"
|
||||||
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
|
"gitoa.ru/go-4devs/log/level"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Nop() log.Logger {
|
||||||
|
return New(zap.NewNop())
|
||||||
|
}
|
||||||
|
|
||||||
|
func Example(options ...zap.Option) log.Logger {
|
||||||
|
return New(zap.NewExample(options...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Production(options ...zap.Option) log.Logger {
|
||||||
|
z, err := zap.NewProduction(options...)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return New(z)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Development(options ...zap.Option) log.Logger {
|
||||||
|
z, err := zap.NewDevelopment(options...)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return New(z)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New create handler by zap logger.
|
||||||
|
func New(logger *zap.Logger) log.Logger {
|
||||||
|
return func(ctx context.Context, data *entry.Entry) (int, error) {
|
||||||
|
zf := make([]zap.Field, data.Fields().Len())
|
||||||
|
for i, field := range data.Fields() {
|
||||||
|
zf[i] = zap.Any(string(field.Key()), field.AsInterface())
|
||||||
|
}
|
||||||
|
|
||||||
|
switch data.Level() {
|
||||||
|
case level.Emergency:
|
||||||
|
logger.Fatal(data.Message(), zf...)
|
||||||
|
case level.Alert:
|
||||||
|
logger.Panic(data.Message(), zf...)
|
||||||
|
case level.Critical, level.Error:
|
||||||
|
logger.Error(data.Message(), zf...)
|
||||||
|
case level.Warning:
|
||||||
|
logger.Warn(data.Message(), zf...)
|
||||||
|
case level.Notice, level.Info:
|
||||||
|
logger.Info(data.Message(), zf...)
|
||||||
|
case level.Debug:
|
||||||
|
logger.Debug(data.Message(), zf...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
43
handler/zap/logger_test.go
Normal file
43
handler/zap/logger_test.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package zap_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
|
"gitoa.ru/go-4devs/log/field"
|
||||||
|
zlog "gitoa.ru/go-4devs/log/handler/zap"
|
||||||
|
"gitoa.ru/go-4devs/log/level"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"go.uber.org/zap/zapcore"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNew(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
core := zapcore.NewCore(zapcore.NewJSONEncoder(zapcore.EncoderConfig{
|
||||||
|
MessageKey: "msg",
|
||||||
|
LevelKey: "level",
|
||||||
|
NameKey: "logger",
|
||||||
|
EncodeLevel: zapcore.LowercaseLevelEncoder,
|
||||||
|
EncodeTime: zapcore.ISO8601TimeEncoder,
|
||||||
|
EncodeDuration: zapcore.StringDurationEncoder,
|
||||||
|
}), zapcore.AddSync(buf), zapcore.DebugLevel)
|
||||||
|
logger := zlog.New(zap.New(core))
|
||||||
|
expect := `{"level":"info","msg":"handle zap message","env":"test"}` + "\n"
|
||||||
|
|
||||||
|
if _, err := logger(ctx, entry.New(
|
||||||
|
entry.WithFields(field.String("env", "test")),
|
||||||
|
entry.WithLevel(level.Notice),
|
||||||
|
entry.WithMessage("handle zap message"),
|
||||||
|
)); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if buf.String() != expect {
|
||||||
|
t.Errorf("invalid message\n got: %s\nexpect:%s\n", buf.String(), expect)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package buffer
|
|
||||||
|
|
||||||
import "sync"
|
|
||||||
|
|
||||||
const bufferSize = 1024
|
|
||||||
|
|
||||||
type Buffer []byte
|
|
||||||
|
|
||||||
// Having an initial size gives a dramatic speedup.
|
|
||||||
//
|
|
||||||
//nolint:gochecknoglobals
|
|
||||||
var bufPool = sync.Pool{
|
|
||||||
New: func() any {
|
|
||||||
b := make([]byte, 0, bufferSize)
|
|
||||||
|
|
||||||
return (*Buffer)(&b)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:forcetypeassert
|
|
||||||
func New() *Buffer {
|
|
||||||
return bufPool.Get().(*Buffer)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Buffer) Free() {
|
|
||||||
// To reduce peak allocation, return only smaller buffers to the pool.
|
|
||||||
const maxBufferSize = 16 << 10
|
|
||||||
if cap(*b) <= maxBufferSize {
|
|
||||||
*b = (*b)[:0]
|
|
||||||
bufPool.Put(b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Buffer) WriteString(s string) (int, error) {
|
|
||||||
*b = append(*b, s...)
|
|
||||||
|
|
||||||
return len(s), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Buffer) String() string {
|
|
||||||
return string(*b)
|
|
||||||
}
|
|
||||||
@@ -1,20 +1,16 @@
|
|||||||
package level
|
package level
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:generate stringer -type=Level -linecomment
|
//go:generate stringer -type=Level -linecomment
|
||||||
|
|
||||||
var (
|
var (
|
||||||
_ json.Marshaler = Level(0)
|
_ json.Marshaler = Level(0)
|
||||||
_ json.Unmarshaler = (*Level)(nil)
|
_ json.Unmarshaler = (*Level)(nil)
|
||||||
_ encoding.TextMarshaler = Level(0)
|
|
||||||
_ encoding.TextUnmarshaler = (*Level)(nil)
|
|
||||||
_ encoding.BinaryMarshaler = Level(0)
|
|
||||||
_ encoding.BinaryUnmarshaler = (*Level)(nil)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Level log.
|
// Level log.
|
||||||
@@ -22,9 +18,9 @@ type Level uint32
|
|||||||
|
|
||||||
// available log levels.
|
// available log levels.
|
||||||
const (
|
const (
|
||||||
Emergency Level = iota // emerg
|
Emergency Level = iota // emergency
|
||||||
Alert // alert
|
Alert // alert
|
||||||
Critical // crit
|
Critical // critical
|
||||||
Error // error
|
Error // error
|
||||||
Warning // warning
|
Warning // warning
|
||||||
Notice // notice
|
Notice // notice
|
||||||
@@ -32,6 +28,15 @@ const (
|
|||||||
Debug // debug
|
Debug // debug
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (l Level) MarshalJSON() ([]byte, error) {
|
||||||
|
b, err := json.Marshal(l.String())
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("marshal err: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (l Level) Is(level Level) bool {
|
func (l Level) Is(level Level) bool {
|
||||||
return level == l
|
return level == l
|
||||||
}
|
}
|
||||||
@@ -40,34 +45,13 @@ func (l Level) Enabled(level Level) bool {
|
|||||||
return l <= level
|
return l <= level
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Level) MarshalJSON() ([]byte, error) {
|
|
||||||
return []byte("\"" + l.String() + "\""), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Level) UnmarshalJSON(in []byte) error {
|
func (l *Level) UnmarshalJSON(in []byte) error {
|
||||||
lvl := Parse(string(in[1 : len(in)-1]))
|
var v string
|
||||||
*l = lvl
|
if err := json.Unmarshal(in, &v); err != nil {
|
||||||
|
return fmt.Errorf("unmarshal err: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
lvl := Parse(v)
|
||||||
}
|
|
||||||
|
|
||||||
func (l Level) MarshalText() ([]byte, error) {
|
|
||||||
return []byte(l.String()), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Level) UnmarshalText(in []byte) error {
|
|
||||||
lvl := Parse(string(in))
|
|
||||||
*l = lvl
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l Level) MarshalBinary() ([]byte, error) {
|
|
||||||
return []byte(l.String()), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Level) UnmarshalBinary(in []byte) error {
|
|
||||||
lvl := Parse(string(in))
|
|
||||||
*l = lvl
|
*l = lvl
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -81,11 +65,11 @@ func Parse(lvl string) Level {
|
|||||||
return Info
|
return Info
|
||||||
case "notice", "Notice", "NOTICE":
|
case "notice", "Notice", "NOTICE":
|
||||||
return Notice
|
return Notice
|
||||||
case "warning", "Warning", "WARNING", "warm", "Warm", "WARN":
|
case "warning", "Warning", "WARNING":
|
||||||
return Warning
|
return Warning
|
||||||
case "error", "Error", "ERROR", "err", "Err", "ERR":
|
case "error", "Error", "ERROR":
|
||||||
return Error
|
return Error
|
||||||
case "critical", "Critical", "CRITICAL", "crit", "Crit", "CRIT":
|
case "critical", "Critical", "CRITICAL":
|
||||||
return Critical
|
return Critical
|
||||||
case "alert", "Alert", "ALERT":
|
case "alert", "Alert", "ALERT":
|
||||||
return Alert
|
return Alert
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ func _() {
|
|||||||
_ = x[Debug-7]
|
_ = x[Debug-7]
|
||||||
}
|
}
|
||||||
|
|
||||||
const _Level_name = "emergalertcriterrorwarningnoticeinfodebug"
|
const _Level_name = "emergencyalertcriticalerrorwarningnoticeinfodebug"
|
||||||
|
|
||||||
var _Level_index = [...]uint8{0, 5, 10, 14, 19, 26, 32, 36, 41}
|
var _Level_index = [...]uint8{0, 9, 14, 22, 27, 34, 40, 44, 49}
|
||||||
|
|
||||||
func (i Level) String() string {
|
func (i Level) String() string {
|
||||||
if i >= Level(len(_Level_index)-1) {
|
if i >= Level(len(_Level_index)-1) {
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
package level_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/log/level"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestMarshalJSON(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
levels := map[level.Level]string{
|
|
||||||
level.Emergency: `"emerg"`,
|
|
||||||
level.Alert: `"alert"`,
|
|
||||||
level.Critical: `"crit"`,
|
|
||||||
level.Error: `"error"`,
|
|
||||||
level.Warning: `"warning"`,
|
|
||||||
level.Notice: `"notice"`,
|
|
||||||
level.Info: `"info"`,
|
|
||||||
level.Debug: `"debug"`,
|
|
||||||
}
|
|
||||||
|
|
||||||
for level, expect := range levels {
|
|
||||||
actual, err := level.MarshalJSON()
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("%s got err: %s", level, err)
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if string(actual) != expect {
|
|
||||||
t.Errorf("%s got: %s expect: %s", level, actual, expect)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUnmarshalJSON(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
levels := map[level.Level][]string{
|
|
||||||
level.Emergency: {`"emerg"`, `"Emerg"`},
|
|
||||||
level.Alert: {`"alert"`, `"ALERT"`},
|
|
||||||
level.Critical: {`"crit"`, `"critical"`},
|
|
||||||
level.Error: {`"error"`, `"ERR"`},
|
|
||||||
level.Warning: {`"warning"`, `"Warning"`},
|
|
||||||
level.Notice: {`"notice"`},
|
|
||||||
level.Info: {`"info"`},
|
|
||||||
level.Debug: {`"debug"`, `"DEBUG"`},
|
|
||||||
}
|
|
||||||
|
|
||||||
for expect, actuals := range levels {
|
|
||||||
for _, actual := range actuals {
|
|
||||||
var level level.Level
|
|
||||||
if err := level.UnmarshalJSON([]byte(actual)); err != nil {
|
|
||||||
t.Errorf("%s got err: %s", level, err)
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if !level.Is(expect) {
|
|
||||||
t.Errorf("%s got: %s expect: %s", actual, level, expect)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
19
logger.go
19
logger.go
@@ -2,6 +2,7 @@ package log
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@@ -13,7 +14,10 @@ import (
|
|||||||
|
|
||||||
var _ io.Writer = (Logger)(nil)
|
var _ io.Writer = (Logger)(nil)
|
||||||
|
|
||||||
const badKey = "!BADKEY"
|
var (
|
||||||
|
ErrIgnoredKey = errors.New("ignored key without a value")
|
||||||
|
ErrNonStringKeys = errors.New("ignored key-value pairs with non-string keys")
|
||||||
|
)
|
||||||
|
|
||||||
func writeOutput(_ int, err error) {
|
func writeOutput(_ int, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -48,7 +52,7 @@ func (l Logger) writef(ctx context.Context, level level.Level, format string, ar
|
|||||||
return l(ctx, data.SetLevel(level).SetMessagef(format, args...))
|
return l(ctx, data.SetLevel(level).SetMessagef(format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Logger) kv(_ context.Context, args ...interface{}) field.Fields {
|
func (l Logger) kv(ctx context.Context, args ...interface{}) field.Fields {
|
||||||
kvEntry := entry.Get()
|
kvEntry := entry.Get()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
@@ -63,20 +67,21 @@ func (l Logger) kv(_ context.Context, args ...interface{}) field.Fields {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if i == len(args)-1 {
|
if i == len(args)-1 {
|
||||||
kvEntry = kvEntry.AddAny(badKey, args[i])
|
writeOutput(l.write(ctx, level.Critical, fmt.Sprint("Ignored key without a value.", args[i]), kvEntry.Fields()...))
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
key, val := args[i], args[i+1]
|
i++
|
||||||
|
|
||||||
|
key, val := args[i-1], args[i]
|
||||||
if keyStr, ok := key.(string); ok {
|
if keyStr, ok := key.(string); ok {
|
||||||
kvEntry = kvEntry.AddAny(keyStr, val)
|
kvEntry = kvEntry.AddAny(keyStr, val)
|
||||||
i++
|
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
kvEntry = kvEntry.AddAny(badKey, args[i])
|
writeOutput(l.write(ctx, level.Critical, fmt.Sprint("Ignored key-value pairs with non-string keys.", key, val), kvEntry.Fields()...))
|
||||||
}
|
}
|
||||||
|
|
||||||
return kvEntry.Fields()
|
return kvEntry.Fields()
|
||||||
@@ -301,7 +306,7 @@ func (l Logger) Writer(ctx context.Context, level level.Level, fields ...field.F
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:containedctx
|
//nolint: containedctx
|
||||||
type writer struct {
|
type writer struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
level level.Level
|
level level.Level
|
||||||
|
|||||||
28
logger_example_logrus_test.go
Normal file
28
logger_example_logrus_test.go
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package log_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
slogrus "github.com/sirupsen/logrus"
|
||||||
|
"gitoa.ru/go-4devs/log/field"
|
||||||
|
"gitoa.ru/go-4devs/log/handler/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleNew_logrusHandler() {
|
||||||
|
lgrs := slogrus.New()
|
||||||
|
lgrs.SetOutput(os.Stdout)
|
||||||
|
lgrs.SetFormatter(&slogrus.TextFormatter{
|
||||||
|
DisableTimestamp: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
log := logrus.New(lgrs)
|
||||||
|
log.Err(ctx, "log logrus")
|
||||||
|
log.ErrKV(ctx, "log logrus kv", field.Int("int", 42))
|
||||||
|
log.ErrKVs(ctx, "log logrus kv sugar", "err", io.EOF)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// level=error msg="log logrus"
|
||||||
|
// level=error msg="log logrus kv" int=42
|
||||||
|
// level=error msg="log logrus kv sugar" err=EOF
|
||||||
|
}
|
||||||
@@ -2,12 +2,9 @@ package log_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"os"
|
"os"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/log"
|
"gitoa.ru/go-4devs/log"
|
||||||
"gitoa.ru/go-4devs/log/entry"
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
@@ -15,11 +12,12 @@ import (
|
|||||||
"gitoa.ru/go-4devs/log/level"
|
"gitoa.ru/go-4devs/log/level"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//nolint:gochecknoglobals
|
||||||
var ctx = context.Background()
|
var ctx = context.Background()
|
||||||
|
|
||||||
func setStdout() {
|
func setStdout() {
|
||||||
// set stout for example by default stderror
|
// set stout for example by default stderror
|
||||||
log.SetLogger(log.New(log.WithStdout()).With(log.WithLevel(log.KeyLevel, level.Debug)))
|
log.SetLogger(log.New(log.WithStdout()).With(log.WithLevel("level", level.Debug)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleNew() {
|
func ExampleNew() {
|
||||||
@@ -37,7 +35,7 @@ func ExampleInfo() {
|
|||||||
func ExampleErrKV() {
|
func ExampleErrKV() {
|
||||||
setStdout()
|
setStdout()
|
||||||
log.ErrKVs(ctx, "same message", "key", "addition value")
|
log.ErrKVs(ctx, "same message", "key", "addition value")
|
||||||
// Output: msg="same message" key="addition value" level=error
|
// Output: msg="same message" key=addition value level=error
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleNew_errf() {
|
func ExampleNew_errf() {
|
||||||
@@ -49,7 +47,7 @@ func ExampleNew_errf() {
|
|||||||
func ExampleNew_debugKV() {
|
func ExampleNew_debugKV() {
|
||||||
logger := log.New(log.WithStdout()).With(log.WithLevel("level", level.Debug))
|
logger := log.New(log.WithStdout()).With(log.WithLevel("level", level.Debug))
|
||||||
logger.DebugKVs(ctx, "same message", "error", os.ErrNotExist)
|
logger.DebugKVs(ctx, "same message", "error", os.ErrNotExist)
|
||||||
// Output: msg="same message" error="file does not exist" level=debug
|
// Output: msg="same message" error=file does not exist level=debug
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleNew_level() {
|
func ExampleNew_level() {
|
||||||
@@ -64,171 +62,28 @@ func ExampleNew_level_info() {
|
|||||||
// Output:
|
// Output:
|
||||||
}
|
}
|
||||||
|
|
||||||
type Obj struct {
|
|
||||||
Name string
|
|
||||||
IsEnable bool
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
obj = Obj{
|
|
||||||
Name: "test obj",
|
|
||||||
}
|
|
||||||
|
|
||||||
str = "test str"
|
|
||||||
boolsVal = true
|
|
||||||
intVal = int(math.MaxInt)
|
|
||||||
int8Val = int8(math.MaxInt8)
|
|
||||||
int16Val = int16(math.MaxInt16)
|
|
||||||
int32Val = int32(math.MaxInt32)
|
|
||||||
int64Val = int64(math.MaxInt64)
|
|
||||||
|
|
||||||
uintVal = uint(math.MaxUint)
|
|
||||||
uint8Val = uint8(math.MaxUint8)
|
|
||||||
uint16Val = uint16(math.MaxInt16)
|
|
||||||
uint32Val = uint32(math.MaxInt32)
|
|
||||||
uint64Val = uint64(math.MaxInt64)
|
|
||||||
|
|
||||||
float32Val = float32(math.MaxFloat32)
|
|
||||||
float64Val = float64(math.MaxFloat64)
|
|
||||||
|
|
||||||
minute = time.Minute
|
|
||||||
timeVal = time.Unix(0, math.MaxInt32)
|
|
||||||
)
|
|
||||||
|
|
||||||
func ExampleNew_anyField() {
|
|
||||||
logger := log.New(log.WithStdout(), log.WithJSONFormat())
|
|
||||||
logger.InfoKV(ctx, "any info message",
|
|
||||||
field.Any("obj", Obj{Name: "obj name"}),
|
|
||||||
field.Any("obj", &obj),
|
|
||||||
field.Any("int", intVal),
|
|
||||||
field.Any("uint", uintVal),
|
|
||||||
field.Any("float", float64Val),
|
|
||||||
field.Any("time", timeVal),
|
|
||||||
field.Any("duration", time.Hour),
|
|
||||||
field.Any("error", errors.New("error")),
|
|
||||||
)
|
|
||||||
// Output:
|
|
||||||
// {"msg":"any info message","obj":{"Name":"obj name","IsEnable":false},"obj":{"Name":"test obj","IsEnable":false},"int":9223372036854775807,"uint":18446744073709551615,"float":1.7976931348623157e+308,"time":"1970-01-01T03:00:02+03:00","duration":"1h0m0s","error":"error"}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleNew_arrayField() {
|
|
||||||
logger := log.New(log.WithStdout(), log.WithJSONFormat())
|
|
||||||
logger.InfoKV(ctx, "array info message",
|
|
||||||
field.Strings("strings", "string", str),
|
|
||||||
field.Bools("bools", true, false),
|
|
||||||
field.Ints("ints", 42, 24),
|
|
||||||
field.Int8s("int8s", 42, 24),
|
|
||||||
field.Int16s("int16s", 42, 24),
|
|
||||||
field.Int32s("int32s", 42, 24),
|
|
||||||
field.Int64s("int64s", 42, 24),
|
|
||||||
field.Uint8s("uint8s", uint8Val, 0),
|
|
||||||
field.Uint16s("uint16s", 42, 24),
|
|
||||||
field.Uint32s("uint32s", 42, 24),
|
|
||||||
field.Uint64s("uint64s", 42, 24),
|
|
||||||
field.Float32s("float32s", 42, 24),
|
|
||||||
field.Float64s("float64s", 42, 24),
|
|
||||||
field.Complex64s("complex64s", 42, 24),
|
|
||||||
field.Complex128s("complex128s", 42, 24),
|
|
||||||
field.Durations("durations", time.Minute, time.Second),
|
|
||||||
field.Times("times", time.Unix(0, 42), time.Unix(0, 24)),
|
|
||||||
field.Errors("errors", errors.New("error"), errors.New("error2")),
|
|
||||||
)
|
|
||||||
// Output:
|
|
||||||
// {"msg":"array info message","strings":["string","test str"],"bools":[true,false],"ints":[42,24],"int8s":[42,24],"int16s":[42,24],"int32s":[42,24],"int64s":[42,24],"uint8s":[255,0],"uint16s":[42,24],"uint32s":[42,24],"uint64s":[42,24],"float32s":[42,24],"float64s":[42,24],"complex64s":["(42+0i)","(24+0i)"],"complex128s":["(42+0i)","(24+0i)"],"durations":["1m0s","1s"],"times":["1970-01-01T03:00:00+03:00","1970-01-01T03:00:00+03:00"],"errors":["error","error2"]}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleNew_pointerField() {
|
|
||||||
logger := log.New(log.WithStdout(), log.WithJSONFormat())
|
|
||||||
logger.InfoKV(ctx, "pointer info message",
|
|
||||||
field.Stringp("stringp", &str),
|
|
||||||
field.Stringp("stringp", nil),
|
|
||||||
field.Boolp("boolp", &boolsVal),
|
|
||||||
field.Boolp("boolp", nil),
|
|
||||||
field.Intp("intp", &intVal),
|
|
||||||
field.Intp("intp", nil),
|
|
||||||
field.Int8p("int8p", &int8Val),
|
|
||||||
field.Int8p("int8p", nil),
|
|
||||||
field.Int16p("int16p", &int16Val),
|
|
||||||
field.Int16p("int16p", nil),
|
|
||||||
field.Int32p("int32p", &int32Val),
|
|
||||||
field.Int32p("int32p", nil),
|
|
||||||
field.Int64p("int64p", &int64Val),
|
|
||||||
field.Int64p("int64p", nil),
|
|
||||||
field.Uintp("uintp", &uintVal),
|
|
||||||
field.Uintp("uintp", nil),
|
|
||||||
field.Uint8p("uint8p", &uint8Val),
|
|
||||||
field.Uint8p("uint8p", nil),
|
|
||||||
field.Uint16p("uint16p", &uint16Val),
|
|
||||||
field.Uint16p("uint16p", nil),
|
|
||||||
field.Uint32p("uint32p", &uint32Val),
|
|
||||||
field.Uint32p("uint32p", nil),
|
|
||||||
field.Uint64p("uint64p", &uint64Val),
|
|
||||||
field.Uint64p("uint64p", nil),
|
|
||||||
field.Float32p("float32p", &float32Val),
|
|
||||||
field.Float32p("float32p", nil),
|
|
||||||
field.Float64p("float64p", &float64Val),
|
|
||||||
field.Float64p("float64p", nil),
|
|
||||||
field.Durationp("durationp", &minute),
|
|
||||||
field.Durationp("durationp", nil),
|
|
||||||
field.Timep("timep", &timeVal),
|
|
||||||
field.Timep("timep", nil),
|
|
||||||
)
|
|
||||||
// Output:
|
|
||||||
// {"msg":"pointer info message","stringp":"test str","stringp":null,"boolp":true,"boolp":null,"intp":9223372036854775807,"intp":null,"int8p":127,"int8p":null,"int16p":32767,"int16p":null,"int32p":2147483647,"int32p":null,"int64p":9223372036854775807,"int64p":null,"uintp":18446744073709551615,"uintp":null,"uint8p":255,"uint8p":null,"uint16p":32767,"uint16p":null,"uint32p":2147483647,"uint32p":null,"uint64p":9223372036854775807,"uint64p":null,"float32p":3.4028235e+38,"float32p":null,"float64p":1.7976931348623157e+308,"float64p":null,"durationp":"1m0s","durationp":null,"timep":"1970-01-01T03:00:02+03:00","timep":null}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleNew_fields() {
|
|
||||||
logger := log.New(log.WithStdout(), log.WithJSONFormat())
|
|
||||||
logger.InfoKV(ctx, "info message",
|
|
||||||
field.String("string", str),
|
|
||||||
field.Bool("bool", true),
|
|
||||||
field.Int("int", 42),
|
|
||||||
field.Int8("int8", 42),
|
|
||||||
field.Int16("int16", 42),
|
|
||||||
field.Int32("int32", 42),
|
|
||||||
field.Int64("int64", 42),
|
|
||||||
field.Uint8("uint8", uint8Val),
|
|
||||||
field.Uint16("uint16", 42),
|
|
||||||
field.Uint32("uint32", 42),
|
|
||||||
field.Uint64("uint64", 42),
|
|
||||||
field.Float32("float32", 42),
|
|
||||||
field.Float64("float64", 42),
|
|
||||||
field.Complex64("complex16", 42),
|
|
||||||
field.Complex128("complex128", 42),
|
|
||||||
field.Duration("duration", time.Minute),
|
|
||||||
field.Time("time", time.Unix(0, 42)),
|
|
||||||
field.FormatTime("format_time", time.UnixDate, timeVal),
|
|
||||||
field.Error("error", errors.New("error")),
|
|
||||||
)
|
|
||||||
// Output:
|
|
||||||
// {"msg":"info message","string":"test str","bool":true,"int":42,"int8":42,"int16":42,"int32":42,"int64":42,"uint8":255,"uint16":42,"uint32":42,"uint64":42,"float32":42,"float64":42,"complex16":"(42+0i)","complex128":"(42+0i)","duration":"1m0s","time":"1970-01-01T03:00:00+03:00","format_time":"Thu Jan 1 03:00:02 MSK 1970","error":"error"}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleNew_jsonFormat() {
|
func ExampleNew_jsonFormat() {
|
||||||
logger := log.New(log.WithStdout(), log.WithJSONFormat()).
|
logger := log.New(log.WithStdout(), log.WithJSONFormat()).
|
||||||
With(
|
With(
|
||||||
log.WithLevel(log.KeyLevel, level.Debug),
|
log.WithLevel("level", level.Debug),
|
||||||
log.GoVersion("go-version"),
|
log.GoVersion("go-version"),
|
||||||
)
|
)
|
||||||
logger.Err(ctx, "same error message")
|
logger.Err(ctx, "same error message")
|
||||||
logger.WarnKVs(ctx, "same warn message", "obj", Obj{Name: "obj name"})
|
// Output: {"go-version":"go1.17.8","level":"error","msg":"same error message"}
|
||||||
// Output:
|
|
||||||
// {"msg":"same error message","level":"error","go-version":"go1.21.5"}
|
|
||||||
// {"msg":"same warn message","obj":{"Name":"obj name","IsEnable":false},"level":"warning","go-version":"go1.21.5"}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleNew_textEncoding() {
|
func ExampleNew_textEncoding() {
|
||||||
logger := log.With(
|
logger := log.With(
|
||||||
log.New(log.WithStdout()),
|
log.New(log.WithStdout()),
|
||||||
log.WithLevel(log.KeyLevel, level.Debug),
|
log.WithLevel("level", level.Debug),
|
||||||
log.GoVersion("go-version"),
|
log.GoVersion("go-version"),
|
||||||
)
|
)
|
||||||
logger.Err(ctx, "same error message")
|
logger.Err(ctx, "same error message")
|
||||||
logger.InfoKVs(ctx, "same info message", "api-version", 0.1, "obj", Obj{Name: "text value", IsEnable: true})
|
logger.InfoKVs(ctx, "same info message", "api-version", 0.1)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// msg="same error message" level=error go-version=go1.21.5
|
// msg="same error message" level=error go-version=go1.17.8
|
||||||
// msg="same info message" api-version=0.1 obj={Name:text value IsEnable:true} level=info go-version=go1.21.5
|
// msg="same info message" api-version=0.1 level=info go-version=go1.17.8
|
||||||
}
|
}
|
||||||
|
|
||||||
type ctxKey string
|
type ctxKey string
|
||||||
@@ -250,7 +105,7 @@ func ExampleWith() {
|
|||||||
levelInfo, log.WithContextValue(requestID), log.KeyValue("api", "0.1.0"), log.GoVersion("go"),
|
levelInfo, log.WithContextValue(requestID), log.KeyValue("api", "0.1.0"), log.GoVersion("go"),
|
||||||
)
|
)
|
||||||
logger.Info(vctx, "same message")
|
logger.Info(vctx, "same message")
|
||||||
// Output: msg="same message" level=info requestID=6a5fa048-7181-11ea-bc55-0242ac130003 api=0.1.0 go=go1.21.5
|
// Output: msg="same message" level=info requestID=6a5fa048-7181-11ea-bc55-0242ac130003 api=0.1.0 go=go1.17.8
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Print() {
|
func ExampleLogger_Print() {
|
||||||
@@ -259,7 +114,7 @@ func ExampleLogger_Print() {
|
|||||||
levelInfo, log.KeyValue("client", "http"), log.KeyValue("api", "0.1.0"), log.GoVersion("go"),
|
levelInfo, log.KeyValue("client", "http"), log.KeyValue("api", "0.1.0"), log.GoVersion("go"),
|
||||||
)
|
)
|
||||||
logger.Print("same message")
|
logger.Print("same message")
|
||||||
// Output: msg="same message" level=info client=http api=0.1.0 go=go1.21.5
|
// Output: msg="same message" level=info client=http api=0.1.0 go=go1.17.8
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExamplePrint() {
|
func ExamplePrint() {
|
||||||
@@ -268,43 +123,22 @@ func ExamplePrint() {
|
|||||||
// Output: msg="same message" level=info
|
// Output: msg="same message" level=info
|
||||||
}
|
}
|
||||||
|
|
||||||
func Example_fieldClosureFn() {
|
func ExampleWithClosure() {
|
||||||
cnt := int32(0)
|
cnt := int32(0)
|
||||||
closure := field.ClosureFn(func() any {
|
closure := func() string {
|
||||||
d := fmt.Sprintf("additional error data: %d", cnt)
|
d := fmt.Sprintf("additional error data: %d", cnt)
|
||||||
atomic.AddInt32(&cnt, 1)
|
atomic.AddInt32(&cnt, 1)
|
||||||
|
|
||||||
return d
|
return d
|
||||||
})
|
}
|
||||||
|
|
||||||
log := log.With(log.New(log.WithStdout()), log.WithLevel("level", level.Info))
|
log := log.With(log.New(log.WithStdout()), log.WithLevel("level", level.Info), log.WithClosure)
|
||||||
|
|
||||||
log.DebugKVs(ctx, "debug message", "data", closure)
|
log.DebugKVs(ctx, "debug message", "data", closure)
|
||||||
log.ErrKVs(ctx, "error message", "err", closure)
|
log.ErrKVs(ctx, "error message", "err", closure)
|
||||||
log.WarnKVs(ctx, "warn message", "warn", closure)
|
log.WarnKVs(ctx, "warn message", "warn", closure)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// msg="error message" err="additional error data: 0" level=error
|
// msg="error message" err=additional error data: 0 level=error
|
||||||
// msg="warn message" warn="additional error data: 1" level=warning
|
// msg="warn message" warn=additional error data: 1 level=warning
|
||||||
}
|
|
||||||
|
|
||||||
func Example_withGroup() {
|
|
||||||
log := log.With(log.New(log.WithStdout()), log.WithLevel(log.KeyLevel, level.Info))
|
|
||||||
|
|
||||||
log.ErrKVs(ctx, "error message",
|
|
||||||
field.Groups("grous_field",
|
|
||||||
field.Error("err", os.ErrDeadlineExceeded),
|
|
||||||
field.Bool("bool", false),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
log.WarnKV(ctx, "error message", field.ValuerFn("valuer_field", func() any {
|
|
||||||
return field.Fields{
|
|
||||||
field.Int("int_value", math.MaxInt),
|
|
||||||
field.Uint8("uint8_value", math.MaxUint8),
|
|
||||||
}
|
|
||||||
}))
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// msg="error message" grous_field.err="i/o timeout" grous_field.bool=false level=error
|
|
||||||
// msg="error message" valuer_field.int_value=9223372036854775807 valuer_field.uint8_value=255 level=warning
|
|
||||||
}
|
}
|
||||||
|
|||||||
61
logger_example_trace_test.go
Normal file
61
logger_example_trace_test.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package log_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/log"
|
||||||
|
"gitoa.ru/go-4devs/log/field"
|
||||||
|
"gitoa.ru/go-4devs/log/handler/otel"
|
||||||
|
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||||
|
"go.opentelemetry.io/otel/trace"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleNew_withTrace() {
|
||||||
|
logger := log.New(log.WithStdout()).With(otel.Middleware())
|
||||||
|
|
||||||
|
sctx, span := startSpan(ctx)
|
||||||
|
|
||||||
|
logger.Err(sctx, "log logrus")
|
||||||
|
logger.ErrKV(sctx, "log logrus kv", field.Int("int", 42))
|
||||||
|
logger.ErrKVs(sctx, "log logrus kv sugar", "err", io.EOF)
|
||||||
|
|
||||||
|
span.End()
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// msg="log logrus"
|
||||||
|
// msg="log logrus kv" int=42
|
||||||
|
// msg="log logrus kv sugar" err=EOF
|
||||||
|
// event: log logrus, SeverityText = ERROR, SeverityNumber = 17
|
||||||
|
// event: log logrus kv, SeverityText = ERROR, SeverityNumber = 17, int = 42
|
||||||
|
// event: log logrus kv sugar, SeverityText = ERROR, SeverityNumber = 17, err = EOF
|
||||||
|
}
|
||||||
|
|
||||||
|
func startSpan(ctx context.Context) (context.Context, trace.Span) {
|
||||||
|
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter{}))
|
||||||
|
|
||||||
|
return tp.Tracer("logger").Start(ctx, "operation")
|
||||||
|
}
|
||||||
|
|
||||||
|
type exporter struct{}
|
||||||
|
|
||||||
|
func (e exporter) Shutdown(_ context.Context) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e exporter) ExportSpans(ctx context.Context, spanData []*sdktrace.SpanSnapshot) error {
|
||||||
|
for _, data := range spanData {
|
||||||
|
for _, events := range data.MessageEvents {
|
||||||
|
fmt.Print("event: ", events.Name)
|
||||||
|
|
||||||
|
for _, attr := range events.Attributes {
|
||||||
|
fmt.Printf(", %v = %v", attr.Key, attr.Value.AsInterface())
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
21
logger_example_zap_test.go
Normal file
21
logger_example_zap_test.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package log_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/log/field"
|
||||||
|
"gitoa.ru/go-4devs/log/handler/zap"
|
||||||
|
uzap "go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleNew_zapHandler() {
|
||||||
|
log := zap.New(uzap.NewExample())
|
||||||
|
log.Err(ctx, "log zap")
|
||||||
|
log.ErrKV(ctx, "log zap kv", field.Int("int", 42))
|
||||||
|
log.ErrKVs(ctx, "log zap kv sugar", "err", io.EOF)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// {"level":"error","msg":"log zap"}
|
||||||
|
// {"level":"error","msg":"log zap kv","int":42}
|
||||||
|
// {"level":"error","msg":"log zap kv sugar","err":"EOF"}
|
||||||
|
}
|
||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"gitoa.ru/go-4devs/log/level"
|
"gitoa.ru/go-4devs/log/level"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//nolint:gochecknoglobals
|
||||||
var requestID ctxKey = "requestID"
|
var requestID ctxKey = "requestID"
|
||||||
|
|
||||||
func TestFields(t *testing.T) {
|
func TestFields(t *testing.T) {
|
||||||
@@ -27,14 +28,14 @@ func TestFields(t *testing.T) {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
log := log.New(log.WithWriter(buf)).
|
log := log.New(log.WithWriter(buf)).
|
||||||
With(log.WithLevel("level", level.Info))
|
With(log.WithLevel("level", level.Info), log.WithClosure)
|
||||||
success := "msg=message err=\"file already exists\" version=0.1.0 obj={id:uid} closure=\"some closure data\" level=info\n"
|
success := "msg=\"message\" err=file already exists version=0.1.0 obj={id:uid} closure=some closure data level=info\n"
|
||||||
|
|
||||||
log.InfoKVs(ctx, "message",
|
log.InfoKVs(ctx, "message",
|
||||||
"err", os.ErrExist,
|
"err", os.ErrExist,
|
||||||
"version", "0.1.0",
|
"version", "0.1.0",
|
||||||
"obj", rObj{id: "uid"},
|
"obj", rObj{id: "uid"},
|
||||||
"closure", func() any {
|
"closure", func() string {
|
||||||
atomic.AddInt32(&cnt, 1)
|
atomic.AddInt32(&cnt, 1)
|
||||||
|
|
||||||
return "some closure data"
|
return "some closure data"
|
||||||
@@ -42,11 +43,11 @@ func TestFields(t *testing.T) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
log.DebugKVs(ctx, "debug message",
|
log.DebugKVs(ctx, "debug message",
|
||||||
"closure", field.ClosureFn(func() any {
|
"closure", func() string {
|
||||||
atomic.AddInt32(&cnt, 1)
|
atomic.AddInt32(&cnt, 1)
|
||||||
|
|
||||||
return "some debug data"
|
return "some debug data"
|
||||||
}),
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
if success != buf.String() {
|
if success != buf.String() {
|
||||||
@@ -63,7 +64,7 @@ func TestWriter(t *testing.T) {
|
|||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
success := "msg=\"info message\" err=\"file already exists\" requestID=6a5fa048-7181-11ea-bc55-0242ac1311113 level=info\n"
|
success := "msg=\"info message\" err=file already exists requestID=6a5fa048-7181-11ea-bc55-0242ac1311113 level=info\n"
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
logger := log.New(log.WithWriter(buf)).With(log.WithContextValue(requestID), log.WithLevel("level", level.Info))
|
logger := log.New(log.WithWriter(buf)).With(log.WithContextValue(requestID), log.WithLevel("level", level.Info))
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ import (
|
|||||||
"gitoa.ru/go-4devs/log/level"
|
"gitoa.ru/go-4devs/log/level"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ Middleware = WithClosure
|
||||||
|
|
||||||
// Middleware handle.
|
// Middleware handle.
|
||||||
type Middleware func(ctx context.Context, e *entry.Entry, handler Logger) (int, error)
|
type Middleware func(ctx context.Context, e *entry.Entry, handler Logger) (int, error)
|
||||||
|
|
||||||
@@ -60,6 +62,18 @@ func WithLevel(key string, lvl level.Level) Middleware {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithClosure(ctx context.Context, data *entry.Entry, handler Logger) (int, error) {
|
||||||
|
for i, field := range data.Fields() {
|
||||||
|
if field.Type().IsAny() {
|
||||||
|
if f, ok := field.AsInterface().(func() string); ok {
|
||||||
|
data.Fields().Set(i, field.Key().String(f()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return handler(ctx, data)
|
||||||
|
}
|
||||||
|
|
||||||
// KeyValue add field by const key value.
|
// KeyValue add field by const key value.
|
||||||
func KeyValue(key string, value interface{}) Middleware {
|
func KeyValue(key string, value interface{}) Middleware {
|
||||||
return func(ctx context.Context, e *entry.Entry, handler Logger) (int, error) {
|
return func(ctx context.Context, e *entry.Entry, handler Logger) (int, error) {
|
||||||
@@ -97,7 +111,7 @@ func WithCaller(key string, depth int, full bool) Middleware {
|
|||||||
// WithTime adds time.
|
// WithTime adds time.
|
||||||
func WithTime(key, format string) Middleware {
|
func WithTime(key, format string) Middleware {
|
||||||
return func(ctx context.Context, e *entry.Entry, handler Logger) (int, error) {
|
return func(ctx context.Context, e *entry.Entry, handler Logger) (int, error) {
|
||||||
return handler(ctx, e.Add(field.FormatTime(key, format, time.Now())))
|
return handler(ctx, e.Add(field.Time(key, time.Now())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
168
writter.go
168
writter.go
@@ -1,129 +1,119 @@
|
|||||||
package log
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/log/entry"
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
"gitoa.ru/go-4devs/log/field"
|
|
||||||
"gitoa.ru/go-4devs/log/internal/buffer"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Keys for "built-in" attributes.
|
// New creates standart logger.
|
||||||
const (
|
func New(opts ...Option) Logger {
|
||||||
// TimeKey is the key used by the built-in handlers for the time
|
logger := log{e: stringFormat(), w: os.Stderr}
|
||||||
// when the log method is called. The associated Value is a [time.Time].
|
|
||||||
KeyTime = "time"
|
|
||||||
// LevelKey is the key used by the built-in handlers for the level
|
|
||||||
// of the log call. The associated value is a [Level].
|
|
||||||
KeyLevel = "level"
|
|
||||||
// MessageKey is the key used by the built-in handlers for the
|
|
||||||
// message of the log call. The associated value is a string.
|
|
||||||
KeyMessage = "msg"
|
|
||||||
// SourceKey is the key used by the built-in handlers for the source file
|
|
||||||
// and line of the log call. The associated value is a string.
|
|
||||||
KeySource = "source"
|
|
||||||
)
|
|
||||||
|
|
||||||
func WithWriter(w io.Writer) func(*option) {
|
for _, opt := range opts {
|
||||||
return func(o *option) {
|
opt(&logger)
|
||||||
o.out = w
|
}
|
||||||
|
|
||||||
|
return func(_ context.Context, entry *entry.Entry) (int, error) {
|
||||||
|
b, err := logger.e(entry)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("enode err: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
n, err := logger.w.Write(b)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("failed write: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return n, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithStdout() func(*option) {
|
// Option configure log.
|
||||||
return func(o *option) {
|
type Option func(*log)
|
||||||
o.out = os.Stdout
|
|
||||||
|
// Encode sets formats and encode output message.
|
||||||
|
type Encode func(*entry.Entry) ([]byte, error)
|
||||||
|
|
||||||
|
type log struct {
|
||||||
|
w io.Writer
|
||||||
|
e Encode
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithWriter sets writer logger.
|
||||||
|
func WithWriter(writer io.Writer) Option {
|
||||||
|
return func(l *log) {
|
||||||
|
l.w = writer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithStdout sets logged to os.Stdout.
|
||||||
|
func WithStdout() Option {
|
||||||
|
return WithWriter(os.Stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithEncode sets format log.
|
||||||
|
func WithEncode(e Encode) Option {
|
||||||
|
return func(l *log) {
|
||||||
|
l.e = e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithStringFormat sets format as simple string.
|
// WithStringFormat sets format as simple string.
|
||||||
func WithStringFormat() func(*option) {
|
func WithStringFormat() Option {
|
||||||
return func(o *option) {
|
return WithEncode(stringFormat())
|
||||||
o.format = formatText()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithJSONFormat sets json output format.
|
// WithJSONFormat sets json output format.
|
||||||
func WithJSONFormat() func(*option) {
|
func WithJSONFormat() Option {
|
||||||
return func(o *option) {
|
return WithEncode(jsonFormat)
|
||||||
o.format = formatJSON()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type option struct {
|
//nolint: forcetypeassert
|
||||||
format func(io.Writer, *entry.Entry) (int, error)
|
func stringFormat() func(entry *entry.Entry) ([]byte, error) {
|
||||||
out io.Writer
|
pool := sync.Pool{
|
||||||
}
|
New: func() interface{} {
|
||||||
|
return &bytes.Buffer{}
|
||||||
// New creates standart logger.
|
},
|
||||||
func New(opts ...func(*option)) Logger {
|
|
||||||
log := option{
|
|
||||||
format: formatText(),
|
|
||||||
out: os.Stderr,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
return func(entry *entry.Entry) ([]byte, error) {
|
||||||
opt(&log)
|
buf := pool.Get().(*bytes.Buffer)
|
||||||
}
|
buf.Reset()
|
||||||
|
|
||||||
return func(_ context.Context, entry *entry.Entry) (int, error) {
|
|
||||||
return log.format(log.out, entry)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func formatText() func(io.Writer, *entry.Entry) (int, error) {
|
|
||||||
enc := field.NewEncoderText()
|
|
||||||
|
|
||||||
return func(w io.Writer, entry *entry.Entry) (int, error) {
|
|
||||||
buf := buffer.New()
|
|
||||||
defer func() {
|
defer func() {
|
||||||
buf.Free()
|
pool.Put(buf)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
*buf = enc.AppendField(*buf, field.String(KeyMessage, entry.Message()))
|
buf.WriteString("msg=\"")
|
||||||
|
buf.WriteString(strings.TrimSpace(entry.Message()))
|
||||||
|
buf.WriteString("\"")
|
||||||
|
|
||||||
for _, field := range entry.Fields() {
|
for _, field := range entry.Fields() {
|
||||||
*buf = enc.AppendField(*buf, field)
|
buf.WriteString(" ")
|
||||||
|
buf.WriteString(string(field.Key()))
|
||||||
|
buf.WriteString("=")
|
||||||
|
buf.WriteString(field.Value().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _ = buf.WriteString("\n")
|
buf.WriteString("\n")
|
||||||
|
|
||||||
n, err := w.Write(*buf)
|
return buf.Bytes(), nil
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("format text:%w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return n, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatJSON() func(w io.Writer, entry *entry.Entry) (int, error) {
|
func jsonFormat(entry *entry.Entry) ([]byte, error) {
|
||||||
enc := field.NewEncoderJSON()
|
res, err := json.Marshal(entry.AddString("msg", entry.Message()).Fields().AsMap())
|
||||||
|
if err != nil {
|
||||||
return func(w io.Writer, entry *entry.Entry) (int, error) {
|
return nil, fmt.Errorf("marshal err: %w", err)
|
||||||
buf := buffer.New()
|
|
||||||
defer func() {
|
|
||||||
buf.Free()
|
|
||||||
}()
|
|
||||||
|
|
||||||
_, _ = buf.WriteString("{")
|
|
||||||
*buf = enc.AppendField(*buf, field.String(KeyMessage, entry.Message()))
|
|
||||||
|
|
||||||
for _, field := range entry.Fields() {
|
|
||||||
*buf = enc.AppendField(*buf, field)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _ = buf.WriteString("}")
|
|
||||||
_, _ = buf.WriteString("\n")
|
|
||||||
|
|
||||||
n, err := w.Write(*buf)
|
|
||||||
if err != nil {
|
|
||||||
return 0, fmt.Errorf("format json:%w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return n, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return append(res, []byte("\n")...), nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user