Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 81eaf8c8b2 | |||
| 7570ade82a |
@@ -3,7 +3,7 @@ name: default
|
||||
|
||||
steps:
|
||||
- name: test
|
||||
image: golang:1.17.8
|
||||
image: golang:1.18.1
|
||||
volumes:
|
||||
- name: deps
|
||||
path: /go/src/mod
|
||||
@@ -11,7 +11,7 @@ steps:
|
||||
- go test
|
||||
|
||||
- name: golangci-lint
|
||||
image: golangci/golangci-lint:v1.44
|
||||
image: golangci/golangci-lint:v1.49
|
||||
commands:
|
||||
- golangci-lint run
|
||||
|
||||
|
||||
@@ -37,6 +37,17 @@ linters-settings:
|
||||
|
||||
linters:
|
||||
enable-all: true
|
||||
disable:
|
||||
- exhaustivestruct
|
||||
- maligned
|
||||
- deadcode
|
||||
- interfacer
|
||||
- golint
|
||||
- varcheck
|
||||
- nosnakecase
|
||||
- scopelint
|
||||
- ifshort
|
||||
- structcheck
|
||||
|
||||
issues:
|
||||
# Excluding configuration per-path, per-linter, per-text and per-source
|
||||
@@ -44,5 +55,5 @@ issues:
|
||||
- path: _test\.go
|
||||
linters:
|
||||
- gomnd
|
||||
- exhaustivestruct
|
||||
- ireturn
|
||||
- exhaustruct
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
MIT License Copyright (c) 2020 go-4devs
|
||||
MIT License Copyright (c) 2020-2022 4devs
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -118,7 +118,7 @@ func fakeSugarFields() []interface{} {
|
||||
}
|
||||
|
||||
func NewLogger() log.Logger {
|
||||
return log.New(log.WithWriter(ioutil.Discard))
|
||||
return log.New(log.WithWriter(io.Discard))
|
||||
}
|
||||
|
||||
func BenchmarkDisabledWithoutFields(b *testing.B) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func Caller(depth int, full bool) string {
|
||||
const offset = 4
|
||||
const offset = 3
|
||||
_, file, line, has := runtime.Caller(depth + offset)
|
||||
|
||||
if !has {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package entry
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/log/field"
|
||||
@@ -27,7 +28,14 @@ func WithFields(fields ...field.Field) Option {
|
||||
|
||||
func WithMessage(msg string) Option {
|
||||
return func(e *Entry) {
|
||||
e.msg = msg
|
||||
e.format = msg
|
||||
}
|
||||
}
|
||||
|
||||
func WithMessagef(format string, args ...interface{}) Option {
|
||||
return func(e *Entry) {
|
||||
e.format = format
|
||||
e.args = args
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +49,8 @@ func New(opts ...Option) *Entry {
|
||||
entry := &Entry{
|
||||
fields: make(field.Fields, 0, defaultCap+1),
|
||||
level: level.Debug,
|
||||
msg: "",
|
||||
format: "",
|
||||
args: make([]interface{}, 0, defaultCap+1),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -53,13 +62,16 @@ func New(opts ...Option) *Entry {
|
||||
|
||||
// Entry slice field.
|
||||
type Entry struct {
|
||||
msg string
|
||||
format string
|
||||
args []interface{}
|
||||
level level.Level
|
||||
fields field.Fields
|
||||
}
|
||||
|
||||
func (e *Entry) Reset() {
|
||||
e.fields = e.fields[:0]
|
||||
e.args = e.args[:0]
|
||||
e.format = ""
|
||||
}
|
||||
|
||||
func (e *Entry) Fields() field.Fields {
|
||||
@@ -73,7 +85,7 @@ func (e *Entry) String() string {
|
||||
}
|
||||
|
||||
str := make([]string, len(e.fields)+1)
|
||||
str[0] = e.msg
|
||||
str[0] = e.Message()
|
||||
|
||||
for i, field := range e.fields {
|
||||
str[i+1] = field.String()
|
||||
@@ -83,7 +95,14 @@ func (e *Entry) String() string {
|
||||
}
|
||||
|
||||
func (e *Entry) Message() string {
|
||||
return e.msg
|
||||
switch {
|
||||
case len(e.args) > 0 && e.format != "":
|
||||
return fmt.Sprintf(e.format, e.args...)
|
||||
case len(e.args) > 0:
|
||||
return fmt.Sprint(e.args...)
|
||||
default:
|
||||
return e.format
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Entry) Level() level.Level {
|
||||
@@ -109,7 +128,18 @@ func (e *Entry) SetMessage(msg string) *Entry {
|
||||
return New().SetMessage(msg)
|
||||
}
|
||||
|
||||
e.msg = msg
|
||||
e.format = msg
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func (e *Entry) SetMessagef(format string, args ...interface{}) *Entry {
|
||||
if e == nil {
|
||||
return New().SetMessagef(format, args...)
|
||||
}
|
||||
|
||||
e.format = format
|
||||
e.args = append(e.args[:0], args...)
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package field
|
||||
|
||||
import "time"
|
||||
|
||||
//nolint:interfacebloat
|
||||
type Encoder interface {
|
||||
// Built-in types.
|
||||
AddArray(key string, value Value)
|
||||
|
||||
@@ -261,7 +261,7 @@ func (f Field) AddTo(enc Encoder) {
|
||||
|
||||
switch {
|
||||
case f.value.IsArray():
|
||||
enc.AddAny(key, f.value)
|
||||
enc.AddArray(key, f.value)
|
||||
case f.value.IsNil():
|
||||
enc.AddNil(key)
|
||||
case f.value.IsBool():
|
||||
|
||||
@@ -3,7 +3,6 @@ package field_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitoa.ru/go-4devs/log/field"
|
||||
)
|
||||
|
||||
@@ -13,5 +12,7 @@ func TestFields_Append(t *testing.T) {
|
||||
fields := field.Fields{field.Any("any", "value")}
|
||||
fields = fields.Append(field.String("string", "value"))
|
||||
|
||||
require.Len(t, fields, 2)
|
||||
if len(fields) != 2 {
|
||||
t.Fatalf("require 2 field got %v", len(fields))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
type Key string
|
||||
|
||||
//nolint: gocyclo,funlen,cyclop
|
||||
//nolint:funlen,cyclop,gocyclo
|
||||
func (k Key) Any(value interface{}) Field {
|
||||
switch val := value.(type) {
|
||||
case string:
|
||||
|
||||
13
go.mod
13
go.mod
@@ -4,19 +4,16 @@ go 1.17
|
||||
|
||||
require (
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/stretchr/testify v1.7.0
|
||||
go.opentelemetry.io/otel v0.20.0
|
||||
go.opentelemetry.io/otel/sdk v0.20.0
|
||||
go.opentelemetry.io/otel/trace v0.20.0
|
||||
go.opentelemetry.io/otel v1.9.0
|
||||
go.opentelemetry.io/otel/sdk v1.9.0
|
||||
go.opentelemetry.io/otel/trace v1.9.0
|
||||
go.uber.org/zap v1.21.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v0.20.0 // indirect
|
||||
github.com/go-logr/logr v1.2.3 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
go.uber.org/atomic v1.7.0 // indirect
|
||||
go.uber.org/multierr v1.6.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
||||
)
|
||||
|
||||
38
go.sum
38
go.sum
@@ -3,12 +3,15 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
|
||||
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
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.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
|
||||
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
@@ -19,28 +22,22 @@ github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
||||
go.opentelemetry.io/otel v0.20.0 h1:eaP0Fqu7SXHwvjiqDq83zImeehOHX8doTvU9AwXON8g=
|
||||
go.opentelemetry.io/otel v0.20.0/go.mod h1:Y3ugLH2oa81t5QO+Lty+zXf8zC9L26ax4Nzoxm/dooo=
|
||||
go.opentelemetry.io/otel/metric v0.20.0 h1:4kzhXFP+btKm4jwxpjIqjs41A7MakRFUS86bqLHTIw8=
|
||||
go.opentelemetry.io/otel/metric v0.20.0/go.mod h1:598I5tYlH1vzBjn+BTuhzTCSb/9debfNp6R3s7Pr1eU=
|
||||
go.opentelemetry.io/otel/oteltest v0.20.0 h1:HiITxCawalo5vQzdHfKeZurV8x7ljcqAgiWzF6Vaeaw=
|
||||
go.opentelemetry.io/otel/oteltest v0.20.0/go.mod h1:L7bgKf9ZB7qCwT9Up7i9/pn0PWIa9FqQ2IQ8LoxiGnw=
|
||||
go.opentelemetry.io/otel/sdk v0.20.0 h1:JsxtGXd06J8jrnya7fdI/U/MR6yXA5DtbZy+qoHQlr8=
|
||||
go.opentelemetry.io/otel/sdk v0.20.0/go.mod h1:g/IcepuwNsoiX5Byy2nNV0ySUF1em498m7hBWC279Yc=
|
||||
go.opentelemetry.io/otel/trace v0.20.0 h1:1DL6EXUdcg95gukhuRRvLDO/4X5THh/5dIV52lqtnbw=
|
||||
go.opentelemetry.io/otel/trace v0.20.0/go.mod h1:6GjCW8zgDjwGHGa6GkyeB8+/5vjT16gUEi0Nf1iBdgw=
|
||||
go.opentelemetry.io/otel v1.9.0 h1:8WZNQFIB2a71LnANS9JeyidJKKGOOremcUtb/OtHISw=
|
||||
go.opentelemetry.io/otel v1.9.0/go.mod h1:np4EoPGzoPs3O67xUVNoPPcmSvsfOxNlNA4F4AC+0Eo=
|
||||
go.opentelemetry.io/otel/sdk v1.9.0 h1:LNXp1vrr83fNXTHgU8eO89mhzxb/bbWAsHG6fNf3qWo=
|
||||
go.opentelemetry.io/otel/sdk v1.9.0/go.mod h1:AEZc8nt5bd2F7BC24J5R0mrjYnpEgYHyTcM/vrSple4=
|
||||
go.opentelemetry.io/otel/trace v1.9.0 h1:oZaCNJUjWcg60VXWee8lJKlqhPbXAPB51URuR47pQYc=
|
||||
go.opentelemetry.io/otel/trace v1.9.0/go.mod h1:2737Q0MuG8q1uILYm2YYVkAyLtOofiTNGg6VODnOiPo=
|
||||
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
|
||||
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
|
||||
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723 h1:sHOAIxRGBp443oHZIPB+HsUGaksVCXVQENPxwTfQdH4=
|
||||
go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
|
||||
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
|
||||
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
|
||||
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
|
||||
go.uber.org/zap v1.19.1 h1:ue41HOKd1vGURxrmeKIgELGb3jPW9DMUDGtsinblHwI=
|
||||
go.uber.org/zap v1.19.1/go.mod h1:j3DNczoxDZroyBnOT1L/Q79cfUMGZxlv/9dzN7SM1rI=
|
||||
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
|
||||
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
@@ -58,6 +55,7 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
@@ -69,10 +67,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
|
||||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
||||
114
logger.go
114
logger.go
@@ -28,38 +28,28 @@ func writeOutput(_ int, err error) {
|
||||
// Logger logged message.
|
||||
type Logger func(ctx context.Context, entry *entry.Entry) (int, error)
|
||||
|
||||
func (l Logger) log(ctx context.Context, level level.Level, args ...interface{}) {
|
||||
writeOutput(l.write(ctx, level, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
func (l Logger) Write(in []byte) (int, error) {
|
||||
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) {
|
||||
writeEntry := entry.Get()
|
||||
data := entry.Get()
|
||||
|
||||
defer func() {
|
||||
entry.Put(writeEntry)
|
||||
entry.Put(data)
|
||||
}()
|
||||
|
||||
return l(ctx, writeEntry.SetLevel(level).SetMessage(msg).Add(fields...))
|
||||
return l(ctx, data.SetLevel(level).SetMessage(msg).Add(fields...))
|
||||
}
|
||||
|
||||
func (l Logger) logKVs(ctx context.Context, level level.Level, msg string, args ...interface{}) {
|
||||
writeOutput(l.write(ctx, level, msg, l.kv(ctx, args...)...))
|
||||
}
|
||||
func (l Logger) writef(ctx context.Context, level level.Level, format string, args ...interface{}) (int, error) {
|
||||
data := entry.Get()
|
||||
|
||||
func (l Logger) logKV(ctx context.Context, level level.Level, msg string, fields ...field.Field) {
|
||||
writeOutput(l.write(ctx, level, msg, fields...))
|
||||
}
|
||||
defer func() {
|
||||
entry.Put(data)
|
||||
}()
|
||||
|
||||
func (l Logger) logf(ctx context.Context, level level.Level, format string, args ...interface{}) {
|
||||
writeOutput(l.write(ctx, level, fmt.Sprintf(format, args...)))
|
||||
}
|
||||
|
||||
func (l Logger) logln(ctx context.Context, level level.Level, args ...interface{}) {
|
||||
writeOutput(l.write(ctx, level, fmt.Sprintln(args...)))
|
||||
return l(ctx, data.SetLevel(level).SetMessagef(format, args...))
|
||||
}
|
||||
|
||||
func (l Logger) kv(ctx context.Context, args ...interface{}) field.Fields {
|
||||
@@ -77,7 +67,7 @@ func (l Logger) kv(ctx context.Context, args ...interface{}) field.Fields {
|
||||
}
|
||||
|
||||
if i == len(args)-1 {
|
||||
l.logKV(ctx, level.Critical, fmt.Sprint("Ignored key without a value.", args[i]), kvEntry.Fields()...)
|
||||
writeOutput(l.write(ctx, level.Critical, fmt.Sprint("Ignored key without a value.", args[i]), kvEntry.Fields()...))
|
||||
|
||||
break
|
||||
}
|
||||
@@ -91,7 +81,7 @@ func (l Logger) kv(ctx context.Context, args ...interface{}) field.Fields {
|
||||
continue
|
||||
}
|
||||
|
||||
l.logKV(ctx, level.Critical, fmt.Sprint("Ignored key-value pairs with non-string keys.", key, val), kvEntry.Fields()...)
|
||||
writeOutput(l.write(ctx, level.Critical, fmt.Sprint("Ignored key-value pairs with non-string keys.", key, val), kvEntry.Fields()...))
|
||||
}
|
||||
|
||||
return kvEntry.Fields()
|
||||
@@ -104,207 +94,207 @@ func (l Logger) With(mw ...Middleware) Logger {
|
||||
|
||||
// Emerg log by emergency level.
|
||||
func (l Logger) Emerg(ctx context.Context, args ...interface{}) {
|
||||
l.log(ctx, level.Emergency, args...)
|
||||
writeOutput(l.writef(ctx, level.Emergency, "", args...))
|
||||
}
|
||||
|
||||
// Alert log by alert level.
|
||||
func (l Logger) Alert(ctx context.Context, args ...interface{}) {
|
||||
l.log(ctx, level.Alert, args...)
|
||||
writeOutput(l.writef(ctx, level.Alert, "", args...))
|
||||
}
|
||||
|
||||
// Crit log by critical level.
|
||||
func (l Logger) Crit(ctx context.Context, args ...interface{}) {
|
||||
l.log(ctx, level.Critical, args...)
|
||||
writeOutput(l.writef(ctx, level.Critical, "", args...))
|
||||
}
|
||||
|
||||
// Err log by error level.
|
||||
func (l Logger) Err(ctx context.Context, args ...interface{}) {
|
||||
l.log(ctx, level.Error, args...)
|
||||
writeOutput(l.writef(ctx, level.Error, "", args...))
|
||||
}
|
||||
|
||||
// Warn log by warning level.
|
||||
func (l Logger) Warn(ctx context.Context, args ...interface{}) {
|
||||
l.log(ctx, level.Warning, args...)
|
||||
writeOutput(l.writef(ctx, level.Warning, "", args...))
|
||||
}
|
||||
|
||||
// Notice log by notice level.
|
||||
func (l Logger) Notice(ctx context.Context, args ...interface{}) {
|
||||
l.log(ctx, level.Notice, args...)
|
||||
writeOutput(l.writef(ctx, level.Notice, "", args...))
|
||||
}
|
||||
|
||||
// Info log by info level.
|
||||
func (l Logger) Info(ctx context.Context, args ...interface{}) {
|
||||
l.log(ctx, level.Info, args...)
|
||||
writeOutput(l.writef(ctx, level.Info, "", args...))
|
||||
}
|
||||
|
||||
// Debug log by debug level.
|
||||
func (l Logger) Debug(ctx context.Context, args ...interface{}) {
|
||||
l.log(ctx, level.Debug, args...)
|
||||
writeOutput(l.writef(ctx, level.Debug, "", args...))
|
||||
}
|
||||
|
||||
// Print log by info level and arguments.
|
||||
func (l Logger) Print(args ...interface{}) {
|
||||
l.log(context.Background(), level.Info, args...)
|
||||
writeOutput(l.writef(context.Background(), level.Info, "", args...))
|
||||
}
|
||||
|
||||
// Fatal log by alert level and arguments.
|
||||
func (l Logger) Fatal(args ...interface{}) {
|
||||
l.log(context.Background(), level.Alert, args...)
|
||||
writeOutput(l.writef(context.Background(), level.Alert, "", args...))
|
||||
}
|
||||
|
||||
// Panic log by emergency level and arguments.
|
||||
func (l Logger) Panic(args ...interface{}) {
|
||||
l.log(context.Background(), level.Emergency, args...)
|
||||
writeOutput(l.writef(context.Background(), level.Emergency, "", args...))
|
||||
}
|
||||
|
||||
// Println log by info level and arguments.
|
||||
func (l Logger) Println(args ...interface{}) {
|
||||
l.logln(context.Background(), level.Info, args...)
|
||||
writeOutput(l.write(context.Background(), level.Info, fmt.Sprintln(args...)))
|
||||
}
|
||||
|
||||
// Fatalln log by alert level and arguments.
|
||||
func (l Logger) Fatalln(args ...interface{}) {
|
||||
l.logln(context.Background(), level.Alert, args...)
|
||||
writeOutput(l.write(context.Background(), level.Alert, fmt.Sprintln(args...)))
|
||||
}
|
||||
|
||||
// Panicln log by emergency level and arguments.
|
||||
func (l Logger) Panicln(args ...interface{}) {
|
||||
l.logln(context.Background(), level.Emergency, args...)
|
||||
writeOutput(l.write(context.Background(), level.Emergency, fmt.Sprintln(args...)))
|
||||
}
|
||||
|
||||
// EmergKVs sugared log by emergency level and key-values.
|
||||
func (l Logger) EmergKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||
l.logKVs(ctx, level.Emergency, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Emergency, msg, l.kv(ctx, args...)...))
|
||||
}
|
||||
|
||||
// AlertKVs sugared log by alert level and key-values.
|
||||
func (l Logger) AlertKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||
l.logKVs(ctx, level.Alert, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Alert, msg, l.kv(ctx, args...)...))
|
||||
}
|
||||
|
||||
// CritKVs sugared log by critcal level and key-values.
|
||||
func (l Logger) CritKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||
l.logKVs(ctx, level.Critical, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Critical, msg, l.kv(ctx, args...)...))
|
||||
}
|
||||
|
||||
// ErrKVs sugared log by error level and key-values.
|
||||
func (l Logger) ErrKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||
l.logKVs(ctx, level.Error, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Error, msg, l.kv(ctx, args...)...))
|
||||
}
|
||||
|
||||
// WarnKVs sugared log by warning level and key-values.
|
||||
func (l Logger) WarnKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||
l.logKVs(ctx, level.Warning, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Warning, msg, l.kv(ctx, args...)...))
|
||||
}
|
||||
|
||||
// NoticeKVs sugared log by notice level and key-values.
|
||||
func (l Logger) NoticeKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||
l.logKVs(ctx, level.Notice, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Notice, msg, l.kv(ctx, args...)...))
|
||||
}
|
||||
|
||||
// InfoKVs sugared log by info level and key-values.
|
||||
func (l Logger) InfoKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||
l.logKVs(ctx, level.Info, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Info, msg, l.kv(ctx, args...)...))
|
||||
}
|
||||
|
||||
// DebugKVs sugared log by debug level and key-values.
|
||||
func (l Logger) DebugKVs(ctx context.Context, msg string, args ...interface{}) {
|
||||
l.logKVs(ctx, level.Debug, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Debug, msg, l.kv(ctx, args...)...))
|
||||
}
|
||||
|
||||
// EmergKV log by emergency level and key-values.
|
||||
func (l Logger) EmergKV(ctx context.Context, msg string, args ...field.Field) {
|
||||
l.logKV(ctx, level.Emergency, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Emergency, msg, args...))
|
||||
}
|
||||
|
||||
// AlertKV log by alert level and key-values.
|
||||
func (l Logger) AlertKV(ctx context.Context, msg string, args ...field.Field) {
|
||||
l.logKV(ctx, level.Alert, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Alert, msg, args...))
|
||||
}
|
||||
|
||||
// CritKV log by critcal level and key-values.
|
||||
func (l Logger) CritKV(ctx context.Context, msg string, args ...field.Field) {
|
||||
l.logKV(ctx, level.Critical, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Critical, msg, args...))
|
||||
}
|
||||
|
||||
// ErrKV log by error level and key-values.
|
||||
func (l Logger) ErrKV(ctx context.Context, msg string, args ...field.Field) {
|
||||
l.logKV(ctx, level.Error, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Error, msg, args...))
|
||||
}
|
||||
|
||||
// WarnKV log by warning level and key-values.
|
||||
func (l Logger) WarnKV(ctx context.Context, msg string, args ...field.Field) {
|
||||
l.logKV(ctx, level.Warning, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Warning, msg, args...))
|
||||
}
|
||||
|
||||
// NoticeKV log by notice level and key-values.
|
||||
func (l Logger) NoticeKV(ctx context.Context, msg string, args ...field.Field) {
|
||||
l.logKV(ctx, level.Notice, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Notice, msg, args...))
|
||||
}
|
||||
|
||||
// InfoKV log by info level and key-values.
|
||||
func (l Logger) InfoKV(ctx context.Context, msg string, args ...field.Field) {
|
||||
l.logKV(ctx, level.Info, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Info, msg, args...))
|
||||
}
|
||||
|
||||
// DebugKV log by debug level and key-values.
|
||||
func (l Logger) DebugKV(ctx context.Context, msg string, args ...field.Field) {
|
||||
l.logKV(ctx, level.Debug, msg, args...)
|
||||
writeOutput(l.write(ctx, level.Debug, msg, args...))
|
||||
}
|
||||
|
||||
// Emergf log by emergency level by format and arguments.
|
||||
func (l Logger) Emergf(ctx context.Context, format string, args ...interface{}) {
|
||||
l.logf(ctx, level.Emergency, format, args...)
|
||||
writeOutput(l.writef(ctx, level.Emergency, format, args...))
|
||||
}
|
||||
|
||||
// Alertf log by alert level by format and arguments.
|
||||
func (l Logger) Alertf(ctx context.Context, format string, args ...interface{}) {
|
||||
l.logf(ctx, level.Alert, format, args...)
|
||||
writeOutput(l.writef(ctx, level.Alert, format, args...))
|
||||
}
|
||||
|
||||
// Critf log by critical level by format and arguments.
|
||||
func (l Logger) Critf(ctx context.Context, format string, args ...interface{}) {
|
||||
l.logf(ctx, level.Critical, format, args...)
|
||||
writeOutput(l.writef(ctx, level.Critical, format, args...))
|
||||
}
|
||||
|
||||
// Errf log by error level by format and arguments.
|
||||
func (l Logger) Errf(ctx context.Context, format string, args ...interface{}) {
|
||||
l.logf(ctx, level.Error, format, args...)
|
||||
writeOutput(l.writef(ctx, level.Error, format, args...))
|
||||
}
|
||||
|
||||
// Warnf log by warning level by format and arguments.
|
||||
func (l Logger) Warnf(ctx context.Context, format string, args ...interface{}) {
|
||||
l.logf(ctx, level.Warning, format, args...)
|
||||
writeOutput(l.writef(ctx, level.Warning, format, args...))
|
||||
}
|
||||
|
||||
// Noticef log by notice level by format and arguments.
|
||||
func (l Logger) Noticef(ctx context.Context, format string, args ...interface{}) {
|
||||
l.logf(ctx, level.Notice, format, args...)
|
||||
writeOutput(l.writef(ctx, level.Notice, format, args...))
|
||||
}
|
||||
|
||||
// Infof log by info level by format and arguments.
|
||||
func (l Logger) Infof(ctx context.Context, format string, args ...interface{}) {
|
||||
l.logf(ctx, level.Info, format, args...)
|
||||
writeOutput(l.writef(ctx, level.Info, format, args...))
|
||||
}
|
||||
|
||||
// Debugf log by debug level by format and arguments.
|
||||
func (l Logger) Debugf(ctx context.Context, format string, args ...interface{}) {
|
||||
l.logf(ctx, level.Debug, format, args...)
|
||||
writeOutput(l.writef(ctx, level.Debug, format, args...))
|
||||
}
|
||||
|
||||
// Printf log by info level by format and arguments without context.
|
||||
func (l Logger) Printf(format string, args ...interface{}) {
|
||||
l.logf(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.
|
||||
func (l Logger) Fatalf(format string, args ...interface{}) {
|
||||
l.logf(context.Background(), level.Alert, format, args...)
|
||||
writeOutput(l.writef(context.Background(), level.Alert, format, args...))
|
||||
}
|
||||
|
||||
// Panicf log by emergency level and arguments without context.
|
||||
func (l Logger) Panicf(format string, args ...interface{}) {
|
||||
l.logf(context.Background(), level.Emergency, format, args...)
|
||||
writeOutput(l.writef(context.Background(), level.Emergency, format, args...))
|
||||
}
|
||||
|
||||
func (l Logger) Writer(ctx context.Context, level level.Level, fields ...field.Field) io.Writer {
|
||||
|
||||
@@ -13,8 +13,10 @@ func ExampleNew_withCaller() {
|
||||
)
|
||||
logger.Err(ctx, "same error message")
|
||||
logger.InfoKVs(ctx, "same info message", "api-version", 0.1)
|
||||
_, _ = logger.Write([]byte("same write message"))
|
||||
|
||||
// Output:
|
||||
// msg="same error message" level=error caller=logger_example_caller_test.go:14
|
||||
// msg="same info message" api-version=0.1 level=info caller=logger_example_caller_test.go:15
|
||||
// msg="same write message" level=info caller=logger_example_caller_test.go:16
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ func ExampleNew_jsonFormat() {
|
||||
log.GoVersion("go-version"),
|
||||
)
|
||||
logger.Err(ctx, "same error message")
|
||||
// Output: {"go-version":"go1.17.8","level":"error","msg":"same error message"}
|
||||
// Output: {"go-version":"go1.18.1","level":"error","msg":"same error message"}
|
||||
}
|
||||
|
||||
func ExampleNew_textEncoding() {
|
||||
@@ -82,8 +82,8 @@ func ExampleNew_textEncoding() {
|
||||
logger.InfoKVs(ctx, "same info message", "api-version", 0.1)
|
||||
|
||||
// Output:
|
||||
// msg="same error message" level=error go-version=go1.17.8
|
||||
// msg="same info message" api-version=0.1 level=info go-version=go1.17.8
|
||||
// msg="same error message" level=error go-version=go1.18.1
|
||||
// msg="same info message" api-version=0.1 level=info go-version=go1.18.1
|
||||
}
|
||||
|
||||
type ctxKey string
|
||||
@@ -105,7 +105,7 @@ func ExampleWith() {
|
||||
levelInfo, log.WithContextValue(requestID), log.KeyValue("api", "0.1.0"), log.GoVersion("go"),
|
||||
)
|
||||
logger.Info(vctx, "same message")
|
||||
// Output: msg="same message" level=info requestID=6a5fa048-7181-11ea-bc55-0242ac130003 api=0.1.0 go=go1.17.8
|
||||
// Output: msg="same message" level=info requestID=6a5fa048-7181-11ea-bc55-0242ac130003 api=0.1.0 go=go1.18.1
|
||||
}
|
||||
|
||||
func ExampleLogger_Print() {
|
||||
@@ -114,7 +114,7 @@ func ExampleLogger_Print() {
|
||||
levelInfo, log.KeyValue("client", "http"), log.KeyValue("api", "0.1.0"), log.GoVersion("go"),
|
||||
)
|
||||
logger.Print("same message")
|
||||
// Output: msg="same message" level=info client=http api=0.1.0 go=go1.17.8
|
||||
// Output: msg="same message" level=info client=http api=0.1.0 go=go1.18.1
|
||||
}
|
||||
|
||||
func ExamplePrint() {
|
||||
|
||||
@@ -44,9 +44,9 @@ func (e exporter) Shutdown(_ context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e exporter) ExportSpans(ctx context.Context, spanData []*sdktrace.SpanSnapshot) error {
|
||||
func (e exporter) ExportSpans(ctx context.Context, spanData []sdktrace.ReadOnlySpan) error {
|
||||
for _, data := range spanData {
|
||||
for _, events := range data.MessageEvents {
|
||||
for _, events := range data.Events() {
|
||||
fmt.Print("event: ", events.Name)
|
||||
|
||||
for _, attr := range events.Attributes {
|
||||
|
||||
Reference in New Issue
Block a user