6 changed files with 167 additions and 0 deletions
@ -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 |
@ -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= |
@ -0,0 +1,47 @@ |
|||||
|
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 |
||||
|
} |
||||
|
} |
@ -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
|
||||
|
} |
@ -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) |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue