17 Commits

Author SHA1 Message Date
9ccc7030f2 Update README.md
All checks were successful
Go Action / goaction (push) Successful in 19m15s
2025-12-23 22:48:17 +03:00
9f8f38e43f add lint action (#19)
All checks were successful
Go Action / goaction (push) Successful in 19m15s
Reviewed-on: #19
2025-12-23 22:01:14 +03:00
859a8d88f7 Merge pull request 'update ling and go version' (#17) from format into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Reviewed-on: #17
2024-04-04 12:19:45 +03:00
4baf4b36e7 set golang version
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2024-04-04 12:17:02 +03:00
andrey
2fd0c1f9ec update ling and go version
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing
2024-04-04 12:12:09 +03:00
c7090b5067 Merge pull request 'use dependency format log' (#16) from format into master
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #16
2024-04-04 12:08:06 +03:00
andrey
16d3e04fd9 use dependency format log 2024-04-04 12:04:45 +03:00
57a908f894 add bracket format (#15)
All checks were successful
continuous-integration/drone/tag Build is passing
continuous-integration/drone/push Build is passing
Reviewed-on: #15
Co-authored-by: andrey <andrey@4devs.io>
Co-committed-by: andrey <andrey@4devs.io>
2024-01-03 21:35:21 +03:00
acaa46b73f update source (#14)
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #14
Co-authored-by: andrey <andrey@4devs.io>
Co-committed-by: andrey <andrey@4devs.io>
2024-01-03 18:44:40 +03:00
24a5d3dd88 Merge pull request 'add source with func name' (#13) from source into master
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/tag Build is failing
Reviewed-on: #13
2024-01-02 20:23:08 +03:00
andrey
1de7cc0034 add source with func name
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing
2024-01-02 20:22:35 +03:00
9a61d4b9d3 Merge pull request 'add handler zap' (#12) from zap into master
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #12
2024-01-02 17:33:24 +03:00
andrey
4aef5329c7 add handler zap
Some checks failed
continuous-integration/drone/pr Build is failing
continuous-integration/drone/push Build is failing
2024-01-02 17:18:18 +03:00
eb1708a296 Merge pull request 'add logrus handler' (#11) from logrus into master
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #11
2024-01-02 17:14:38 +03:00
andrey
50cfee751d add logrus handler
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing
2024-01-02 17:03:49 +03:00
722669f094 add otel as separate module (#10)
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #10
Co-authored-by: andrey <andrey@4devs.io>
Co-committed-by: andrey <andrey@4devs.io>
2024-01-02 16:56:49 +03:00
d365c5b36b Merge pull request 'remove handlers' (#9) from handlers into master
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #9
2024-01-02 15:59:46 +03:00
46 changed files with 1403 additions and 431 deletions

View File

@@ -1,9 +1,11 @@
---
kind: pipeline kind: pipeline
name: default type: docker
name: logger
steps: steps:
- name: test - name: test
image: golang:1.21.5 image: golang:1.22.2
volumes: volumes:
- name: deps - name: deps
path: /go/src/mod path: /go/src/mod
@@ -11,7 +13,7 @@ steps:
- go test - go test
- name: golangci-lint - name: golangci-lint
image: golangci/golangci-lint:v1.55 image: golangci/golangci-lint:v1.57
commands: commands:
- golangci-lint run - golangci-lint run

View File

@@ -0,0 +1,26 @@
name: Go Action
on: [push, pull_request]
jobs:
goaction:
runs-on: ubuntu-latest # Use a Gitea Actions runner label
steps:
- name: Check out repository code
uses: actions/checkout@v4 # Action to clone the repo
- name: Set up Go
uses: actions/setup-go@v5 # Action to install a specific Go version
with:
go-version: '1.25.5' # Specify your required Go version
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v8 # Use the golangci-lint action
with:
version: v2.7.2 # Specify the linter version
# Optional: additional arguments
args: --verbose
- name: Run go test
run: go test ./...

View File

@@ -1,6 +1,30 @@
linters-settings: version: "2"
linters:
default: all
disable:
- wsl
- noinlineerr
settings:
depguard:
rules:
main:
allow:
- $gostd
- gitoa.ru
funcorder:
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
@@ -9,60 +33,61 @@ linters-settings:
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
exhaustive: tagliatelle:
default-signifies-exhaustive: true case:
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
tagliatelle: exclusions:
case: generated: lax
use-field-name: true presets:
rules: - comments
json: snake - common-false-positives
yaml: camel - legacy
xml: camel - std-error-handling
bson: camel rules:
avro: snake - linters:
- exhaustruct
linters: - gochecknoglobals
enable-all: true - ireturn
disable: - mnd
# deprecated path: _test\.go
- interfacer - linters:
- structcheck - err113
- varcheck - lll
- golint path: _example_test\.go
- deadcode - linters:
- scopelint - lll
- exhaustivestruct - mnd
- ifshort path: example/*
- nosnakecase paths:
- maligned - third_party$
- builtin$
- depguard # need configure - examples$
formatters:
issues: enable:
# Excluding configuration per-path, per-linter, per-text and per-source - gci
exclude-rules: - gofmt
- path: _test\.go - gofumpt
linters: - goimports
- gomnd exclusions:
- ireturn generated: lax
- exhaustruct paths:
- gochecknoglobals - third_party$
- path: _example_test\.go - builtin$
linters: - examples$
- lll
- goerr113

View File

@@ -1,5 +1,5 @@
# log # log
[![Build Status](https://drone.gitoa.ru/api/badges/go-4devs/log/status.svg)](https://drone.gitoa.ru/go-4devs/log) ![Build Status](https://gitoa.ru/go-4devs/log/actions/workflows/goaction.yml/badge.svg)
[![Go Report Card](https://goreportcard.com/badge/gitoa.ru/go-4devs/log)](https://goreportcard.com/report/gitoa.ru/go-4devs/log) [![Go Report Card](https://goreportcard.com/badge/gitoa.ru/go-4devs/log)](https://goreportcard.com/report/gitoa.ru/go-4devs/log)
[![GoDoc](https://godoc.org/gitoa.ru/go-4devs/log?status.svg)](http://godoc.org/gitoa.ru/go-4devs/log) [![GoDoc](https://godoc.org/gitoa.ru/go-4devs/log?status.svg)](http://godoc.org/gitoa.ru/go-4devs/log)

View File

@@ -69,10 +69,10 @@ func getMessage(iter int) string {
return _messages[iter%1000] return _messages[iter%1000]
} }
func fakeFmtArgs() []interface{} { func fakeFmtArgs() []any {
// 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 []interface{}{ return []any{
_tenInts[0], _tenInts[0],
_tenInts, _tenInts,
_tenStrings[0], _tenStrings[0],
@@ -101,8 +101,8 @@ func fakeFields() []field.Field {
} }
} }
func fakeSugarFields() []interface{} { func fakeSugarFields() []any {
return []interface{}{ return []any{
"int", _tenInts[0], "int", _tenInts[0],
"ints", _tenInts, "ints", _tenInts,
"string", _tenStrings[0], "string", _tenStrings[0],
@@ -166,7 +166,9 @@ func BenchmarkDisabledAccumulatedContext(b *testing.B) {
b.Run("4devs/log.Context", func(b *testing.B) { b.Run("4devs/log.Context", func(b *testing.B) {
b.ResetTimer() b.ResetTimer()
logger := NewLogger().With(log.GoVersion("goversion")) logger := NewLogger().With(log.GoVersion("goversion"))
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
logger.InfoKV(ctx, getMessage(0), fakeFields()...) logger.InfoKV(ctx, getMessage(0), fakeFields()...)

View File

@@ -8,6 +8,7 @@ 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 {

View File

@@ -32,7 +32,7 @@ func WithMessage(msg string) Option {
} }
} }
func WithMessagef(format string, args ...interface{}) Option { func WithMessagef(format string, args ...any) 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([]interface{}, 0, defaultCap+1), args: make([]any, 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 []interface{} args []any
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 ...interface{}) *Entry { func (e *Entry) SetMessagef(format string, args ...any) *Entry {
if e == nil { if e == nil {
return New().SetMessagef(format, args...) return New().SetMessagef(format, args...)
} }
@@ -154,10 +154,31 @@ func (e *Entry) Add(fields ...field.Field) *Entry {
return e return e
} }
func (e *Entry) AddAny(key string, value interface{}) *Entry { func (e *Entry) AddAny(key string, value any) *Entry {
return e.Add(field.Any(key, value)) return e.Add(field.Any(key, value))
} }
func (e *Entry) AddString(key, value string) *Entry { func (e *Entry) AddString(key, value string) *Entry {
return e.Add(field.String(key, value)) return e.Add(field.String(key, value))
} }
func (e *Entry) Replace(key string, value field.Value) *Entry {
has := false
e.fields.Fields(func(f field.Field) bool {
if f.Key == key {
f.Value = value
has = true
return false
}
return true
})
if !has {
e.AddAny(key, value)
}
return e
}

View File

@@ -4,7 +4,7 @@ import "sync"
//nolint:gochecknoglobals //nolint:gochecknoglobals
var pool = sync.Pool{ var pool = sync.Pool{
New: func() interface{} { New: func() any {
return New() return New()
}, },
} }

38
example/log.go Normal file
View File

@@ -0,0 +1,38 @@
package main
import (
"context"
"time"
"gitoa.ru/go-4devs/log"
"gitoa.ru/go-4devs/log/field"
)
func main() {
ctx := context.Background()
log.DebugKV(ctx, "debug message")
log.ErrKV(ctx, "error message")
log.Errf(ctx, "format error message:%v", 42)
log.Err(ctx, "error message", 42)
service(ctx, log.Log())
logger := log.New(log.WithJSONFormat()).With(log.WithSource(10, log.TrimPath), log.WithTime(log.KeyTime, time.RFC3339))
logger.AlertKV(ctx, "alert message new logger", field.String("string", "value"))
service(ctx, logger)
strLogger := log.New(log.WithFormat(log.FormatWithBracket(field.NewEncoderText()))).With(log.WithSource(10, log.TrimPath), log.WithTime(log.KeyTime, time.RFC3339))
strLogger.AlertKV(ctx, "alert message new txt logger", field.String("string", "value"))
service(ctx, strLogger)
}
func service(ctx context.Context, logger log.Logger) {
logger = logger.With(log.WithName("service"))
logger.WarnKV(ctx, "warn service message")
otherService(ctx, logger)
}
func otherService(ctx context.Context, logger log.Logger) {
logger = logger.With(log.WithName("other_service"))
logger.WarnKV(ctx, "warn other service message")
}

View File

@@ -5,7 +5,7 @@ import (
) )
// Field create field. // Field create field.
func Field(key string, value interface{}) field.Field { func Field(key string, value any) field.Field {
return field.Any(key, value) return field.Any(key, value)
} }

View File

@@ -1,4 +1,3 @@
//nolint:gomnd
package field package field
import ( import (
@@ -104,7 +103,147 @@ func (b BaseEncoder) AppendDelimiter(dst []byte, deli byte) []byte {
return append(dst, deli) return append(dst, deli)
} }
//nolint:gocyclo,cyclop 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) AppendKey(dst []byte, key string, prefix string) []byte {
if prefix != "" {
dst = append(dst, prefix...)
}
return b.AppendString(dst, key)
}
//nolint:mnd
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)
}
//nolint:mnd
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...)
}
//nolint:mnd
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) 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, '"')
}
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 { func (b BaseEncoder) appendValue(dst []byte, val Value, prefix string, deli byte) []byte {
switch val.Kind { switch val.Kind {
case KindGroup: case KindGroup:
@@ -143,139 +282,3 @@ func (b BaseEncoder) appendValue(dst []byte, val Value, prefix string, deli byte
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, '"')
}

View File

@@ -15,6 +15,7 @@ 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()
}() }()

View File

@@ -1,6 +1,7 @@
package field package field
import ( import (
"encoding"
"fmt" "fmt"
) )
@@ -8,8 +9,18 @@ func NewEncoderText(opts ...func(*BaseEncoder)) BaseEncoder {
opts = append([]func(*BaseEncoder){ opts = append([]func(*BaseEncoder){
WithGropuConfig(0, 0, ' '), WithGropuConfig(0, 0, ' '),
WithNullValue("<nil>"), WithNullValue("<nil>"),
WithDefaultValue(func(dst []byte, _ Encoder, val Value) []byte { WithDefaultValue(func(dst []byte, enc Encoder, val Value) []byte {
return fmt.Appendf(dst, "%+v", val.Any()) switch value := val.Any().(type) {
case encoding.TextMarshaler:
data, err := value.MarshalText()
if err != nil {
return enc.AppendValue(dst, ErrorValue(err))
}
return enc.AppendValue(dst, StringValue(string(data)))
default:
return fmt.Appendf(dst, "%+v", val.Any())
}
}), }),
}, opts...) }, opts...)

View File

@@ -2,6 +2,7 @@ package field
import ( import (
"fmt" "fmt"
"slices"
"time" "time"
) )
@@ -501,3 +502,7 @@ type Field struct {
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)
} }
func (f Field) IsKey(keys ...string) bool {
return slices.Contains(keys, f.Key)
}

View File

@@ -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)
} }
// StringpValue returns a new Value for a string. // StringsValue 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 {
} }
} }
// Uint64sValue returns a Value for a []uint64. // Uint64pValue 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 {
} }
} }
// Int64sValue returns a Value for an *int64. // Int64pValue 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}
} }
// Float64Value returns a Value for a floating-points number. // Float64sValue 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 {
} }
} }
// Float64Value returns a Value for a floating-points number. // Float64pValue 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 {
} }
} }
// Complex128Value returns a Value for a []complex128. // Complex128sValue 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 {
} }
} }
// Complex128Value returns a Value for a *complex128. // Complex128pValue 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}
} }
// DurationValue returns a Value for a *time.Duration. // DurationpValue 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)
} }
// DurationValue returns a Value for a *time.Duration. // DurationsValue 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,42 +469,7 @@ func (v Value) String() string {
return string(v.append(buf)) return string(v.append(buf))
} }
// append appends a text representation of v to dst. //nolint:gocyclo,cyclop
// 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:
@@ -542,7 +507,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 ""
@@ -567,6 +532,7 @@ 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))
} }
@@ -649,3 +615,38 @@ 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)
}
}

View File

@@ -3,6 +3,7 @@ package log
import ( import (
"context" "context"
"io" "io"
"time"
"gitoa.ru/go-4devs/log/field" "gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/level" "gitoa.ru/go-4devs/log/level"
@@ -10,7 +11,7 @@ import (
//nolint:gochecknoglobals //nolint:gochecknoglobals
var global = With(New(), var global = With(New(),
WithCaller(KeySource, 1, false), WithTime(KeyTime, time.RFC3339),
WithLevel(KeyLevel, level.Debug), WithLevel(KeyLevel, level.Debug),
WithExit(level.Alert), WithExit(level.Alert),
WithPanic(level.Emergency), WithPanic(level.Emergency),
@@ -27,112 +28,112 @@ func Log() Logger {
} }
// Emerg log by emergency level. // Emerg log by emergency level.
func Emerg(ctx context.Context, args ...interface{}) { func Emerg(ctx context.Context, args ...any) {
global.Emerg(ctx, args...) global.Emerg(ctx, args...)
} }
// Alert log by alert level. // Alert log by alert level.
func Alert(ctx context.Context, args ...interface{}) { func Alert(ctx context.Context, args ...any) {
global.Alert(ctx, args...) global.Alert(ctx, args...)
} }
// Crit log by critical level. // Crit log by critical level.
func Crit(ctx context.Context, args ...interface{}) { func Crit(ctx context.Context, args ...any) {
global.Crit(ctx, args...) global.Crit(ctx, args...)
} }
// Err log by error level. // Err log by error level.
func Err(ctx context.Context, args ...interface{}) { func Err(ctx context.Context, args ...any) {
global.Err(ctx, args...) global.Err(ctx, args...)
} }
// Warn logs by warning level. // Warn logs by warning level.
func Warn(ctx context.Context, args ...interface{}) { func Warn(ctx context.Context, args ...any) {
global.Warn(ctx, args...) global.Warn(ctx, args...)
} }
// Notice log by notice level. // Notice log by notice level.
func Notice(ctx context.Context, args ...interface{}) { func Notice(ctx context.Context, args ...any) {
global.Notice(ctx, args...) global.Notice(ctx, args...)
} }
// Info log by info level. // Info log by info level.
func Info(ctx context.Context, args ...interface{}) { func Info(ctx context.Context, args ...any) {
global.Info(ctx, args...) global.Info(ctx, args...)
} }
// Debug log by debug level. // Debug log by debug level.
func Debug(ctx context.Context, args ...interface{}) { func Debug(ctx context.Context, args ...any) {
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 ...interface{}) { func Print(args ...any) {
global.Print(args...) global.Print(args...)
} }
// Fatal log by alert level and arguments. // Fatal log by alert level and arguments.
func Fatal(args ...interface{}) { func Fatal(args ...any) {
global.Fatal(args...) global.Fatal(args...)
} }
// Panic log by emergency level and arguments. // Panic log by emergency level and arguments.
func Panic(args ...interface{}) { func Panic(args ...any) {
global.Panic(args...) global.Panic(args...)
} }
// Println log by info level and arguments. // Println log by info level and arguments.
func Println(args ...interface{}) { func Println(args ...any) {
global.Println(args...) global.Println(args...)
} }
// Fatalln log by alert level and arguments. // Fatalln log by alert level and arguments.
func Fatalln(args ...interface{}) { func Fatalln(args ...any) {
global.Fatalln(args...) global.Fatalln(args...)
} }
// Panicln log by emergency level and arguments. // Panicln log by emergency level and arguments.
func Panicln(args ...interface{}) { func Panicln(args ...any) {
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 ...interface{}) { func EmergKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func AlertKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func CritKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func ErrKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func WarnKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func NoticeKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func InfoKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func DebugKVs(ctx context.Context, msg string, args ...any) {
global.DebugKVs(ctx, msg, args...) global.DebugKVs(ctx, msg, args...)
} }
@@ -177,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 ...interface{}) { func Emergf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func Alertf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func Critf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func Errf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func Warnf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func Noticef(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func Infof(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func Debugf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func Printf(format string, args ...any) {
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 ...interface{}) { func Fatalf(format string, args ...any) {
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 ...interface{}) { func Panicf(format string, args ...any) {
global.Panicf(format, args...) global.Panicf(format, args...)
} }

25
global_example_test.go Normal file
View File

@@ -0,0 +1,25 @@
package log_test
import (
"context"
"path/filepath"
"gitoa.ru/go-4devs/log"
"gitoa.ru/go-4devs/log/level"
)
func ExampleDebug() {
logger := log.New(log.WithStdout()).With(
log.WithSource(2, filepath.Base),
log.WithLevel(log.KeyLevel, level.Debug),
log.WithExit(level.Alert),
log.WithPanic(level.Emergency),
)
log.SetLogger(logger)
ctx := context.Background()
log.Debug(ctx, "debug message")
// Output:
// msg="debug message" source=global_example_test.go:22 level=debug
}

2
go.mod
View File

@@ -1,3 +1,3 @@
module gitoa.ru/go-4devs/log module gitoa.ru/go-4devs/log
go 1.20 go 1.22

10
handler/logrus/go.mod Normal file
View File

@@ -0,0 +1,10 @@
module gitoa.ru/go-4devs/log/handler/logrus
go 1.20
require (
github.com/sirupsen/logrus v1.9.3
gitoa.ru/go-4devs/log v0.5.1
)
require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect

17
handler/logrus/go.sum Normal file
View File

@@ -0,0 +1,17 @@
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/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.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gitoa.ru/go-4devs/log v0.5.1 h1:rrIyjpUaw8AjDCf7ZuH0HgCRf370O3TV29yrU1xizWM=
gitoa.ru/go-4devs/log v0.5.1/go.mod h1:tREtjEH2cTHl0p3uCVcH9g5tlqtsVNI/tDQVfq53Ty4=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

49
handler/logrus/logger.go Normal file
View File

@@ -0,0 +1,49 @@
package logrus
import (
"context"
"github.com/sirupsen/logrus"
"gitoa.ru/go-4devs/log"
"gitoa.ru/go-4devs/log/entry"
"gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/level"
)
// Standard create new standart logrus handler.
// Deprecated: delete after 0.7.0
func Standard() log.Logger {
return New(logrus.StandardLogger())
}
// New create new logrus handler.
// Deprecated: delete after 0.7.0
func New(log *logrus.Logger) log.Logger {
return func(ctx context.Context, data *entry.Entry) (int, error) {
lrgFields := make(logrus.Fields, data.Fields().Len())
data.Fields().Fields(func(f field.Field) bool {
lrgFields[f.Key] = f.Value.Any()
return true
})
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
}
}

View File

@@ -0,0 +1,30 @@
package logrus_test
import (
"context"
"io"
"os"
slogrus "github.com/sirupsen/logrus"
"gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/handler/logrus"
)
func ExampleNew_logrusHandler() {
ctx := context.Background()
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
}

View 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)
}
}

17
handler/otel/go.mod Normal file
View File

@@ -0,0 +1,17 @@
module gitoa.ru/go-4devs/log/handler/otel
go 1.21.5
require (
gitoa.ru/go-4devs/log v0.5.1
go.opentelemetry.io/otel v1.21.0
go.opentelemetry.io/otel/sdk v1.21.0
go.opentelemetry.io/otel/trace v1.21.0
)
require (
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
golang.org/x/sys v0.14.0 // indirect
)

27
handler/otel/go.sum Normal file
View File

@@ -0,0 +1,27 @@
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/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
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/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gitoa.ru/go-4devs/log v0.5.1 h1:rrIyjpUaw8AjDCf7ZuH0HgCRf370O3TV29yrU1xizWM=
gitoa.ru/go-4devs/log v0.5.1/go.mod h1:tREtjEH2cTHl0p3uCVcH9g5tlqtsVNI/tDQVfq53Ty4=
go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc=
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=
go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4=
go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM=
go.opentelemetry.io/otel/sdk v1.21.0 h1:FTt8qirL1EysG6sTQRZ5TokkU8d0ugCj8htOgThZXQ8=
go.opentelemetry.io/otel/sdk v1.21.0/go.mod h1:Nna6Yv7PWTdgJHVRD9hIYywQBRx7pbox6nwBnZIxl/E=
go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc=
go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

59
handler/otel/helpers.go Normal file
View File

@@ -0,0 +1,59 @@
package otel
import (
"context"
"gitoa.ru/go-4devs/log/entry"
"gitoa.ru/go-4devs/log/field"
"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)),
)
data.Fields().Fields(func(f field.Field) bool {
attrs = append(attrs, attribute.String(f.Key, f.Value.String()))
return true
})
span.AddEvent(data.Message(), trace.WithAttributes(attrs...))
}

16
handler/otel/level.go Normal file
View 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
)

View 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) + ")"
}
}

17
handler/otel/logger.go Normal file
View File

@@ -0,0 +1,17 @@
package otel
import (
"context"
"gitoa.ru/go-4devs/log"
"gitoa.ru/go-4devs/log/entry"
)
// Deprecated: delete after 0.7.0
func New() log.Logger {
return func(ctx context.Context, e *entry.Entry) (int, error) {
addEvent(ctx, e)
return 0, nil
}
}

View File

@@ -0,0 +1,62 @@
package otel_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() {
ctx := context.Background()
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(_ context.Context, spanData []sdktrace.ReadOnlySpan) error {
for _, data := range spanData {
for _, events := range data.Events() {
fmt.Print("event: ", events.Name)
for _, attr := range events.Attributes {
fmt.Printf(", %v = %v", attr.Key, attr.Value.AsInterface())
}
fmt.Print("\n")
}
}
return nil
}

View File

@@ -0,0 +1,17 @@
package otel
import (
"context"
"gitoa.ru/go-4devs/log"
"gitoa.ru/go-4devs/log/entry"
)
// Deprecated: delete after 0.7.0
func Middleware() log.Middleware {
return func(ctx context.Context, e *entry.Entry, handler log.Logger) (int, error) {
addEvent(ctx, e)
return handler(ctx, e)
}
}

10
handler/zap/go.mod Normal file
View File

@@ -0,0 +1,10 @@
module gitoa.ru/go-4devs/log/handler/zap
go 1.21.5
require (
gitoa.ru/go-4devs/log v0.5.1
go.uber.org/zap v1.26.0
)
require go.uber.org/multierr v1.10.0 // indirect

16
handler/zap/go.sum Normal file
View File

@@ -0,0 +1,16 @@
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gitoa.ru/go-4devs/log v0.5.1 h1:rrIyjpUaw8AjDCf7ZuH0HgCRf370O3TV29yrU1xizWM=
gitoa.ru/go-4devs/log v0.5.1/go.mod h1:tREtjEH2cTHl0p3uCVcH9g5tlqtsVNI/tDQVfq53Ty4=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

70
handler/zap/logger.go Normal file
View File

@@ -0,0 +1,70 @@
package zap
import (
"context"
"gitoa.ru/go-4devs/log"
"gitoa.ru/go-4devs/log/entry"
"gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/level"
"go.uber.org/zap"
)
// Deprecated: delete after 0.7.0
func Nop() log.Logger {
return New(zap.NewNop())
}
// Deprecated: delete after 0.7.0
func Example(options ...zap.Option) log.Logger {
return New(zap.NewExample(options...))
}
// Deprecated: delete after 0.7.0
func Production(options ...zap.Option) log.Logger {
z, err := zap.NewProduction(options...)
if err != nil {
panic(err)
}
return New(z)
}
// Deprecated: delete after 0.7.0
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, 0, data.Fields().Len())
data.Fields().Fields(func(f field.Field) bool {
zf = append(zf, zap.Any(f.Key, f.Value.Any()))
return true
})
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
}
}

View File

@@ -0,0 +1,23 @@
package zap_test
import (
"context"
"io"
"gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/handler/zap"
uzap "go.uber.org/zap"
)
func ExampleNew_zapHandler() {
ctx := context.Background()
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"}
}

View 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)
}
}

View File

@@ -51,7 +51,9 @@ 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
View File

@@ -28,172 +28,118 @@ 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 ...interface{}) { func (l Logger) Emerg(ctx context.Context, args ...any) {
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 ...interface{}) { func (l Logger) Alert(ctx context.Context, args ...any) {
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 ...interface{}) { func (l Logger) Crit(ctx context.Context, args ...any) {
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 ...interface{}) { func (l Logger) Err(ctx context.Context, args ...any) {
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 ...interface{}) { func (l Logger) Warn(ctx context.Context, args ...any) {
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 ...interface{}) { func (l Logger) Notice(ctx context.Context, args ...any) {
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 ...interface{}) { func (l Logger) Info(ctx context.Context, args ...any) {
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 ...interface{}) { func (l Logger) Debug(ctx context.Context, args ...any) {
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 ...interface{}) { func (l Logger) Print(args ...any) {
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 ...interface{}) { func (l Logger) Fatal(args ...any) {
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 ...interface{}) { func (l Logger) Panic(args ...any) {
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 ...interface{}) { func (l Logger) Println(args ...any) {
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 ...interface{}) { func (l Logger) Fatalln(args ...any) {
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 ...interface{}) { func (l Logger) Panicln(args ...any) {
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 ...interface{}) { func (l Logger) EmergKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func (l Logger) AlertKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func (l Logger) CritKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func (l Logger) ErrKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func (l Logger) WarnKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func (l Logger) NoticeKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func (l Logger) InfoKVs(ctx context.Context, msg string, args ...any) {
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 ...interface{}) { func (l Logger) DebugKVs(ctx context.Context, msg string, args ...any) {
writeOutput(l.write(ctx, level.Debug, msg, l.kv(ctx, args...)...)) writeOutput(l.write(ctx, level.Debug, msg, l.kv(ctx, args...)...))
} }
@@ -238,57 +184,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 ...interface{}) { func (l Logger) Emergf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func (l Logger) Alertf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func (l Logger) Critf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func (l Logger) Errf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func (l Logger) Warnf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func (l Logger) Noticef(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func (l Logger) Infof(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func (l Logger) Debugf(ctx context.Context, format string, args ...any) {
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 ...interface{}) { func (l Logger) Printf(format string, args ...any) {
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 ...interface{}) { func (l Logger) Fatalf(format string, args ...any) {
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 ...interface{}) { func (l Logger) Panicf(format string, args ...any) {
writeOutput(l.writef(context.Background(), level.Emergency, format, args...)) writeOutput(l.writef(context.Background(), level.Emergency, format, args...))
} }
@@ -301,12 +247,67 @@ 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 {

View File

@@ -1,22 +1,23 @@
package log_test package log_test
import ( import (
"path/filepath"
"gitoa.ru/go-4devs/log" "gitoa.ru/go-4devs/log"
"gitoa.ru/go-4devs/log/level" "gitoa.ru/go-4devs/log/level"
) )
func ExampleNew_withCaller() { func ExampleNew_withCaller() {
logger := log.With( logger := log.New(log.WithStdout()).With(
log.New(log.WithStdout()), log.WithLevel(log.KeyLevel, level.Debug),
log.WithLevel("level", level.Debug), log.WithSource(3, filepath.Base),
log.WithCaller("caller", 2, false),
) )
logger.Err(ctx, "same error message") logger.Err(ctx, "same error message")
logger.InfoKVs(ctx, "same info message", "api-version", 0.1) logger.InfoKVs(ctx, "same info message", "api-version", 0.1)
_, _ = logger.Write([]byte("same write message")) _, _ = logger.Write([]byte("same write message"))
// Output: // Output:
// msg="same error message" level=error caller=logger_example_caller_test.go:14 // msg="same error message" level=error source=logger_example_caller_test.go:15
// msg="same info message" api-version=0.1 level=info caller=logger_example_caller_test.go:15 // msg="same info message" api-version=0.1 level=info source=logger_example_caller_test.go:16
// msg="same write message" level=info caller=logger_example_caller_test.go:16 // msg="same write message" level=info source=logger_example_caller_test.go:17
} }

View File

@@ -47,19 +47,19 @@ 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(log.KeyLevel, 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() {
logger := log.New(log.WithStdout()).With(log.WithLevel("level", level.Error)) logger := log.New(log.WithStdout()).With(log.WithLevel(log.KeyLevel, level.Error))
logger.Err(ctx, "same error message") logger.Err(ctx, "same error message")
// Output: msg="same error message" level=error // Output: msg="same error message" level=error
} }
func ExampleNew_level_info() { func ExampleNew_level_info() {
logger := log.New(log.WithStdout()).With(log.WithLevel("level", level.Error)) logger := log.New(log.WithStdout()).With(log.WithLevel(log.KeyLevel, level.Error))
logger.Info(ctx, "same message") logger.Info(ctx, "same message")
// Output: // Output:
} }
@@ -92,7 +92,7 @@ var (
float64Val = float64(math.MaxFloat64) float64Val = float64(math.MaxFloat64)
minute = time.Minute minute = time.Minute
timeVal = time.Unix(0, math.MaxInt32) timeVal = time.Unix(0, math.MaxInt32).In(time.UTC)
) )
func ExampleNew_anyField() { func ExampleNew_anyField() {
@@ -108,7 +108,7 @@ func ExampleNew_anyField() {
field.Any("error", errors.New("error")), field.Any("error", errors.New("error")),
) )
// Output: // 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"} // {"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-01T00:00:02Z","duration":"1h0m0s","error":"error"}
} }
func ExampleNew_arrayField() { func ExampleNew_arrayField() {
@@ -130,11 +130,11 @@ func ExampleNew_arrayField() {
field.Complex64s("complex64s", 42, 24), field.Complex64s("complex64s", 42, 24),
field.Complex128s("complex128s", 42, 24), field.Complex128s("complex128s", 42, 24),
field.Durations("durations", time.Minute, time.Second), field.Durations("durations", time.Minute, time.Second),
field.Times("times", time.Unix(0, 42), time.Unix(0, 24)), field.Times("times", time.Unix(0, 42).In(time.UTC), time.Unix(0, 24).In(time.UTC)),
field.Errors("errors", errors.New("error"), errors.New("error2")), field.Errors("errors", errors.New("error"), errors.New("error2")),
) )
// Output: // 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"]} // {"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-01T00:00:00Z","1970-01-01T00:00:00Z"],"errors":["error","error2"]}
} }
func ExampleNew_pointerField() { func ExampleNew_pointerField() {
@@ -174,7 +174,7 @@ func ExampleNew_pointerField() {
field.Timep("timep", nil), field.Timep("timep", nil),
) )
// Output: // 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} // {"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-01T00:00:02Z","timep":null}
} }
func ExampleNew_fields() { func ExampleNew_fields() {
@@ -196,12 +196,12 @@ func ExampleNew_fields() {
field.Complex64("complex16", 42), field.Complex64("complex16", 42),
field.Complex128("complex128", 42), field.Complex128("complex128", 42),
field.Duration("duration", time.Minute), field.Duration("duration", time.Minute),
field.Time("time", time.Unix(0, 42)), field.Time("time", timeVal),
field.FormatTime("format_time", time.UnixDate, timeVal), field.FormatTime("format_time", time.UnixDate, timeVal),
field.Error("error", errors.New("error")), field.Error("error", errors.New("error")),
) )
// Output: // 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"} // {"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-01T00:00:02Z","format_time":"Thu Jan 1 00:00:02 UTC 1970","error":"error"}
} }
func ExampleNew_jsonFormat() { func ExampleNew_jsonFormat() {
@@ -213,22 +213,22 @@ 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.21.5"} // {"msg":"same error message","level":"error","go-version":"go1.25.5"}
// {"msg":"same warn message","obj":{"Name":"obj name","IsEnable":false},"level":"warning","go-version":"go1.21.5"} // {"msg":"same warn message","obj":{"Name":"obj name","IsEnable":false},"level":"warning","go-version":"go1.25.5"}
} }
func ExampleNew_textEncoding() { func ExampleNew_textEncoding() {
logger := log.With( logger := log.New(log.WithStdout()).
log.New(log.WithStdout()), With(
log.WithLevel(log.KeyLevel, level.Debug), log.WithLevel(log.KeyLevel, 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, "obj", Obj{Name: "text value", IsEnable: true})
// Output: // Output:
// msg="same error message" level=error go-version=go1.21.5 // msg="same error message" level=error go-version=go1.25.5
// 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 obj={Name:text value IsEnable:true} level=info go-version=go1.25.5
} }
type ctxKey string type ctxKey string
@@ -238,28 +238,33 @@ func (c ctxKey) String() string {
} }
func levelInfo(ctx context.Context, entry *entry.Entry, handler log.Logger) (int, error) { func levelInfo(ctx context.Context, entry *entry.Entry, handler log.Logger) (int, error) {
return handler(ctx, entry.Add(field.String("level", entry.Level().String()))) return handler(ctx, entry.Add(field.String(log.KeyLevel, entry.Level().String())))
} }
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.With( logger := log.New(log.WithStdout()).With(
log.New(log.WithStdout()), levelInfo,
levelInfo, log.WithContextValue(requestID), log.KeyValue("api", "0.1.0"), log.GoVersion("go"), 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.25.5
} }
func ExampleLogger_Print() { func ExampleLogger_Print() {
logger := log.With( logger := log.New(log.WithStdout()).With(
log.New(log.WithStdout()), levelInfo,
levelInfo, log.KeyValue("client", "http"), log.KeyValue("api", "0.1.0"), log.GoVersion("go"), 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.25.5
} }
func ExamplePrint() { func ExamplePrint() {
@@ -277,7 +282,7 @@ func Example_fieldClosureFn() {
return d return d
}) })
log := log.With(log.New(log.WithStdout()), log.WithLevel("level", level.Info)) log := log.New(log.WithStdout()).With(log.WithLevel(log.KeyLevel, level.Info))
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)
@@ -289,7 +294,9 @@ func Example_fieldClosureFn() {
} }
func Example_withGroup() { func Example_withGroup() {
log := log.With(log.New(log.WithStdout()), log.WithLevel(log.KeyLevel, level.Info)) log := log.New(log.WithStdout()).With(
log.WithLevel(log.KeyLevel, level.Info),
)
log.ErrKVs(ctx, "error message", log.ErrKVs(ctx, "error message",
field.Groups("grous_field", field.Groups("grous_field",

View File

@@ -27,7 +27,7 @@ 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(log.KeyLevel, level.Info))
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",
@@ -65,7 +65,7 @@ func TestWriter(t *testing.T) {
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(log.KeyLevel, level.Info))
_, _ = logger.Writer( _, _ = logger.Writer(
context.WithValue(ctx, requestID, "6a5fa048-7181-11ea-bc55-0242ac1311113"), context.WithValue(ctx, requestID, "6a5fa048-7181-11ea-bc55-0242ac1311113"),
@@ -91,7 +91,7 @@ func TestLogger(t *testing.T) {
ctx := context.Background() ctx := context.Background()
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(log.KeyLevel, level.Info))
_, err := logger(ctx, nil) _, err := logger(ctx, nil)
if err != nil { if err != nil {

View File

@@ -38,6 +38,7 @@ func With(logger Logger, mw ...Middleware) Logger {
if curI == lastI { if curI == lastI {
return logger(currentCtx, currentEntry) return logger(currentCtx, currentEntry)
} }
curI++ curI++
n, err := mw[curI](currentCtx, currentEntry, chainHandler) n, err := mw[curI](currentCtx, currentEntry, chainHandler)
curI-- curI--
@@ -61,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 interface{}) Middleware { func KeyValue(key string, value any) 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))
} }
@@ -74,7 +75,7 @@ func GoVersion(key string) Middleware {
} }
} }
// WithContext add field by context key. // WithContextValue 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 {
@@ -85,7 +86,15 @@ func WithContextValue(keys ...fmt.Stringer) Middleware {
} }
} }
func WithName(name string) Middleware {
return func(ctx context.Context, data *entry.Entry, handler Logger) (int, error) {
return handler(ctx, data.Replace(KeyName, field.StringValue(name)))
}
}
// WithCaller adds called file. // WithCaller adds called file.
//
// 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

94
source.go Normal file
View File

@@ -0,0 +1,94 @@
package log
import (
"context"
"fmt"
"path/filepath"
"runtime"
"strings"
"gitoa.ru/go-4devs/log/entry"
"gitoa.ru/go-4devs/log/field"
)
func WithSource(items int, trimPath func(string) string) Middleware {
const (
skip = 4
funcPrefix = "gitoa.ru/go-4devs/log.Logger"
skipHelper = "gitoa.ru/go-4devs/log."
)
items += skip
return func(ctx context.Context, data *entry.Entry, handler Logger) (int, error) {
pc := make([]uintptr, items)
n := runtime.Callers(skip, pc)
if n == 0 {
return handler(ctx, data.Add(errSourceField(skip, items)))
}
pc = pc[:n] // pass only valid pcs to runtime.CallersFrames
frames := runtime.CallersFrames(pc)
prew := false
for {
frame, more := frames.Next()
has := strings.HasPrefix(frame.Function, funcPrefix)
if !has && prew {
if strings.HasPrefix(frame.Function, skipHelper) {
continue
}
return handler(ctx, data.AddAny(KeySource, Source{
Func: filepath.Base(frame.Function),
Line: frame.Line,
File: trimPath(frame.File),
}))
}
prew = has
if !more {
break
}
}
return handler(ctx, data.Add(errSourceField(skip, items)))
}
}
func TrimPath(file string) string {
idx := strings.LastIndexByte(file, '/')
if idx == -1 {
return filepath.Base(file)
}
// Find the penultimate separator.
idx = strings.LastIndexByte(file[:idx], '/')
if idx == -1 {
return filepath.Base(file)
}
return file[idx+1:]
}
// Source describes the location of a line of source code.
type Source struct {
Func string `json:"func"`
File string `json:"file"`
Line int `json:"line"`
}
func (l Source) MarshalText() ([]byte, error) {
return fmt.Appendf(nil, "%s:%d", l.File, l.Line), nil
}
func (l Source) MarshalJSON() ([]byte, error) {
return fmt.Appendf([]byte{}, `{"file":"%s","line":%d,"func":"%s"}`, l.File, l.Line, l.Func), nil
}
func errSourceField(skip, mframe int) field.Field {
return field.String(KeySource, fmt.Sprintf("source not found by frames[%d:%d]", skip, mframe))
}

26
source_example_test.go Normal file
View File

@@ -0,0 +1,26 @@
package log_test
import (
"context"
"path/filepath"
"gitoa.ru/go-4devs/log"
)
func ExampleWithSource() {
ctx := context.Background()
logger := log.New(log.WithStdout()).With(log.WithSource(1, filepath.Base))
logger.Debug(ctx, "debug message")
// Output:
// msg="debug message" source=source_example_test.go:14
}
func ExampleWithSource_json() {
ctx := context.Background()
logger := log.New(log.WithStdout(), log.WithJSONFormat()).With(log.WithSource(2, filepath.Base))
logger.Debug(ctx, "debug message")
// Output:
// {"msg":"debug message","source":{"file":"source_example_test.go","line":23,"func":"log_test.ExampleWithSource_json"}}
}

37
writer_example_test.go Normal file
View File

@@ -0,0 +1,37 @@
package log_test
import (
"context"
"math"
"path/filepath"
"time"
"gitoa.ru/go-4devs/log"
"gitoa.ru/go-4devs/log/entry"
"gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/level"
)
func exampleWithTime(key, format string) log.Middleware {
return func(ctx context.Context, e *entry.Entry, handler log.Logger) (int, error) {
return handler(ctx, e.Add(field.FormatTime(key, format, time.Unix(math.MaxInt32, 0).In(time.UTC))))
}
}
func ExampleFormatWithBracket() {
ctx := context.Background()
logger := log.New(log.WithFormat(log.FormatWithBracket(field.NewEncoderText())), log.WithStdout()).With(
log.WithSource(10, filepath.Base),
// log.WithTime(log.KeyTime, time.RFC3339),
exampleWithTime(log.KeyTime, time.RFC3339),
log.WithLevel(log.KeyLevel, level.Info),
)
logger.InfoKV(ctx, "imfo message", field.Int64("num", 42))
serviceLogger := logger.With(log.WithName("service_name"))
serviceLogger.Err(ctx, "error message")
// Output:
// 2038-01-19T03:14:07Z [info] writer_example_test.go:30 "imfo message" num=42
// 2038-01-19T03:14:07Z [error][service_name] writer_example_test.go:33 "error message"
}

View File

@@ -13,18 +13,20 @@ import (
// Keys for "built-in" attributes. // Keys for "built-in" attributes.
const ( const (
// TimeKey is the key used by the built-in handlers for the time // KeyTime 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"
// LevelKey is the key used by the built-in handlers for the level // KeyLevel 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"
// MessageKey is the key used by the built-in handlers for the // KeyMessage 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"
// SourceKey is the key used by the built-in handlers for the source file // KeySource 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 = "name"
) )
func WithWriter(w io.Writer) func(*option) { func WithWriter(w io.Writer) func(*option) {
@@ -41,15 +43,18 @@ func WithStdout() func(*option) {
// WithStringFormat sets format as simple string. // WithStringFormat sets format as simple string.
func WithStringFormat() func(*option) { func WithStringFormat() func(*option) {
return func(o *option) { return WithFormat(FormatString(field.NewEncoderText()))
o.format = formatText()
}
} }
// WithJSONFormat sets json output format. // WithJSONFormat sets json output format.
func WithJSONFormat() func(*option) { func WithJSONFormat() func(*option) {
return WithFormat(FormatJSON(field.NewEncoderJSON()))
}
// WithFormat sets custom output format.
func WithFormat(format func(io.Writer, *entry.Entry) (int, error)) func(*option) {
return func(o *option) { return func(o *option) {
o.format = formatJSON() o.format = format
} }
} }
@@ -61,7 +66,7 @@ type option struct {
// New creates standart logger. // New creates standart logger.
func New(opts ...func(*option)) Logger { func New(opts ...func(*option)) Logger {
log := option{ log := option{
format: formatText(), format: FormatString(field.NewEncoderText()),
out: os.Stderr, out: os.Stderr,
} }
@@ -74,11 +79,68 @@ func New(opts ...func(*option)) Logger {
} }
} }
func formatText() func(io.Writer, *entry.Entry) (int, error) { type Encoder interface {
enc := field.NewEncoderText() AppendValue(dst []byte, val field.Value) []byte
AppendField(dst []byte, val field.Field) []byte
}
func FormatWithBracket(enc Encoder) func(io.Writer, *entry.Entry) (int, error) {
appendValue := func(buf *buffer.Buffer, data field.Fields, key, prefix, suffix string) *buffer.Buffer {
data.Fields(
func(f field.Field) bool {
if f.IsKey(key) {
_, _ = buf.WriteString(prefix)
*buf = enc.AppendValue(*buf, f.Value)
_, _ = buf.WriteString(suffix)
return false
}
return true
})
return buf
}
return func(w io.Writer, data *entry.Entry) (int, error) {
buf := buffer.New()
defer func() {
buf.Free()
}()
fields := data.Fields()
buf = appendValue(buf, fields, KeyTime, "", " ")
_, _ = buf.WriteString("[")
*buf = enc.AppendValue(*buf, field.StringValue(data.Level().String()))
_, _ = buf.WriteString("]")
buf = appendValue(buf, fields, KeyName, "[", "]")
buf = appendValue(buf, fields, KeySource, " ", " ")
*buf = enc.AppendValue(*buf, field.StringValue(data.Message()))
fields.Fields(func(f field.Field) bool {
if !f.IsKey(KeyTime, KeySource, KeyName, KeyLevel) {
*buf = enc.AppendField(*buf, f)
}
return true
})
_, _ = buf.WriteString("\n")
n, err := w.Write(*buf)
if err != nil {
return 0, fmt.Errorf("format text:%w", err)
}
return n, nil
}
}
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()
}() }()
@@ -100,11 +162,10 @@ func formatText() func(io.Writer, *entry.Entry) (int, error) {
} }
} }
func formatJSON() func(w io.Writer, entry *entry.Entry) (int, error) { func FormatJSON(enc Encoder) func(w io.Writer, entry *entry.Entry) (int, error) {
enc := field.NewEncoderJSON()
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()
}() }()