Compare commits
6 Commits
v0.5.2
...
9cd8f95c73
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9cd8f95c73 | ||
|
|
4a61b678cf | ||
|
|
c4cd2bdde3 | ||
|
|
52ae0b3124 | ||
|
|
11a73b8d84 | ||
| acaa46b73f |
75
.drone.yml
75
.drone.yml
@@ -20,78 +20,3 @@ steps:
|
|||||||
volumes:
|
volumes:
|
||||||
- name: deps
|
- name: deps
|
||||||
temp: {}
|
temp: {}
|
||||||
|
|
||||||
---
|
|
||||||
kind: pipeline
|
|
||||||
type: docker
|
|
||||||
name: otel
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: test
|
|
||||||
image: golang:1.21.5
|
|
||||||
volumes:
|
|
||||||
- name: deps
|
|
||||||
path: /go/src/mod
|
|
||||||
commands:
|
|
||||||
- cd handler/otel
|
|
||||||
- go test
|
|
||||||
|
|
||||||
- name: golangci-lint
|
|
||||||
image: golangci/golangci-lint:v1.55
|
|
||||||
commands:
|
|
||||||
- cd handler/otel
|
|
||||||
- golangci-lint run
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
- name: deps
|
|
||||||
temp: {}
|
|
||||||
|
|
||||||
---
|
|
||||||
kind: pipeline
|
|
||||||
type: docker
|
|
||||||
name: logrus
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: test
|
|
||||||
image: golang:1.21.5
|
|
||||||
volumes:
|
|
||||||
- name: deps
|
|
||||||
path: /go/src/mod
|
|
||||||
commands:
|
|
||||||
- cd handler/logrus
|
|
||||||
- go test
|
|
||||||
|
|
||||||
- name: golangci-lint
|
|
||||||
image: golangci/golangci-lint:v1.55
|
|
||||||
commands:
|
|
||||||
- cd handler/logrus
|
|
||||||
- golangci-lint run
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
- name: deps
|
|
||||||
temp: {}
|
|
||||||
|
|
||||||
---
|
|
||||||
kind: pipeline
|
|
||||||
type: docker
|
|
||||||
name: zap
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: test
|
|
||||||
image: golang:1.21.5
|
|
||||||
volumes:
|
|
||||||
- name: deps
|
|
||||||
path: /go/src/mod
|
|
||||||
commands:
|
|
||||||
- cd handler/zap
|
|
||||||
- go test
|
|
||||||
|
|
||||||
- name: golangci-lint
|
|
||||||
image: golangci/golangci-lint:v1.55
|
|
||||||
commands:
|
|
||||||
- cd handler/zap
|
|
||||||
- golangci-lint run
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
- name: deps
|
|
||||||
temp: {}
|
|
||||||
|
|||||||
@@ -70,3 +70,7 @@ issues:
|
|||||||
linters:
|
linters:
|
||||||
- lll
|
- lll
|
||||||
- goerr113
|
- goerr113
|
||||||
|
- path: example/*
|
||||||
|
linters:
|
||||||
|
- gomnd
|
||||||
|
- lll
|
||||||
|
|||||||
@@ -161,3 +161,24 @@ func (e *Entry) AddAny(key string, value interface{}) *Entry {
|
|||||||
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
|
||||||
|
}
|
||||||
|
|||||||
38
example/log.go
Normal file
38
example/log.go
Normal 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())).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")
|
||||||
|
}
|
||||||
@@ -501,3 +501,14 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String implent stringer.
|
||||||
|
func (f Field) IsKey(keys ...string) bool {
|
||||||
|
for _, key := range keys {
|
||||||
|
if key == f.Key {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,14 +3,15 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
//nolint:gochecknoglobals
|
//nolint:gochecknoglobals,gomnd
|
||||||
var global = With(New(),
|
var global = With(New(),
|
||||||
WithSource(2),
|
WithTime(KeyTime, time.RFC3339),
|
||||||
WithLevel(KeyLevel, level.Debug),
|
WithLevel(KeyLevel, level.Debug),
|
||||||
WithExit(level.Alert),
|
WithExit(level.Alert),
|
||||||
WithPanic(level.Emergency),
|
WithPanic(level.Emergency),
|
||||||
|
|||||||
@@ -2,14 +2,15 @@ package log_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"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 ExampleDebug() {
|
func ExampleDebug() {
|
||||||
logger := log.With(log.New(log.WithStdout()),
|
logger := log.New(log.WithStdout()).With(
|
||||||
log.WithSource(2),
|
log.WithSource(2, filepath.Base),
|
||||||
log.WithLevel(log.KeyLevel, level.Debug),
|
log.WithLevel(log.KeyLevel, level.Debug),
|
||||||
log.WithExit(level.Alert),
|
log.WithExit(level.Alert),
|
||||||
log.WithPanic(level.Emergency),
|
log.WithPanic(level.Emergency),
|
||||||
@@ -20,5 +21,5 @@ func ExampleDebug() {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
log.Debug(ctx, "debug message")
|
log.Debug(ctx, "debug message")
|
||||||
// Output:
|
// Output:
|
||||||
// msg="debug message" source=global_example_test.go:21 level=debug
|
// msg="debug message" source=global_example_test.go:22 level=debug
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,11 +11,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Standard create new standart logrus handler.
|
// Standard create new standart logrus handler.
|
||||||
|
// Deprecated: delete after 0.7.0
|
||||||
func Standard() log.Logger {
|
func Standard() log.Logger {
|
||||||
return New(logrus.StandardLogger())
|
return New(logrus.StandardLogger())
|
||||||
}
|
}
|
||||||
|
|
||||||
// New create new logrus handler.
|
// New create new logrus handler.
|
||||||
|
// Deprecated: delete after 0.7.0
|
||||||
func New(log *logrus.Logger) log.Logger {
|
func New(log *logrus.Logger) log.Logger {
|
||||||
return func(ctx context.Context, data *entry.Entry) (int, error) {
|
return func(ctx context.Context, data *entry.Entry) (int, error) {
|
||||||
lrgFields := make(logrus.Fields, data.Fields().Len())
|
lrgFields := make(logrus.Fields, data.Fields().Len())
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"gitoa.ru/go-4devs/log/entry"
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Deprecated: delete after 0.7.0
|
||||||
func New() log.Logger {
|
func New() log.Logger {
|
||||||
return func(ctx context.Context, e *entry.Entry) (int, error) {
|
return func(ctx context.Context, e *entry.Entry) (int, error) {
|
||||||
addEvent(ctx, e)
|
addEvent(ctx, e)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"gitoa.ru/go-4devs/log/entry"
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Deprecated: delete after 0.7.0
|
||||||
func Middleware() log.Middleware {
|
func Middleware() log.Middleware {
|
||||||
return func(ctx context.Context, e *entry.Entry, handler log.Logger) (int, error) {
|
return func(ctx context.Context, e *entry.Entry, handler log.Logger) (int, error) {
|
||||||
addEvent(ctx, e)
|
addEvent(ctx, e)
|
||||||
|
|||||||
@@ -10,14 +10,17 @@ import (
|
|||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Deprecated: delete after 0.7.0
|
||||||
func Nop() log.Logger {
|
func Nop() log.Logger {
|
||||||
return New(zap.NewNop())
|
return New(zap.NewNop())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: delete after 0.7.0
|
||||||
func Example(options ...zap.Option) log.Logger {
|
func Example(options ...zap.Option) log.Logger {
|
||||||
return New(zap.NewExample(options...))
|
return New(zap.NewExample(options...))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: delete after 0.7.0
|
||||||
func Production(options ...zap.Option) log.Logger {
|
func Production(options ...zap.Option) log.Logger {
|
||||||
z, err := zap.NewProduction(options...)
|
z, err := zap.NewProduction(options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -27,6 +30,7 @@ func Production(options ...zap.Option) log.Logger {
|
|||||||
return New(z)
|
return New(z)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: delete after 0.7.0
|
||||||
func Development(options ...zap.Option) log.Logger {
|
func Development(options ...zap.Option) log.Logger {
|
||||||
z, err := zap.NewDevelopment(options...)
|
z, err := zap.NewDevelopment(options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -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.WithSource(3),
|
|
||||||
)
|
)
|
||||||
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 source=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 source=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 source=logger_example_caller_test.go:16
|
// msg="same write message" level=info source=logger_example_caller_test.go:17
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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:
|
||||||
}
|
}
|
||||||
@@ -218,8 +218,8 @@ func ExampleNew_jsonFormat() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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"),
|
||||||
)
|
)
|
||||||
@@ -238,25 +238,29 @@ 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.21.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.21.5
|
||||||
@@ -277,7 +281,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 +293,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",
|
||||||
|
|||||||
@@ -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 {
|
||||||
|
|||||||
@@ -85,6 +85,12 @@ 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.
|
// Deprecated: use WithSource.
|
||||||
func WithCaller(key string, depth int, full bool) Middleware {
|
func WithCaller(key string, depth int, full bool) Middleware {
|
||||||
|
|||||||
67
source.go
67
source.go
@@ -5,28 +5,73 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/log/entry"
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
"gitoa.ru/go-4devs/log/field"
|
"gitoa.ru/go-4devs/log/field"
|
||||||
)
|
)
|
||||||
|
|
||||||
func WithSource(depth int) Middleware {
|
func WithSource(items int, trimPath func(string) string) Middleware {
|
||||||
const offset = 3
|
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) {
|
return func(ctx context.Context, data *entry.Entry, handler Logger) (int, error) {
|
||||||
pc, file, line, has := runtime.Caller(depth + offset)
|
pc := make([]uintptr, items)
|
||||||
if !has {
|
n := runtime.Callers(skip, pc)
|
||||||
return handler(ctx, data.AddAny(KeyLevel, field.NilValue()))
|
|
||||||
|
if n == 0 {
|
||||||
|
return handler(ctx, data.Add(errSourceField(skip, items)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fnc := runtime.FuncForPC(pc)
|
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{
|
return handler(ctx, data.AddAny(KeySource, Source{
|
||||||
Func: filepath.Base(fnc.Name()),
|
Func: filepath.Base(frame.Function),
|
||||||
File: filepath.Base(file),
|
Line: frame.Line,
|
||||||
Line: 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.
|
// Source describes the location of a line of source code.
|
||||||
@@ -43,3 +88,7 @@ func (l Source) MarshalText() ([]byte, error) {
|
|||||||
func (l Source) MarshalJSON() ([]byte, error) {
|
func (l Source) MarshalJSON() ([]byte, error) {
|
||||||
return fmt.Appendf([]byte{}, `{"file":"%s","line":%d,"func":"%s"}`, l.File, l.Line, l.Func), nil
|
return fmt.Appendf([]byte{}, `{"file":"%s","line":%d,"func":"%s"}`, l.File, l.Line, l.Func), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func errSourceField(skip, max int) field.Field {
|
||||||
|
return field.String(KeySource, fmt.Sprintf("source not found by frames[%d:%d]", skip, max))
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,24 +2,25 @@ package log_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/log"
|
"gitoa.ru/go-4devs/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleWithSource() {
|
func ExampleWithSource() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
logger := log.New(log.WithStdout()).With(log.WithSource(1))
|
logger := log.New(log.WithStdout()).With(log.WithSource(1, filepath.Base))
|
||||||
|
|
||||||
logger.Debug(ctx, "debug message")
|
logger.Debug(ctx, "debug message")
|
||||||
// Output:
|
// Output:
|
||||||
// msg="debug message" source=source_example_test.go:13
|
// msg="debug message" source=source_example_test.go:14
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleWithSource_json() {
|
func ExampleWithSource_json() {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
logger := log.New(log.WithStdout(), log.WithJSONFormat()).With(log.WithSource(1))
|
logger := log.New(log.WithStdout(), log.WithJSONFormat()).With(log.WithSource(2, filepath.Base))
|
||||||
|
|
||||||
logger.Debug(ctx, "debug message")
|
logger.Debug(ctx, "debug message")
|
||||||
// Output:
|
// Output:
|
||||||
// {"msg":"debug message","source":{"file":"source_example_test.go","line":22,"func":"log_test.ExampleWithSource_json"}}
|
// {"msg":"debug message","source":{"file":"source_example_test.go","line":23,"func":"log_test.ExampleWithSource_json"}}
|
||||||
}
|
}
|
||||||
|
|||||||
37
writer_example_test.go
Normal file
37
writer_example_test.go
Normal 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()), 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"
|
||||||
|
}
|
||||||
71
writter.go
71
writter.go
@@ -25,6 +25,8 @@ const (
|
|||||||
// SourceKey is the key used by the built-in handlers for the source file
|
// SourceKey is the key used by the built-in handlers for the source file
|
||||||
// and line of the log call. The associated value is a string.
|
// and line of the log call. The associated value is a string.
|
||||||
KeySource = "source"
|
KeySource = "source"
|
||||||
|
// KeyName logger name.
|
||||||
|
KeyName = "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())
|
||||||
o.format = formatText()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithJSONFormat sets json output format.
|
// WithJSONFormat sets json output format.
|
||||||
func WithJSONFormat() func(*option) {
|
func WithJSONFormat() func(*option) {
|
||||||
|
return WithFormat(formatJSON())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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(),
|
||||||
out: os.Stderr,
|
out: os.Stderr,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,7 +79,61 @@ func New(opts ...func(*option)) Logger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatText() func(io.Writer, *entry.Entry) (int, error) {
|
func FormatWithBracket() func(io.Writer, *entry.Entry) (int, error) {
|
||||||
|
enc := field.NewEncoderText()
|
||||||
|
|
||||||
|
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() func(io.Writer, *entry.Entry) (int, error) {
|
||||||
enc := field.NewEncoderText()
|
enc := field.NewEncoderText()
|
||||||
|
|
||||||
return func(w io.Writer, entry *entry.Entry) (int, error) {
|
return func(w io.Writer, entry *entry.Entry) (int, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user