Compare commits
1 Commits
master
...
b35976ce7b
| Author | SHA1 | Date | |
|---|---|---|---|
| b35976ce7b |
@@ -1,9 +1,9 @@
|
|||||||
name: Go Action
|
name: Go Lint
|
||||||
|
|
||||||
on: [push, pull_request]
|
on: [push, pull_request]
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
goaction:
|
lint:
|
||||||
runs-on: ubuntu-latest # Use a Gitea Actions runner label
|
runs-on: ubuntu-latest # Use a Gitea Actions runner label
|
||||||
steps:
|
steps:
|
||||||
- name: Check out repository code
|
- name: Check out repository code
|
||||||
@@ -12,15 +12,12 @@ jobs:
|
|||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v5 # Action to install a specific Go version
|
uses: actions/setup-go@v5 # Action to install a specific Go version
|
||||||
with:
|
with:
|
||||||
go-version: '1.25.5' # Specify your required Go version
|
go-version: '1.22' # Specify your required Go version
|
||||||
|
|
||||||
- name: Run golangci-lint
|
- name: Run golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
|
uses: golangci/golangci-lint-action@v3 # Use the golangci-lint action
|
||||||
with:
|
with:
|
||||||
version: v2.7.2 # Specify the linter version
|
version: v1.54.0 # Specify the linter version
|
||||||
# Optional: additional arguments
|
# Optional: additional arguments
|
||||||
args: --verbose
|
args: --verbose
|
||||||
|
|
||||||
- name: Run go test
|
|
||||||
run: go test ./...
|
|
||||||
|
|
||||||
127
.golangci.yml
127
.golangci.yml
@@ -1,30 +1,9 @@
|
|||||||
version: "2"
|
run:
|
||||||
linters:
|
timeout: 5m
|
||||||
default: all
|
|
||||||
disable:
|
|
||||||
- wsl
|
|
||||||
- noinlineerr
|
|
||||||
settings:
|
|
||||||
depguard:
|
|
||||||
rules:
|
|
||||||
main:
|
|
||||||
allow:
|
|
||||||
- $gostd
|
|
||||||
- gitoa.ru
|
|
||||||
|
|
||||||
funcorder:
|
linters-settings:
|
||||||
constructor: false
|
|
||||||
recvcheck:
|
|
||||||
disable-builtin: false
|
|
||||||
exclusions:
|
|
||||||
- "*.String"
|
|
||||||
- "*.UnmarshalText"
|
|
||||||
- "*.UnmarshalJSON"
|
|
||||||
- "*.UnmarshalBinary"
|
|
||||||
dupl:
|
dupl:
|
||||||
threshold: 100
|
threshold: 100
|
||||||
exhaustive:
|
|
||||||
default-signifies-exhaustive: true
|
|
||||||
funlen:
|
funlen:
|
||||||
lines: 100
|
lines: 100
|
||||||
statements: 50
|
statements: 50
|
||||||
@@ -33,61 +12,65 @@ linters:
|
|||||||
min-occurrences: 2
|
min-occurrences: 2
|
||||||
gocyclo:
|
gocyclo:
|
||||||
min-complexity: 15
|
min-complexity: 15
|
||||||
|
govet:
|
||||||
|
check-shadowing: true
|
||||||
lll:
|
lll:
|
||||||
line-length: 140
|
line-length: 140
|
||||||
|
fieldalignment:
|
||||||
|
suggest-new: true
|
||||||
misspell:
|
misspell:
|
||||||
locale: US
|
locale: US
|
||||||
tagliatelle:
|
exhaustive:
|
||||||
case:
|
default-signifies-exhaustive: true
|
||||||
rules:
|
|
||||||
avro: snake
|
|
||||||
bson: camel
|
|
||||||
json: snake
|
|
||||||
xml: camel
|
|
||||||
yaml: camel
|
|
||||||
use-field-name: true
|
|
||||||
varnamelen:
|
varnamelen:
|
||||||
min-name-length: 2
|
min-name-length: 2
|
||||||
ignore-names:
|
ignore-names:
|
||||||
- err
|
- err
|
||||||
- "n"
|
- n
|
||||||
- i
|
- i
|
||||||
- w
|
- w
|
||||||
exclusions:
|
tagliatelle:
|
||||||
generated: lax
|
case:
|
||||||
presets:
|
use-field-name: true
|
||||||
- comments
|
rules:
|
||||||
- common-false-positives
|
json: snake
|
||||||
- legacy
|
yaml: camel
|
||||||
- std-error-handling
|
xml: camel
|
||||||
rules:
|
bson: camel
|
||||||
- linters:
|
avro: snake
|
||||||
- exhaustruct
|
|
||||||
- gochecknoglobals
|
linters:
|
||||||
- ireturn
|
enable-all: true
|
||||||
- mnd
|
disable:
|
||||||
path: _test\.go
|
# deprecated
|
||||||
- linters:
|
- interfacer
|
||||||
- err113
|
- structcheck
|
||||||
- lll
|
- varcheck
|
||||||
path: _example_test\.go
|
- golint
|
||||||
- linters:
|
- deadcode
|
||||||
- lll
|
- scopelint
|
||||||
- mnd
|
- exhaustivestruct
|
||||||
path: example/*
|
- ifshort
|
||||||
paths:
|
- nosnakecase
|
||||||
- third_party$
|
- maligned
|
||||||
- builtin$
|
|
||||||
- examples$
|
- depguard # need configure
|
||||||
formatters:
|
- nolintlint # use with space
|
||||||
enable:
|
|
||||||
- gci
|
issues:
|
||||||
- gofmt
|
# Excluding configuration per-path, per-linter, per-text and per-source
|
||||||
- gofumpt
|
exclude-rules:
|
||||||
- goimports
|
- path: _test\.go
|
||||||
exclusions:
|
linters:
|
||||||
generated: lax
|
- gomnd
|
||||||
paths:
|
- ireturn
|
||||||
- third_party$
|
- exhaustruct
|
||||||
- builtin$
|
- gochecknoglobals
|
||||||
- examples$
|
- path: _example_test\.go
|
||||||
|
linters:
|
||||||
|
- lll
|
||||||
|
- goerr113
|
||||||
|
- path: example/*
|
||||||
|
linters:
|
||||||
|
- gomnd
|
||||||
|
- lll
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# log
|
# log
|
||||||
|
|
||||||

|
[](https://drone.gitoa.ru/go-4devs/log)
|
||||||
[](https://goreportcard.com/report/gitoa.ru/go-4devs/log)
|
[](https://goreportcard.com/report/gitoa.ru/go-4devs/log)
|
||||||
[](http://godoc.org/gitoa.ru/go-4devs/log)
|
[](http://godoc.org/gitoa.ru/go-4devs/log)
|
||||||
|
|||||||
@@ -69,10 +69,10 @@ func getMessage(iter int) string {
|
|||||||
return _messages[iter%1000]
|
return _messages[iter%1000]
|
||||||
}
|
}
|
||||||
|
|
||||||
func fakeFmtArgs() []any {
|
func fakeFmtArgs() []interface{} {
|
||||||
// Need to keep this a function instead of a package-global var so that we
|
// Need to keep this a function instead of a package-global var so that we
|
||||||
// pay the cast-to-interface{} penalty on each call.
|
// pay the cast-to-interface{} penalty on each call.
|
||||||
return []any{
|
return []interface{}{
|
||||||
_tenInts[0],
|
_tenInts[0],
|
||||||
_tenInts,
|
_tenInts,
|
||||||
_tenStrings[0],
|
_tenStrings[0],
|
||||||
@@ -101,8 +101,8 @@ func fakeFields() []field.Field {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fakeSugarFields() []any {
|
func fakeSugarFields() []interface{} {
|
||||||
return []any{
|
return []interface{}{
|
||||||
"int", _tenInts[0],
|
"int", _tenInts[0],
|
||||||
"ints", _tenInts,
|
"ints", _tenInts,
|
||||||
"string", _tenStrings[0],
|
"string", _tenStrings[0],
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
func Caller(depth int, full bool) string {
|
func Caller(depth int, full bool) string {
|
||||||
const offset = 3
|
const offset = 3
|
||||||
|
|
||||||
_, file, line, has := runtime.Caller(depth + offset)
|
_, file, line, has := runtime.Caller(depth + offset)
|
||||||
|
|
||||||
if !has {
|
if !has {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func WithMessage(msg string) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithMessagef(format string, args ...any) Option {
|
func WithMessagef(format string, args ...interface{}) Option {
|
||||||
return func(e *Entry) {
|
return func(e *Entry) {
|
||||||
e.format = format
|
e.format = format
|
||||||
e.args = args
|
e.args = args
|
||||||
@@ -50,7 +50,7 @@ func New(opts ...Option) *Entry {
|
|||||||
fields: make(field.Fields, 0, defaultCap+1),
|
fields: make(field.Fields, 0, defaultCap+1),
|
||||||
level: level.Debug,
|
level: level.Debug,
|
||||||
format: "",
|
format: "",
|
||||||
args: make([]any, 0, defaultCap+1),
|
args: make([]interface{}, 0, defaultCap+1),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
@@ -63,7 +63,7 @@ func New(opts ...Option) *Entry {
|
|||||||
// Entry slice field.
|
// Entry slice field.
|
||||||
type Entry struct {
|
type Entry struct {
|
||||||
format string
|
format string
|
||||||
args []any
|
args []interface{}
|
||||||
level level.Level
|
level level.Level
|
||||||
fields field.Fields
|
fields field.Fields
|
||||||
}
|
}
|
||||||
@@ -133,7 +133,7 @@ func (e *Entry) SetMessage(msg string) *Entry {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Entry) SetMessagef(format string, args ...any) *Entry {
|
func (e *Entry) SetMessagef(format string, args ...interface{}) *Entry {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return New().SetMessagef(format, args...)
|
return New().SetMessagef(format, args...)
|
||||||
}
|
}
|
||||||
@@ -154,7 +154,7 @@ func (e *Entry) Add(fields ...field.Field) *Entry {
|
|||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Entry) AddAny(key string, value any) *Entry {
|
func (e *Entry) AddAny(key string, value interface{}) *Entry {
|
||||||
return e.Add(field.Any(key, value))
|
return e.Add(field.Any(key, value))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import "sync"
|
|||||||
|
|
||||||
//nolint:gochecknoglobals
|
//nolint:gochecknoglobals
|
||||||
var pool = sync.Pool{
|
var pool = sync.Pool{
|
||||||
New: func() any {
|
New: func() interface{} {
|
||||||
return New()
|
return New()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
2
field.go
2
field.go
@@ -5,7 +5,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Field create field.
|
// Field create field.
|
||||||
func Field(key string, value any) field.Field {
|
func Field(key string, value interface{}) field.Field {
|
||||||
return field.Any(key, value)
|
return field.Any(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
118
field/encoder.go
118
field/encoder.go
@@ -1,3 +1,4 @@
|
|||||||
|
//nolint:gomnd
|
||||||
package field
|
package field
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -103,6 +104,46 @@ func (b BaseEncoder) AppendDelimiter(dst []byte, deli byte) []byte {
|
|||||||
return append(dst, deli)
|
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 {
|
func (b BaseEncoder) AppendDuration(dst []byte, d time.Duration) []byte {
|
||||||
return b.AppendString(dst, d.String())
|
return b.AppendString(dst, d.String())
|
||||||
}
|
}
|
||||||
@@ -163,6 +204,12 @@ func (b BaseEncoder) AppendField(dst []byte, field Field) []byte {
|
|||||||
return b.appendField(dst, field, prefix, b.delimeter)
|
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 {
|
func (b BaseEncoder) AppendKey(dst []byte, key string, prefix string) []byte {
|
||||||
if prefix != "" {
|
if prefix != "" {
|
||||||
dst = append(dst, prefix...)
|
dst = append(dst, prefix...)
|
||||||
@@ -171,7 +218,6 @@ func (b BaseEncoder) AppendKey(dst []byte, key string, prefix string) []byte {
|
|||||||
return b.AppendString(dst, key)
|
return b.AppendString(dst, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:mnd
|
|
||||||
func (b BaseEncoder) AppendComplex(dst []byte, c complex128) []byte {
|
func (b BaseEncoder) AppendComplex(dst []byte, c complex128) []byte {
|
||||||
cmplx := strconv.FormatComplex(c, 'g', -1, 128)
|
cmplx := strconv.FormatComplex(c, 'g', -1, 128)
|
||||||
|
|
||||||
@@ -182,7 +228,6 @@ func (b BaseEncoder) AppendFloat(dst []byte, f float64, bitSize int) []byte {
|
|||||||
return strconv.AppendFloat(dst, f, 'g', -1, bitSize)
|
return strconv.AppendFloat(dst, f, 'g', -1, bitSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:mnd
|
|
||||||
func (b BaseEncoder) AppendUint(dst []byte, u uint64) []byte {
|
func (b BaseEncoder) AppendUint(dst []byte, u uint64) []byte {
|
||||||
return strconv.AppendUint(dst, u, 10)
|
return strconv.AppendUint(dst, u, 10)
|
||||||
}
|
}
|
||||||
@@ -191,7 +236,6 @@ func (b BaseEncoder) AppendNull(dst []byte) []byte {
|
|||||||
return append(dst, b.nullValue...)
|
return append(dst, b.nullValue...)
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:mnd
|
|
||||||
func (b BaseEncoder) AppendInt(dst []byte, val int64) []byte {
|
func (b BaseEncoder) AppendInt(dst []byte, val int64) []byte {
|
||||||
return strconv.AppendInt(dst, val, 10)
|
return strconv.AppendInt(dst, val, 10)
|
||||||
}
|
}
|
||||||
@@ -207,6 +251,17 @@ func (b BaseEncoder) AppendGroup(dst []byte, fields []Field) []byte {
|
|||||||
return append(dst, b.group.end)
|
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 {
|
func (b BaseEncoder) AppendArray(dst []byte, in []Value) []byte {
|
||||||
dst = append(dst, b.array.start)
|
dst = append(dst, b.array.start)
|
||||||
if len(in) > 0 {
|
if len(in) > 0 {
|
||||||
@@ -225,60 +280,3 @@ func (b BaseEncoder) AppendBytes(dst, in []byte) []byte {
|
|||||||
|
|
||||||
return append(dst, '"')
|
return append(dst, '"')
|
||||||
}
|
}
|
||||||
|
|
||||||
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) 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:mnd,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)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ func TestEncoderJSONAppendField_string(t *testing.T) {
|
|||||||
encode := field.NewEncoderJSON()
|
encode := field.NewEncoderJSON()
|
||||||
|
|
||||||
buf := buffer.New()
|
buf := buffer.New()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
buf.Free()
|
buf.Free()
|
||||||
}()
|
}()
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package field
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"slices"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -503,6 +502,13 @@ func (f Field) String() string {
|
|||||||
return fmt.Sprintf("%s=%+v", f.Key, f.Value)
|
return fmt.Sprintf("%s=%+v", f.Key, f.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String implent stringer.
|
||||||
func (f Field) IsKey(keys ...string) bool {
|
func (f Field) IsKey(keys ...string) bool {
|
||||||
return slices.Contains(keys, f.Key)
|
for _, key := range keys {
|
||||||
|
if key == f.Key {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//nolint:exhaustruct
|
// nolint: exhaustruct
|
||||||
package field
|
package field
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -27,7 +27,7 @@ func StringpValue(value *string) Value {
|
|||||||
return StringValue(*value)
|
return StringValue(*value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringsValue returns a new Value for a string.
|
// StringpValue returns a new Value for a string.
|
||||||
func StringsValue(value []string) Value {
|
func StringsValue(value []string) Value {
|
||||||
return Value{
|
return Value{
|
||||||
Kind: KindArray,
|
Kind: KindArray,
|
||||||
@@ -115,7 +115,7 @@ func Uint8sValue(values []uint8) Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uint64pValue returns a Value for a []uint64.
|
// Uint64sValue returns a Value for a []uint64.
|
||||||
func Uint64pValue(v *uint64) Value {
|
func Uint64pValue(v *uint64) Value {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return NilValue()
|
return NilValue()
|
||||||
@@ -145,7 +145,7 @@ func Int64sValue(value []int64) Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int64pValue returns a Value for an *int64.
|
// Int64sValue returns a Value for an *int64.
|
||||||
func Int64pValue(value *int64) Value {
|
func Int64pValue(value *int64) Value {
|
||||||
if value == nil {
|
if value == nil {
|
||||||
return NilValue()
|
return NilValue()
|
||||||
@@ -159,7 +159,7 @@ func Float64Value(v float64) Value {
|
|||||||
return Value{num: math.Float64bits(v), Kind: KindFloat64}
|
return Value{num: math.Float64bits(v), Kind: KindFloat64}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Float64sValue returns a Value for a floating-points number.
|
// Float64Value returns a Value for a floating-points number.
|
||||||
func Float64sValue(values []float64) Value {
|
func Float64sValue(values []float64) Value {
|
||||||
return Value{
|
return Value{
|
||||||
Kind: KindArray,
|
Kind: KindArray,
|
||||||
@@ -175,7 +175,7 @@ func Float64sValue(values []float64) Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Float64pValue returns a Value for a floating-points number.
|
// Float64Value returns a Value for a floating-points number.
|
||||||
func Float64pValue(v *float64) Value {
|
func Float64pValue(v *float64) Value {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return NilValue()
|
return NilValue()
|
||||||
@@ -208,7 +208,7 @@ func Complex128Value(v complex128) Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex128sValue returns a Value for a []complex128.
|
// Complex128Value returns a Value for a []complex128.
|
||||||
func Complex128sValue(values []complex128) Value {
|
func Complex128sValue(values []complex128) Value {
|
||||||
return Value{
|
return Value{
|
||||||
Kind: KindArray,
|
Kind: KindArray,
|
||||||
@@ -224,7 +224,7 @@ func Complex128sValue(values []complex128) Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Complex128pValue returns a Value for a *complex128.
|
// Complex128Value returns a Value for a *complex128.
|
||||||
func Complex128pValue(v *complex128) Value {
|
func Complex128pValue(v *complex128) Value {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return NilValue()
|
return NilValue()
|
||||||
@@ -275,7 +275,7 @@ func DurationValue(v time.Duration) Value {
|
|||||||
return Value{inum: v.Nanoseconds(), Kind: KindDuration}
|
return Value{inum: v.Nanoseconds(), Kind: KindDuration}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DurationpValue returns a Value for a *time.Duration.
|
// DurationValue returns a Value for a *time.Duration.
|
||||||
func DurationpValue(v *time.Duration) Value {
|
func DurationpValue(v *time.Duration) Value {
|
||||||
if v == nil {
|
if v == nil {
|
||||||
return NilValue()
|
return NilValue()
|
||||||
@@ -284,7 +284,7 @@ func DurationpValue(v *time.Duration) Value {
|
|||||||
return DurationValue(*v)
|
return DurationValue(*v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DurationsValue returns a Value for a *time.Duration.
|
// DurationValue returns a Value for a *time.Duration.
|
||||||
func DurationsValue(values []time.Duration) Value {
|
func DurationsValue(values []time.Duration) Value {
|
||||||
return Value{
|
return Value{
|
||||||
Kind: KindArray,
|
Kind: KindArray,
|
||||||
@@ -469,7 +469,42 @@ func (v Value) String() string {
|
|||||||
return string(v.append(buf))
|
return string(v.append(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:gocyclo,cyclop
|
// append appends a text representation of v to dst.
|
||||||
|
// v is formatted as with fmt.Sprint.
|
||||||
|
//
|
||||||
|
//nolint:gomnd,cyclop
|
||||||
|
func (v Value) append(dst []byte) []byte {
|
||||||
|
switch v.Kind {
|
||||||
|
case KindString:
|
||||||
|
return append(dst, v.AsString()...)
|
||||||
|
case KindInt64:
|
||||||
|
return strconv.AppendInt(dst, v.inum, 10)
|
||||||
|
case KindUint64:
|
||||||
|
return strconv.AppendUint(dst, v.num, 10)
|
||||||
|
case KindFloat64:
|
||||||
|
return strconv.AppendFloat(dst, v.AsFloat64(), 'g', -1, 64)
|
||||||
|
case KindFloat32:
|
||||||
|
return strconv.AppendFloat(dst, float64(v.AsFloat32()), 'g', -1, 32)
|
||||||
|
case KindBool:
|
||||||
|
return strconv.AppendBool(dst, v.AsBool())
|
||||||
|
case KindDuration:
|
||||||
|
return append(dst, v.AsDuration().String()...)
|
||||||
|
case KindTime:
|
||||||
|
return append(dst, v.AsTime().String()...)
|
||||||
|
case KindError:
|
||||||
|
return append(dst, v.AsError().Error()...)
|
||||||
|
case KindGroup:
|
||||||
|
return fmt.Append(dst, v.AsGroup())
|
||||||
|
case KindClosure:
|
||||||
|
return fmt.Append(dst, v.Resolve())
|
||||||
|
case KindAny:
|
||||||
|
return fmt.Append(dst, v.any)
|
||||||
|
default:
|
||||||
|
return fmt.Appendf(dst, "%+v", v.any)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// nolint: gocyclo,cyclop
|
||||||
func (v Value) Any() any {
|
func (v Value) Any() any {
|
||||||
switch v.Kind {
|
switch v.Kind {
|
||||||
case KindAny, KindBinary:
|
case KindAny, KindBinary:
|
||||||
@@ -507,7 +542,7 @@ func (v Value) Any() any {
|
|||||||
return v.any
|
return v.any
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:forcetypeassert
|
// nolint: forcetypeassert
|
||||||
func (v Value) AsString() string {
|
func (v Value) AsString() string {
|
||||||
if v.Kind != KindString {
|
if v.Kind != KindString {
|
||||||
return ""
|
return ""
|
||||||
@@ -532,7 +567,6 @@ func (v Value) AsUint64() uint64 {
|
|||||||
return v.num
|
return v.num
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:gosec
|
|
||||||
func (v Value) AsFloat32() float32 {
|
func (v Value) AsFloat32() float32 {
|
||||||
return math.Float32frombits(uint32(v.num))
|
return math.Float32frombits(uint32(v.num))
|
||||||
}
|
}
|
||||||
@@ -615,38 +649,3 @@ func (v Value) AsArray() Values {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// append appends a text representation of v to dst.
|
|
||||||
// v is formatted as with fmt.Sprint.
|
|
||||||
//
|
|
||||||
//nolint:mnd,cyclop
|
|
||||||
func (v Value) append(dst []byte) []byte {
|
|
||||||
switch v.Kind {
|
|
||||||
case KindString:
|
|
||||||
return append(dst, v.AsString()...)
|
|
||||||
case KindInt64:
|
|
||||||
return strconv.AppendInt(dst, v.inum, 10)
|
|
||||||
case KindUint64:
|
|
||||||
return strconv.AppendUint(dst, v.num, 10)
|
|
||||||
case KindFloat64:
|
|
||||||
return strconv.AppendFloat(dst, v.AsFloat64(), 'g', -1, 64)
|
|
||||||
case KindFloat32:
|
|
||||||
return strconv.AppendFloat(dst, float64(v.AsFloat32()), 'g', -1, 32)
|
|
||||||
case KindBool:
|
|
||||||
return strconv.AppendBool(dst, v.AsBool())
|
|
||||||
case KindDuration:
|
|
||||||
return append(dst, v.AsDuration().String()...)
|
|
||||||
case KindTime:
|
|
||||||
return append(dst, v.AsTime().String()...)
|
|
||||||
case KindError:
|
|
||||||
return append(dst, v.AsError().Error()...)
|
|
||||||
case KindGroup:
|
|
||||||
return fmt.Append(dst, v.AsGroup())
|
|
||||||
case KindClosure:
|
|
||||||
return fmt.Append(dst, v.Resolve())
|
|
||||||
case KindAny:
|
|
||||||
return fmt.Append(dst, v.any)
|
|
||||||
default:
|
|
||||||
return fmt.Appendf(dst, "%+v", v.any)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
68
global.go
68
global.go
@@ -9,7 +9,7 @@ import (
|
|||||||
"gitoa.ru/go-4devs/log/level"
|
"gitoa.ru/go-4devs/log/level"
|
||||||
)
|
)
|
||||||
|
|
||||||
//nolint:gochecknoglobals
|
//nolint:gochecknoglobals,gomnd
|
||||||
var global = With(New(),
|
var global = With(New(),
|
||||||
WithTime(KeyTime, time.RFC3339),
|
WithTime(KeyTime, time.RFC3339),
|
||||||
WithLevel(KeyLevel, level.Debug),
|
WithLevel(KeyLevel, level.Debug),
|
||||||
@@ -28,112 +28,112 @@ func Log() Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Emerg log by emergency level.
|
// Emerg log by emergency level.
|
||||||
func Emerg(ctx context.Context, args ...any) {
|
func Emerg(ctx context.Context, args ...interface{}) {
|
||||||
global.Emerg(ctx, args...)
|
global.Emerg(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alert log by alert level.
|
// Alert log by alert level.
|
||||||
func Alert(ctx context.Context, args ...any) {
|
func Alert(ctx context.Context, args ...interface{}) {
|
||||||
global.Alert(ctx, args...)
|
global.Alert(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Crit log by critical level.
|
// Crit log by critical level.
|
||||||
func Crit(ctx context.Context, args ...any) {
|
func Crit(ctx context.Context, args ...interface{}) {
|
||||||
global.Crit(ctx, args...)
|
global.Crit(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Err log by error level.
|
// Err log by error level.
|
||||||
func Err(ctx context.Context, args ...any) {
|
func Err(ctx context.Context, args ...interface{}) {
|
||||||
global.Err(ctx, args...)
|
global.Err(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warn logs by warning level.
|
// Warn logs by warning level.
|
||||||
func Warn(ctx context.Context, args ...any) {
|
func Warn(ctx context.Context, args ...interface{}) {
|
||||||
global.Warn(ctx, args...)
|
global.Warn(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notice log by notice level.
|
// Notice log by notice level.
|
||||||
func Notice(ctx context.Context, args ...any) {
|
func Notice(ctx context.Context, args ...interface{}) {
|
||||||
global.Notice(ctx, args...)
|
global.Notice(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info log by info level.
|
// Info log by info level.
|
||||||
func Info(ctx context.Context, args ...any) {
|
func Info(ctx context.Context, args ...interface{}) {
|
||||||
global.Info(ctx, args...)
|
global.Info(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug log by debug level.
|
// Debug log by debug level.
|
||||||
func Debug(ctx context.Context, args ...any) {
|
func Debug(ctx context.Context, args ...interface{}) {
|
||||||
global.Debug(ctx, args...)
|
global.Debug(ctx, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print log by info level and arguments.
|
// Print log by info level and arguments.
|
||||||
func Print(args ...any) {
|
func Print(args ...interface{}) {
|
||||||
global.Print(args...)
|
global.Print(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatal log by alert level and arguments.
|
// Fatal log by alert level and arguments.
|
||||||
func Fatal(args ...any) {
|
func Fatal(args ...interface{}) {
|
||||||
global.Fatal(args...)
|
global.Fatal(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panic log by emergency level and arguments.
|
// Panic log by emergency level and arguments.
|
||||||
func Panic(args ...any) {
|
func Panic(args ...interface{}) {
|
||||||
global.Panic(args...)
|
global.Panic(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Println log by info level and arguments.
|
// Println log by info level and arguments.
|
||||||
func Println(args ...any) {
|
func Println(args ...interface{}) {
|
||||||
global.Println(args...)
|
global.Println(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatalln log by alert level and arguments.
|
// Fatalln log by alert level and arguments.
|
||||||
func Fatalln(args ...any) {
|
func Fatalln(args ...interface{}) {
|
||||||
global.Fatalln(args...)
|
global.Fatalln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panicln log by emergency level and arguments.
|
// Panicln log by emergency level and arguments.
|
||||||
func Panicln(args ...any) {
|
func Panicln(args ...interface{}) {
|
||||||
global.Panicln(args...)
|
global.Panicln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// EmergKVs sugared log by emergency level and key-values.
|
// EmergKVs sugared log by emergency level and key-values.
|
||||||
func EmergKVs(ctx context.Context, msg string, args ...any) {
|
func EmergKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
global.EmergKVs(ctx, msg, args...)
|
global.EmergKVs(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AlertKVs sugared log by alert level and key-values.
|
// AlertKVs sugared log by alert level and key-values.
|
||||||
func AlertKVs(ctx context.Context, msg string, args ...any) {
|
func AlertKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
global.AlertKVs(ctx, msg, args...)
|
global.AlertKVs(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CritKVs sugared log by critcal level and key-values.
|
// CritKVs sugared log by critcal level and key-values.
|
||||||
func CritKVs(ctx context.Context, msg string, args ...any) {
|
func CritKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
global.CritKVs(ctx, msg, args...)
|
global.CritKVs(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrKVs sugared log by error level and key-values.
|
// ErrKVs sugared log by error level and key-values.
|
||||||
func ErrKVs(ctx context.Context, msg string, args ...any) {
|
func ErrKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
global.ErrKVs(ctx, msg, args...)
|
global.ErrKVs(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WarnKVs sugared log by warning level and key-values.
|
// WarnKVs sugared log by warning level and key-values.
|
||||||
func WarnKVs(ctx context.Context, msg string, args ...any) {
|
func WarnKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
global.WarnKVs(ctx, msg, args...)
|
global.WarnKVs(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NoticeKVs sugared log by notice level and key-values.
|
// NoticeKVs sugared log by notice level and key-values.
|
||||||
func NoticeKVs(ctx context.Context, msg string, args ...any) {
|
func NoticeKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
global.NoticeKVs(ctx, msg, args...)
|
global.NoticeKVs(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// InfoKVs sugared log by info level and key-values.
|
// InfoKVs sugared log by info level and key-values.
|
||||||
func InfoKVs(ctx context.Context, msg string, args ...any) {
|
func InfoKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
global.InfoKVs(ctx, msg, args...)
|
global.InfoKVs(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DebugKVs sugared log by debug level and key-values.
|
// DebugKVs sugared log by debug level and key-values.
|
||||||
func DebugKVs(ctx context.Context, msg string, args ...any) {
|
func DebugKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
global.DebugKVs(ctx, msg, args...)
|
global.DebugKVs(ctx, msg, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,57 +178,57 @@ func DebugKV(ctx context.Context, msg string, args ...field.Field) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Emergf log by emergency level by format and arguments.
|
// Emergf log by emergency level by format and arguments.
|
||||||
func Emergf(ctx context.Context, format string, args ...any) {
|
func Emergf(ctx context.Context, format string, args ...interface{}) {
|
||||||
global.Emergf(ctx, format, args...)
|
global.Emergf(ctx, format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alertf log by alert level by format and arguments.
|
// Alertf log by alert level by format and arguments.
|
||||||
func Alertf(ctx context.Context, format string, args ...any) {
|
func Alertf(ctx context.Context, format string, args ...interface{}) {
|
||||||
global.Alertf(ctx, format, args...)
|
global.Alertf(ctx, format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Critf log by critical level by format and arguments.
|
// Critf log by critical level by format and arguments.
|
||||||
func Critf(ctx context.Context, format string, args ...any) {
|
func Critf(ctx context.Context, format string, args ...interface{}) {
|
||||||
global.Critf(ctx, format, args...)
|
global.Critf(ctx, format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errf log by error level by format and arguments.
|
// Errf log by error level by format and arguments.
|
||||||
func Errf(ctx context.Context, format string, args ...any) {
|
func Errf(ctx context.Context, format string, args ...interface{}) {
|
||||||
global.Errf(ctx, format, args...)
|
global.Errf(ctx, format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warnf log by warning level by format and arguments.
|
// Warnf log by warning level by format and arguments.
|
||||||
func Warnf(ctx context.Context, format string, args ...any) {
|
func Warnf(ctx context.Context, format string, args ...interface{}) {
|
||||||
global.Warnf(ctx, format, args...)
|
global.Warnf(ctx, format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Noticef log by notice level by format and arguments.
|
// Noticef log by notice level by format and arguments.
|
||||||
func Noticef(ctx context.Context, format string, args ...any) {
|
func Noticef(ctx context.Context, format string, args ...interface{}) {
|
||||||
global.Noticef(ctx, format, args...)
|
global.Noticef(ctx, format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Infof log by info level by format and arguments.
|
// Infof log by info level by format and arguments.
|
||||||
func Infof(ctx context.Context, format string, args ...any) {
|
func Infof(ctx context.Context, format string, args ...interface{}) {
|
||||||
global.Noticef(ctx, format, args...)
|
global.Noticef(ctx, format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debugf log by debug level by format and arguments.
|
// Debugf log by debug level by format and arguments.
|
||||||
func Debugf(ctx context.Context, format string, args ...any) {
|
func Debugf(ctx context.Context, format string, args ...interface{}) {
|
||||||
global.Debugf(ctx, format, args...)
|
global.Debugf(ctx, format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Printf log by info level by format and arguments without context.
|
// Printf log by info level by format and arguments without context.
|
||||||
func Printf(format string, args ...any) {
|
func Printf(format string, args ...interface{}) {
|
||||||
global.Printf(format, args...)
|
global.Printf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatalf log by alert level by format and arguments without context.
|
// Fatalf log by alert level by format and arguments without context.
|
||||||
func Fatalf(format string, args ...any) {
|
func Fatalf(format string, args ...interface{}) {
|
||||||
global.Fatalf(format, args...)
|
global.Fatalf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panicf log by emergency level and arguments without context.
|
// Panicf log by emergency level and arguments without context.
|
||||||
func Panicf(format string, args ...any) {
|
func Panicf(format string, args ...interface{}) {
|
||||||
global.Panicf(format, args...)
|
global.Panicf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,3 +1,3 @@
|
|||||||
module gitoa.ru/go-4devs/log
|
module gitoa.ru/go-4devs/log
|
||||||
|
|
||||||
go 1.22
|
go 1.20
|
||||||
|
|||||||
@@ -51,9 +51,7 @@ func TestUnmarshalJSON(t *testing.T) {
|
|||||||
for expect, actuals := range levels {
|
for expect, actuals := range levels {
|
||||||
for _, actual := range actuals {
|
for _, actual := range actuals {
|
||||||
var level level.Level
|
var level level.Level
|
||||||
|
if err := level.UnmarshalJSON([]byte(actual)); err != nil {
|
||||||
err := level.UnmarshalJSON([]byte(actual))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("%s got err: %s", level, err)
|
t.Errorf("%s got err: %s", level, err)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
|
|||||||
177
logger.go
177
logger.go
@@ -28,118 +28,172 @@ func (l Logger) Write(in []byte) (int, error) {
|
|||||||
return l.write(context.Background(), level.Info, string(in))
|
return l.write(context.Background(), level.Info, string(in))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l Logger) write(ctx context.Context, level level.Level, msg string, fields ...field.Field) (int, error) {
|
||||||
|
data := entry.Get()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
entry.Put(data)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return l(ctx, data.SetLevel(level).SetMessage(msg).Add(fields...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l Logger) writef(ctx context.Context, level level.Level, format string, args ...interface{}) (int, error) {
|
||||||
|
data := entry.Get()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
entry.Put(data)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return l(ctx, data.SetLevel(level).SetMessagef(format, args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l Logger) kv(_ context.Context, args ...interface{}) field.Fields {
|
||||||
|
kvEntry := entry.Get()
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
entry.Put(kvEntry)
|
||||||
|
}()
|
||||||
|
|
||||||
|
for i := 0; i < len(args); i++ {
|
||||||
|
if f, ok := args[i].(field.Field); ok {
|
||||||
|
kvEntry = kvEntry.Add(f)
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if i == len(args)-1 {
|
||||||
|
kvEntry = kvEntry.AddAny(badKey, args[i])
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
key, val := args[i], args[i+1]
|
||||||
|
if keyStr, ok := key.(string); ok {
|
||||||
|
kvEntry = kvEntry.AddAny(keyStr, val)
|
||||||
|
i++
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
kvEntry = kvEntry.AddAny(badKey, args[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
return kvEntry.Fields()
|
||||||
|
}
|
||||||
|
|
||||||
// With adds middlewares to logger.
|
// With adds middlewares to logger.
|
||||||
func (l Logger) With(mw ...Middleware) Logger {
|
func (l Logger) With(mw ...Middleware) Logger {
|
||||||
return With(l, mw...)
|
return With(l, mw...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Emerg log by emergency level.
|
// Emerg log by emergency level.
|
||||||
func (l Logger) Emerg(ctx context.Context, args ...any) {
|
func (l Logger) Emerg(ctx context.Context, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Emergency, "", args...))
|
writeOutput(l.writef(ctx, level.Emergency, "", args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alert log by alert level.
|
// Alert log by alert level.
|
||||||
func (l Logger) Alert(ctx context.Context, args ...any) {
|
func (l Logger) Alert(ctx context.Context, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Alert, "", args...))
|
writeOutput(l.writef(ctx, level.Alert, "", args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Crit log by critical level.
|
// Crit log by critical level.
|
||||||
func (l Logger) Crit(ctx context.Context, args ...any) {
|
func (l Logger) Crit(ctx context.Context, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Critical, "", args...))
|
writeOutput(l.writef(ctx, level.Critical, "", args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Err log by error level.
|
// Err log by error level.
|
||||||
func (l Logger) Err(ctx context.Context, args ...any) {
|
func (l Logger) Err(ctx context.Context, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Error, "", args...))
|
writeOutput(l.writef(ctx, level.Error, "", args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warn log by warning level.
|
// Warn log by warning level.
|
||||||
func (l Logger) Warn(ctx context.Context, args ...any) {
|
func (l Logger) Warn(ctx context.Context, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Warning, "", args...))
|
writeOutput(l.writef(ctx, level.Warning, "", args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Notice log by notice level.
|
// Notice log by notice level.
|
||||||
func (l Logger) Notice(ctx context.Context, args ...any) {
|
func (l Logger) Notice(ctx context.Context, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Notice, "", args...))
|
writeOutput(l.writef(ctx, level.Notice, "", args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info log by info level.
|
// Info log by info level.
|
||||||
func (l Logger) Info(ctx context.Context, args ...any) {
|
func (l Logger) Info(ctx context.Context, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Info, "", args...))
|
writeOutput(l.writef(ctx, level.Info, "", args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug log by debug level.
|
// Debug log by debug level.
|
||||||
func (l Logger) Debug(ctx context.Context, args ...any) {
|
func (l Logger) Debug(ctx context.Context, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Debug, "", args...))
|
writeOutput(l.writef(ctx, level.Debug, "", args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print log by info level and arguments.
|
// Print log by info level and arguments.
|
||||||
func (l Logger) Print(args ...any) {
|
func (l Logger) Print(args ...interface{}) {
|
||||||
writeOutput(l.writef(context.Background(), level.Info, "", args...))
|
writeOutput(l.writef(context.Background(), level.Info, "", args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatal log by alert level and arguments.
|
// Fatal log by alert level and arguments.
|
||||||
func (l Logger) Fatal(args ...any) {
|
func (l Logger) Fatal(args ...interface{}) {
|
||||||
writeOutput(l.writef(context.Background(), level.Alert, "", args...))
|
writeOutput(l.writef(context.Background(), level.Alert, "", args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panic log by emergency level and arguments.
|
// Panic log by emergency level and arguments.
|
||||||
func (l Logger) Panic(args ...any) {
|
func (l Logger) Panic(args ...interface{}) {
|
||||||
writeOutput(l.writef(context.Background(), level.Emergency, "", args...))
|
writeOutput(l.writef(context.Background(), level.Emergency, "", args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Println log by info level and arguments.
|
// Println log by info level and arguments.
|
||||||
func (l Logger) Println(args ...any) {
|
func (l Logger) Println(args ...interface{}) {
|
||||||
writeOutput(l.write(context.Background(), level.Info, fmt.Sprintln(args...)))
|
writeOutput(l.write(context.Background(), level.Info, fmt.Sprintln(args...)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatalln log by alert level and arguments.
|
// Fatalln log by alert level and arguments.
|
||||||
func (l Logger) Fatalln(args ...any) {
|
func (l Logger) Fatalln(args ...interface{}) {
|
||||||
writeOutput(l.write(context.Background(), level.Alert, fmt.Sprintln(args...)))
|
writeOutput(l.write(context.Background(), level.Alert, fmt.Sprintln(args...)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panicln log by emergency level and arguments.
|
// Panicln log by emergency level and arguments.
|
||||||
func (l Logger) Panicln(args ...any) {
|
func (l Logger) Panicln(args ...interface{}) {
|
||||||
writeOutput(l.write(context.Background(), level.Emergency, fmt.Sprintln(args...)))
|
writeOutput(l.write(context.Background(), level.Emergency, fmt.Sprintln(args...)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// EmergKVs sugared log by emergency level and key-values.
|
// EmergKVs sugared log by emergency level and key-values.
|
||||||
func (l Logger) EmergKVs(ctx context.Context, msg string, args ...any) {
|
func (l Logger) EmergKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
writeOutput(l.write(ctx, level.Emergency, msg, l.kv(ctx, args...)...))
|
writeOutput(l.write(ctx, level.Emergency, msg, l.kv(ctx, args...)...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AlertKVs sugared log by alert level and key-values.
|
// AlertKVs sugared log by alert level and key-values.
|
||||||
func (l Logger) AlertKVs(ctx context.Context, msg string, args ...any) {
|
func (l Logger) AlertKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
writeOutput(l.write(ctx, level.Alert, msg, l.kv(ctx, args...)...))
|
writeOutput(l.write(ctx, level.Alert, msg, l.kv(ctx, args...)...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CritKVs sugared log by critcal level and key-values.
|
// CritKVs sugared log by critcal level and key-values.
|
||||||
func (l Logger) CritKVs(ctx context.Context, msg string, args ...any) {
|
func (l Logger) CritKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
writeOutput(l.write(ctx, level.Critical, msg, l.kv(ctx, args...)...))
|
writeOutput(l.write(ctx, level.Critical, msg, l.kv(ctx, args...)...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrKVs sugared log by error level and key-values.
|
// ErrKVs sugared log by error level and key-values.
|
||||||
func (l Logger) ErrKVs(ctx context.Context, msg string, args ...any) {
|
func (l Logger) ErrKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
writeOutput(l.write(ctx, level.Error, msg, l.kv(ctx, args...)...))
|
writeOutput(l.write(ctx, level.Error, msg, l.kv(ctx, args...)...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// WarnKVs sugared log by warning level and key-values.
|
// WarnKVs sugared log by warning level and key-values.
|
||||||
func (l Logger) WarnKVs(ctx context.Context, msg string, args ...any) {
|
func (l Logger) WarnKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
writeOutput(l.write(ctx, level.Warning, msg, l.kv(ctx, args...)...))
|
writeOutput(l.write(ctx, level.Warning, msg, l.kv(ctx, args...)...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// NoticeKVs sugared log by notice level and key-values.
|
// NoticeKVs sugared log by notice level and key-values.
|
||||||
func (l Logger) NoticeKVs(ctx context.Context, msg string, args ...any) {
|
func (l Logger) NoticeKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
writeOutput(l.write(ctx, level.Notice, msg, l.kv(ctx, args...)...))
|
writeOutput(l.write(ctx, level.Notice, msg, l.kv(ctx, args...)...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// InfoKVs sugared log by info level and key-values.
|
// InfoKVs sugared log by info level and key-values.
|
||||||
func (l Logger) InfoKVs(ctx context.Context, msg string, args ...any) {
|
func (l Logger) InfoKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
writeOutput(l.write(ctx, level.Info, msg, l.kv(ctx, args...)...))
|
writeOutput(l.write(ctx, level.Info, msg, l.kv(ctx, args...)...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// DebugKVs sugared log by debug level and key-values.
|
// DebugKVs sugared log by debug level and key-values.
|
||||||
func (l Logger) DebugKVs(ctx context.Context, msg string, args ...any) {
|
func (l Logger) DebugKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||||
writeOutput(l.write(ctx, level.Debug, msg, l.kv(ctx, args...)...))
|
writeOutput(l.write(ctx, level.Debug, msg, l.kv(ctx, args...)...))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,57 +238,57 @@ func (l Logger) DebugKV(ctx context.Context, msg string, args ...field.Field) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Emergf log by emergency level by format and arguments.
|
// Emergf log by emergency level by format and arguments.
|
||||||
func (l Logger) Emergf(ctx context.Context, format string, args ...any) {
|
func (l Logger) Emergf(ctx context.Context, format string, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Emergency, format, args...))
|
writeOutput(l.writef(ctx, level.Emergency, format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alertf log by alert level by format and arguments.
|
// Alertf log by alert level by format and arguments.
|
||||||
func (l Logger) Alertf(ctx context.Context, format string, args ...any) {
|
func (l Logger) Alertf(ctx context.Context, format string, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Alert, format, args...))
|
writeOutput(l.writef(ctx, level.Alert, format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Critf log by critical level by format and arguments.
|
// Critf log by critical level by format and arguments.
|
||||||
func (l Logger) Critf(ctx context.Context, format string, args ...any) {
|
func (l Logger) Critf(ctx context.Context, format string, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Critical, format, args...))
|
writeOutput(l.writef(ctx, level.Critical, format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errf log by error level by format and arguments.
|
// Errf log by error level by format and arguments.
|
||||||
func (l Logger) Errf(ctx context.Context, format string, args ...any) {
|
func (l Logger) Errf(ctx context.Context, format string, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Error, format, args...))
|
writeOutput(l.writef(ctx, level.Error, format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warnf log by warning level by format and arguments.
|
// Warnf log by warning level by format and arguments.
|
||||||
func (l Logger) Warnf(ctx context.Context, format string, args ...any) {
|
func (l Logger) Warnf(ctx context.Context, format string, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Warning, format, args...))
|
writeOutput(l.writef(ctx, level.Warning, format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Noticef log by notice level by format and arguments.
|
// Noticef log by notice level by format and arguments.
|
||||||
func (l Logger) Noticef(ctx context.Context, format string, args ...any) {
|
func (l Logger) Noticef(ctx context.Context, format string, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Notice, format, args...))
|
writeOutput(l.writef(ctx, level.Notice, format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Infof log by info level by format and arguments.
|
// Infof log by info level by format and arguments.
|
||||||
func (l Logger) Infof(ctx context.Context, format string, args ...any) {
|
func (l Logger) Infof(ctx context.Context, format string, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Info, format, args...))
|
writeOutput(l.writef(ctx, level.Info, format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debugf log by debug level by format and arguments.
|
// Debugf log by debug level by format and arguments.
|
||||||
func (l Logger) Debugf(ctx context.Context, format string, args ...any) {
|
func (l Logger) Debugf(ctx context.Context, format string, args ...interface{}) {
|
||||||
writeOutput(l.writef(ctx, level.Debug, format, args...))
|
writeOutput(l.writef(ctx, level.Debug, format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Printf log by info level by format and arguments without context.
|
// Printf log by info level by format and arguments without context.
|
||||||
func (l Logger) Printf(format string, args ...any) {
|
func (l Logger) Printf(format string, args ...interface{}) {
|
||||||
writeOutput(l.writef(context.Background(), level.Info, format, args...))
|
writeOutput(l.writef(context.Background(), level.Info, format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fatalf log by alert level by format and arguments without context.
|
// Fatalf log by alert level by format and arguments without context.
|
||||||
func (l Logger) Fatalf(format string, args ...any) {
|
func (l Logger) Fatalf(format string, args ...interface{}) {
|
||||||
writeOutput(l.writef(context.Background(), level.Alert, format, args...))
|
writeOutput(l.writef(context.Background(), level.Alert, format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Panicf log by emergency level and arguments without context.
|
// Panicf log by emergency level and arguments without context.
|
||||||
func (l Logger) Panicf(format string, args ...any) {
|
func (l Logger) Panicf(format string, args ...interface{}) {
|
||||||
writeOutput(l.writef(context.Background(), level.Emergency, format, args...))
|
writeOutput(l.writef(context.Background(), level.Emergency, format, args...))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,67 +301,12 @@ func (l Logger) Writer(ctx context.Context, level level.Level, fields ...field.F
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Logger) kv(_ context.Context, args ...any) field.Fields {
|
|
||||||
kvEntry := entry.Get()
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
entry.Put(kvEntry)
|
|
||||||
}()
|
|
||||||
|
|
||||||
for i := 0; i < len(args); i++ {
|
|
||||||
if f, ok := args[i].(field.Field); ok {
|
|
||||||
kvEntry = kvEntry.Add(f)
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if i == len(args)-1 {
|
|
||||||
kvEntry = kvEntry.AddAny(badKey, args[i])
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
key, val := args[i], args[i+1]
|
|
||||||
if keyStr, ok := key.(string); ok {
|
|
||||||
kvEntry = kvEntry.AddAny(keyStr, val)
|
|
||||||
i++
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
kvEntry = kvEntry.AddAny(badKey, args[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
return kvEntry.Fields()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l Logger) write(ctx context.Context, level level.Level, msg string, fields ...field.Field) (int, error) {
|
|
||||||
data := entry.Get()
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
entry.Put(data)
|
|
||||||
}()
|
|
||||||
|
|
||||||
return l(ctx, data.SetLevel(level).SetMessage(msg).Add(fields...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l Logger) writef(ctx context.Context, level level.Level, format string, args ...any) (int, error) {
|
|
||||||
data := entry.Get()
|
|
||||||
|
|
||||||
defer func() {
|
|
||||||
entry.Put(data)
|
|
||||||
}()
|
|
||||||
|
|
||||||
return l(ctx, data.SetLevel(level).SetMessagef(format, args...))
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:containedctx
|
//nolint:containedctx
|
||||||
type writer struct {
|
type writer struct {
|
||||||
Logger
|
|
||||||
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
level level.Level
|
level level.Level
|
||||||
fields []field.Field
|
fields []field.Field
|
||||||
|
Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w writer) WithLevel(level level.Level) writer {
|
func (w writer) WithLevel(level level.Level) writer {
|
||||||
|
|||||||
@@ -213,8 +213,8 @@ func ExampleNew_jsonFormat() {
|
|||||||
logger.Err(ctx, "same error message")
|
logger.Err(ctx, "same error message")
|
||||||
logger.WarnKVs(ctx, "same warn message", "obj", Obj{Name: "obj name"})
|
logger.WarnKVs(ctx, "same warn message", "obj", Obj{Name: "obj name"})
|
||||||
// Output:
|
// Output:
|
||||||
// {"msg":"same error message","level":"error","go-version":"go1.25.5"}
|
// {"msg":"same error message","level":"error","go-version":"go1.22.2"}
|
||||||
// {"msg":"same warn message","obj":{"Name":"obj name","IsEnable":false},"level":"warning","go-version":"go1.25.5"}
|
// {"msg":"same warn message","obj":{"Name":"obj name","IsEnable":false},"level":"warning","go-version":"go1.22.2"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleNew_textEncoding() {
|
func ExampleNew_textEncoding() {
|
||||||
@@ -227,8 +227,8 @@ func ExampleNew_textEncoding() {
|
|||||||
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, "obj", Obj{Name: "text value", IsEnable: true})
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// msg="same error message" level=error go-version=go1.25.5
|
// msg="same error message" level=error go-version=go1.22.2
|
||||||
// msg="same info message" api-version=0.1 obj={Name:text value IsEnable:true} level=info go-version=go1.25.5
|
// msg="same info message" api-version=0.1 obj={Name:text value IsEnable:true} level=info go-version=go1.22.2
|
||||||
}
|
}
|
||||||
|
|
||||||
type ctxKey string
|
type ctxKey string
|
||||||
@@ -243,7 +243,6 @@ func levelInfo(ctx context.Context, entry *entry.Entry, handler log.Logger) (int
|
|||||||
|
|
||||||
func ExampleWith() {
|
func ExampleWith() {
|
||||||
var requestID ctxKey = "requestID"
|
var requestID ctxKey = "requestID"
|
||||||
|
|
||||||
vctx := context.WithValue(ctx, requestID, "6a5fa048-7181-11ea-bc55-0242ac130003")
|
vctx := context.WithValue(ctx, requestID, "6a5fa048-7181-11ea-bc55-0242ac130003")
|
||||||
|
|
||||||
logger := log.New(log.WithStdout()).With(
|
logger := log.New(log.WithStdout()).With(
|
||||||
@@ -253,7 +252,7 @@ func ExampleWith() {
|
|||||||
log.GoVersion("go"),
|
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.25.5
|
// Output: msg="same message" level=info requestID=6a5fa048-7181-11ea-bc55-0242ac130003 api=0.1.0 go=go1.22.2
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Print() {
|
func ExampleLogger_Print() {
|
||||||
@@ -264,7 +263,7 @@ func ExampleLogger_Print() {
|
|||||||
log.GoVersion("go"),
|
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.25.5
|
// Output: msg="same message" level=info client=http api=0.1.0 go=go1.22.2
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExamplePrint() {
|
func ExamplePrint() {
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func WithLevel(key string, lvl level.Level) Middleware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// KeyValue add field by const key value.
|
// KeyValue add field by const key value.
|
||||||
func KeyValue(key string, value any) 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) {
|
||||||
return handler(ctx, e.AddAny(key, value))
|
return handler(ctx, e.AddAny(key, value))
|
||||||
}
|
}
|
||||||
@@ -75,7 +75,7 @@ func GoVersion(key string) Middleware {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithContextValue add field by context key.
|
// WithContext add field by context key.
|
||||||
func WithContextValue(keys ...fmt.Stringer) Middleware {
|
func WithContextValue(keys ...fmt.Stringer) 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) {
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
@@ -93,7 +93,6 @@ func WithName(name string) Middleware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// WithCaller adds called file.
|
// WithCaller adds called file.
|
||||||
//
|
|
||||||
// Deprecated: use WithSource.
|
// Deprecated: use WithSource.
|
||||||
func WithCaller(key string, depth int, full bool) Middleware {
|
func WithCaller(key string, depth int, full bool) Middleware {
|
||||||
const offset = 2
|
const offset = 2
|
||||||
|
|||||||
@@ -82,13 +82,13 @@ type Source struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l Source) MarshalText() ([]byte, error) {
|
func (l Source) MarshalText() ([]byte, error) {
|
||||||
return fmt.Appendf(nil, "%s:%d", l.File, l.Line), nil
|
return []byte(fmt.Sprintf("%s:%d", l.File, l.Line)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l Source) MarshalJSON() ([]byte, error) {
|
func (l Source) MarshalJSON() ([]byte, error) {
|
||||||
return fmt.Appendf([]byte{}, `{"file":"%s","line":%d,"func":"%s"}`, l.File, l.Line, l.Func), nil
|
return fmt.Appendf([]byte{}, `{"file":"%s","line":%d,"func":"%s"}`, l.File, l.Line, l.Func), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func errSourceField(skip, mframe int) field.Field {
|
func errSourceField(skip, max int) field.Field {
|
||||||
return field.String(KeySource, fmt.Sprintf("source not found by frames[%d:%d]", skip, mframe))
|
return field.String(KeySource, fmt.Sprintf("source not found by frames[%d:%d]", skip, max))
|
||||||
}
|
}
|
||||||
|
|||||||
11
writter.go
11
writter.go
@@ -13,16 +13,16 @@ import (
|
|||||||
|
|
||||||
// Keys for "built-in" attributes.
|
// Keys for "built-in" attributes.
|
||||||
const (
|
const (
|
||||||
// KeyTime is the key used by the built-in handlers for the time
|
// TimeKey is the key used by the built-in handlers for the time
|
||||||
// when the log method is called. The associated Value is a [time.Time].
|
// when the log method is called. The associated Value is a [time.Time].
|
||||||
KeyTime = "time"
|
KeyTime = "time"
|
||||||
// KeyLevel is the key used by the built-in handlers for the level
|
// LevelKey is the key used by the built-in handlers for the level
|
||||||
// of the log call. The associated value is a [Level].
|
// of the log call. The associated value is a [Level].
|
||||||
KeyLevel = "level"
|
KeyLevel = "level"
|
||||||
// KeyMessage is the key used by the built-in handlers for the
|
// MessageKey is the key used by the built-in handlers for the
|
||||||
// message of the log call. The associated value is a string.
|
// message of the log call. The associated value is a string.
|
||||||
KeyMessage = "msg"
|
KeyMessage = "msg"
|
||||||
// KeySource is the key used by the built-in handlers for the source file
|
// 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.
|
// and line of the log call. The associated value is a string.
|
||||||
KeySource = "source"
|
KeySource = "source"
|
||||||
// KeyName logger name.
|
// KeyName logger name.
|
||||||
@@ -104,7 +104,6 @@ func FormatWithBracket(enc Encoder) func(io.Writer, *entry.Entry) (int, error) {
|
|||||||
|
|
||||||
return func(w io.Writer, data *entry.Entry) (int, error) {
|
return func(w io.Writer, data *entry.Entry) (int, error) {
|
||||||
buf := buffer.New()
|
buf := buffer.New()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
buf.Free()
|
buf.Free()
|
||||||
}()
|
}()
|
||||||
@@ -140,7 +139,6 @@ func FormatWithBracket(enc Encoder) func(io.Writer, *entry.Entry) (int, error) {
|
|||||||
func FormatString(enc Encoder) func(io.Writer, *entry.Entry) (int, error) {
|
func FormatString(enc Encoder) func(io.Writer, *entry.Entry) (int, error) {
|
||||||
return func(w io.Writer, entry *entry.Entry) (int, error) {
|
return func(w io.Writer, entry *entry.Entry) (int, error) {
|
||||||
buf := buffer.New()
|
buf := buffer.New()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
buf.Free()
|
buf.Free()
|
||||||
}()
|
}()
|
||||||
@@ -165,7 +163,6 @@ func FormatString(enc Encoder) func(io.Writer, *entry.Entry) (int, error) {
|
|||||||
func FormatJSON(enc Encoder) func(w io.Writer, entry *entry.Entry) (int, error) {
|
func FormatJSON(enc Encoder) func(w io.Writer, entry *entry.Entry) (int, error) {
|
||||||
return func(w io.Writer, entry *entry.Entry) (int, error) {
|
return func(w io.Writer, entry *entry.Entry) (int, error) {
|
||||||
buf := buffer.New()
|
buf := buffer.New()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
buf.Free()
|
buf.Free()
|
||||||
}()
|
}()
|
||||||
|
|||||||
Reference in New Issue
Block a user