Compare commits
1 Commits
master
...
2628202ff1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2628202ff1 |
@@ -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...)
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
//nolint:gochecknoglobals
|
//nolint:gochecknoglobals
|
||||||
var global = With(New(),
|
var global = With(New(),
|
||||||
WithCaller(KeySource, 1, false),
|
WithSource(1),
|
||||||
WithLevel(KeyLevel, level.Debug),
|
WithLevel(KeyLevel, level.Debug),
|
||||||
WithExit(level.Alert),
|
WithExit(level.Alert),
|
||||||
WithPanic(level.Emergency),
|
WithPanic(level.Emergency),
|
||||||
|
|||||||
24
global_example_test.go
Normal file
24
global_example_test.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package log_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/log"
|
||||||
|
"gitoa.ru/go-4devs/log/level"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleDebug() {
|
||||||
|
logger := log.With(log.New(log.WithStdout()),
|
||||||
|
log.WithSource(2),
|
||||||
|
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:21 level=debug
|
||||||
|
}
|
||||||
@@ -9,14 +9,14 @@ func ExampleNew_withCaller() {
|
|||||||
logger := log.With(
|
logger := log.With(
|
||||||
log.New(log.WithStdout()),
|
log.New(log.WithStdout()),
|
||||||
log.WithLevel("level", level.Debug),
|
log.WithLevel("level", level.Debug),
|
||||||
log.WithCaller("caller", 2, false),
|
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 caller=logger_example_caller_test.go:14
|
// msg="same error message" level=error source=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 info message" api-version=0.1 level=info source=logger_example_caller_test.go:15
|
||||||
// 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:16
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ func WithContextValue(keys ...fmt.Stringer) Middleware {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
|
|||||||
45
source.go
Normal file
45
source.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
|
"gitoa.ru/go-4devs/log/field"
|
||||||
|
)
|
||||||
|
|
||||||
|
func WithSource(depth int) Middleware {
|
||||||
|
const offset = 3
|
||||||
|
|
||||||
|
return func(ctx context.Context, data *entry.Entry, handler Logger) (int, error) {
|
||||||
|
pc, file, line, has := runtime.Caller(depth + offset)
|
||||||
|
if !has {
|
||||||
|
return handler(ctx, data.AddAny(KeyLevel, field.NilValue()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fnc := runtime.FuncForPC(pc)
|
||||||
|
|
||||||
|
return handler(ctx, data.AddAny(KeySource, Source{
|
||||||
|
Func: filepath.Base(fnc.Name()),
|
||||||
|
File: filepath.Base(file),
|
||||||
|
Line: line,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 []byte(fmt.Sprintf("%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
|
||||||
|
}
|
||||||
25
source_example_test.go
Normal file
25
source_example_test.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package log_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleWithSource() {
|
||||||
|
ctx := context.Background()
|
||||||
|
logger := log.New(log.WithStdout()).With(log.WithSource(1))
|
||||||
|
|
||||||
|
logger.Debug(ctx, "debug message")
|
||||||
|
// Output:
|
||||||
|
// msg="debug message" source=source_example_test.go:13
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleWithSource_json() {
|
||||||
|
ctx := context.Background()
|
||||||
|
logger := log.New(log.WithStdout(), log.WithJSONFormat()).With(log.WithSource(1))
|
||||||
|
|
||||||
|
logger.Debug(ctx, "debug message")
|
||||||
|
// Output:
|
||||||
|
// {"msg":"debug message","source":{"file":"source_example_test.go","line":22,"func":"log_test.ExampleWithSource_json"}}
|
||||||
|
}
|
||||||
@@ -62,7 +62,7 @@ type option struct {
|
|||||||
func New(opts ...func(*option)) Logger {
|
func New(opts ...func(*option)) Logger {
|
||||||
log := option{
|
log := option{
|
||||||
format: formatText(),
|
format: formatText(),
|
||||||
out: os.Stderr,
|
out: os.Stdout,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range opts {
|
for _, opt := range opts {
|
||||||
|
|||||||
Reference in New Issue
Block a user