Browse Source

remove handlers

pull/9/head v0.5.1
andrey 12 months ago
parent
commit
e7774808fd
  1. 17
      go.mod
  2. 81
      go.sum
  3. 47
      handler/logrus/logger.go
  4. 38
      handler/logrus/logger_test.go
  5. 59
      handler/otel/helpers.go
  6. 16
      handler/otel/level.go
  7. 51
      handler/otel/level_string.go
  8. 16
      handler/otel/logger.go
  9. 16
      handler/otel/middleware.go
  10. 66
      handler/zap/logger.go
  11. 43
      handler/zap/logger_test.go
  12. 28
      logger_example_logrus_test.go
  13. 61
      logger_example_trace_test.go
  14. 21
      logger_example_zap_test.go

17
go.mod

@ -1,20 +1,3 @@
module gitoa.ru/go-4devs/log
go 1.20
require (
github.com/sirupsen/logrus v1.8.1
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/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.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

81
go.sum

@ -1,81 +0,0 @@
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/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/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/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 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 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.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-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/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

47
handler/logrus/logger.go

@ -1,47 +0,0 @@
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.
func Standard() log.Logger {
return New(logrus.StandardLogger())
}
// New create new logrus handler.
func New(log *logrus.Logger) log.Logger {
return func(ctx context.Context, data *entry.Entry) (int, error) {
lrgFields := make(logrus.Fields, data.Fields().Len())
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
}
}

38
handler/logrus/logger_test.go

@ -1,38 +0,0 @@
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)
}
}

59
handler/otel/helpers.go

@ -1,59 +0,0 @@
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

@ -1,16 +0,0 @@
package otel
//go:generate stringer -type=Level -linecomment -output=level_string.go
type Level int
const (
levelDebug Level = 5 // DEBUG
levelInfo Level = 9 // INFO
levelInfo2 Level = 10 // INFO2
levelWarn Level = 13 // WARN
levelError Level = 17 // ERROR
levelError2 Level = 18 // ERROR2
levelError3 Level = 19 // ERROR3
levelFatal Level = 21 // FATAL
)

51
handler/otel/level_string.go

@ -1,51 +0,0 @@
// Code generated by "stringer -type=Level -linecomment -output=level_string.go"; DO NOT EDIT.
package otel
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[levelDebug-5]
_ = x[levelInfo-9]
_ = x[levelInfo2-10]
_ = x[levelWarn-13]
_ = x[levelError-17]
_ = x[levelError2-18]
_ = x[levelError3-19]
_ = x[levelFatal-21]
}
const (
_Level_name_0 = "DEBUG"
_Level_name_1 = "INFOINFO2"
_Level_name_2 = "WARN"
_Level_name_3 = "ERRORERROR2ERROR3"
_Level_name_4 = "FATAL"
)
var (
_Level_index_1 = [...]uint8{0, 4, 9}
_Level_index_3 = [...]uint8{0, 5, 11, 17}
)
func (i Level) String() string {
switch {
case i == 5:
return _Level_name_0
case 9 <= i && i <= 10:
i -= 9
return _Level_name_1[_Level_index_1[i]:_Level_index_1[i+1]]
case i == 13:
return _Level_name_2
case 17 <= i && i <= 19:
i -= 17
return _Level_name_3[_Level_index_3[i]:_Level_index_3[i+1]]
case i == 21:
return _Level_name_4
default:
return "Level(" + strconv.FormatInt(int64(i), 10) + ")"
}
}

16
handler/otel/logger.go

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

16
handler/otel/middleware.go

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

66
handler/zap/logger.go

@ -1,66 +0,0 @@
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"
)
func Nop() log.Logger {
return New(zap.NewNop())
}
func Example(options ...zap.Option) log.Logger {
return New(zap.NewExample(options...))
}
func Production(options ...zap.Option) log.Logger {
z, err := zap.NewProduction(options...)
if err != nil {
panic(err)
}
return New(z)
}
func Development(options ...zap.Option) log.Logger {
z, err := zap.NewDevelopment(options...)
if err != nil {
panic(err)
}
return New(z)
}
// New create handler by zap logger.
func New(logger *zap.Logger) log.Logger {
return func(ctx context.Context, data *entry.Entry) (int, error) {
zf := make([]zap.Field, 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
}
}

43
handler/zap/logger_test.go

@ -1,43 +0,0 @@
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)
}
}

28
logger_example_logrus_test.go

@ -1,28 +0,0 @@
package log_test
import (
"io"
"os"
slogrus "github.com/sirupsen/logrus"
"gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/handler/logrus"
)
func ExampleNew_logrusHandler() {
lgrs := slogrus.New()
lgrs.SetOutput(os.Stdout)
lgrs.SetFormatter(&slogrus.TextFormatter{
DisableTimestamp: true,
})
log := logrus.New(lgrs)
log.Err(ctx, "log logrus")
log.ErrKV(ctx, "log logrus kv", field.Int("int", 42))
log.ErrKVs(ctx, "log logrus kv sugar", "err", io.EOF)
// Output:
// level=error msg="log logrus"
// level=error msg="log logrus kv" int=42
// level=error msg="log logrus kv sugar" err=EOF
}

61
logger_example_trace_test.go

@ -1,61 +0,0 @@
package log_test
import (
"context"
"fmt"
"io"
"gitoa.ru/go-4devs/log"
"gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/handler/otel"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
)
func ExampleNew_withTrace() {
logger := log.New(log.WithStdout()).With(otel.Middleware())
sctx, span := startSpan(ctx)
logger.Err(sctx, "log logrus")
logger.ErrKV(sctx, "log logrus kv", field.Int("int", 42))
logger.ErrKVs(sctx, "log logrus kv sugar", "err", io.EOF)
span.End()
// Output:
// msg="log logrus"
// msg="log logrus kv" int=42
// msg="log logrus kv sugar" err=EOF
// event: log logrus, SeverityText = ERROR, SeverityNumber = 17
// event: log logrus kv, SeverityText = ERROR, SeverityNumber = 17, int = 42
// event: log logrus kv sugar, SeverityText = ERROR, SeverityNumber = 17, err = EOF
}
func startSpan(ctx context.Context) (context.Context, trace.Span) {
tp := sdktrace.NewTracerProvider(sdktrace.WithSyncer(exporter{}))
return tp.Tracer("logger").Start(ctx, "operation")
}
type exporter struct{}
func (e exporter) Shutdown(_ context.Context) error {
return nil
}
func (e exporter) ExportSpans(_ 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
}

21
logger_example_zap_test.go

@ -1,21 +0,0 @@
package log_test
import (
"io"
"gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/handler/zap"
uzap "go.uber.org/zap"
)
func ExampleNew_zapHandler() {
log := zap.New(uzap.NewExample())
log.Err(ctx, "log zap")
log.ErrKV(ctx, "log zap kv", field.Int("int", 42))
log.ErrKVs(ctx, "log zap kv sugar", "err", io.EOF)
// Output:
// {"level":"error","msg":"log zap"}
// {"level":"error","msg":"log zap kv","int":42}
// {"level":"error","msg":"log zap kv sugar","err":"EOF"}
}
Loading…
Cancel
Save