You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
4.8 KiB
174 lines
4.8 KiB
package log_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math"
|
|
"os"
|
|
"sync/atomic"
|
|
|
|
"gitoa.ru/go-4devs/log"
|
|
"gitoa.ru/go-4devs/log/entry"
|
|
"gitoa.ru/go-4devs/log/field"
|
|
"gitoa.ru/go-4devs/log/level"
|
|
)
|
|
|
|
//nolint:gochecknoglobals
|
|
var ctx = context.Background()
|
|
|
|
func setStdout() {
|
|
// set stout for example by default stderror
|
|
log.SetLogger(log.New(log.WithStdout()).With(log.WithLevel(log.KeyLevel, level.Debug)))
|
|
}
|
|
|
|
func ExampleNew() {
|
|
logger := log.New(log.WithStdout())
|
|
logger.Info(ctx, "same message")
|
|
// Output: msg="same message"
|
|
}
|
|
|
|
func ExampleInfo() {
|
|
setStdout()
|
|
log.Info(ctx, "same message")
|
|
// Output: msg="same message" level=info
|
|
}
|
|
|
|
func ExampleErrKV() {
|
|
setStdout()
|
|
log.ErrKVs(ctx, "same message", "key", "addition value")
|
|
// Output: msg="same message" key="addition value" level=error
|
|
}
|
|
|
|
func ExampleNew_errf() {
|
|
logger := log.New(log.WithStdout())
|
|
logger.Errf(ctx, "same message %d", 1)
|
|
// Output: msg="same message 1"
|
|
}
|
|
|
|
func ExampleNew_debugKV() {
|
|
logger := log.New(log.WithStdout()).With(log.WithLevel("level", level.Debug))
|
|
logger.DebugKVs(ctx, "same message", "error", os.ErrNotExist)
|
|
// Output: msg="same message" error="file does not exist" level=debug
|
|
}
|
|
|
|
func ExampleNew_level() {
|
|
logger := log.New(log.WithStdout()).With(log.WithLevel("level", level.Error))
|
|
logger.Err(ctx, "same error message")
|
|
// Output: msg="same error message" level=error
|
|
}
|
|
|
|
func ExampleNew_level_info() {
|
|
logger := log.New(log.WithStdout()).With(log.WithLevel("level", level.Error))
|
|
logger.Info(ctx, "same message")
|
|
// Output:
|
|
}
|
|
|
|
type Obj struct {
|
|
Name string
|
|
IsEnable bool
|
|
}
|
|
|
|
func ExampleNew_jsonFormat() {
|
|
logger := log.New(log.WithStdout(), log.WithJSONFormat()).
|
|
With(
|
|
log.WithLevel(log.KeyLevel, level.Debug),
|
|
log.GoVersion("go-version"),
|
|
)
|
|
logger.Err(ctx, "same error message")
|
|
logger.WarnKVs(ctx, "same warn message", "obj", Obj{Name: "obj name"})
|
|
// Output:
|
|
// {"msg":"same error message","level":"error","go-version":"go1.21.5"}
|
|
// {"msg":"same warn message","obj":{"Name":"obj name","IsEnable":false},"level":"warning","go-version":"go1.21.5"}
|
|
}
|
|
|
|
func ExampleNew_textEncoding() {
|
|
logger := log.With(
|
|
log.New(log.WithStdout()),
|
|
log.WithLevel(log.KeyLevel, level.Debug),
|
|
log.GoVersion("go-version"),
|
|
)
|
|
logger.Err(ctx, "same error message")
|
|
logger.InfoKVs(ctx, "same info message", "api-version", 0.1, "obj", Obj{Name: "text value", IsEnable: true})
|
|
|
|
// Output:
|
|
// msg="same error message" level=error go-version=go1.21.5
|
|
// msg="same info message" api-version=0.1 obj={Name:text value IsEnable:true} level=info go-version=go1.21.5
|
|
}
|
|
|
|
type ctxKey string
|
|
|
|
func (c ctxKey) String() string {
|
|
return string(c)
|
|
}
|
|
|
|
func levelInfo(ctx context.Context, entry *entry.Entry, handler log.Logger) (int, error) {
|
|
return handler(ctx, entry.Add(field.String("level", entry.Level().String())))
|
|
}
|
|
|
|
func ExampleWith() {
|
|
var requestID ctxKey = "requestID"
|
|
vctx := context.WithValue(ctx, requestID, "6a5fa048-7181-11ea-bc55-0242ac130003")
|
|
|
|
logger := log.With(
|
|
log.New(log.WithStdout()),
|
|
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.21.5
|
|
}
|
|
|
|
func ExampleLogger_Print() {
|
|
logger := log.With(
|
|
log.New(log.WithStdout()),
|
|
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.21.5
|
|
}
|
|
|
|
func ExamplePrint() {
|
|
setStdout()
|
|
log.Print("same message")
|
|
// Output: msg="same message" level=info
|
|
}
|
|
|
|
func Example_fieldClosureFn() {
|
|
cnt := int32(0)
|
|
closure := field.ClosureFn(func() any {
|
|
d := fmt.Sprintf("additional error data: %d", cnt)
|
|
atomic.AddInt32(&cnt, 1)
|
|
|
|
return d
|
|
})
|
|
|
|
log := log.With(log.New(log.WithStdout()), log.WithLevel("level", level.Info))
|
|
|
|
log.DebugKVs(ctx, "debug message", "data", closure)
|
|
log.ErrKVs(ctx, "error message", "err", closure)
|
|
log.WarnKVs(ctx, "warn message", "warn", closure)
|
|
|
|
// Output:
|
|
// msg="error message" err="additional error data: 0" level=error
|
|
// msg="warn message" warn="additional error data: 1" level=warning
|
|
}
|
|
|
|
func Example_withGroup() {
|
|
log := log.With(log.New(log.WithStdout()), log.WithLevel(log.KeyLevel, level.Info))
|
|
|
|
log.ErrKVs(ctx, "error message",
|
|
field.Groups("grous_field",
|
|
field.Error("err", os.ErrDeadlineExceeded),
|
|
field.Bool("bool", false),
|
|
),
|
|
)
|
|
log.WarnKV(ctx, "error message", field.ValuerFn("valuer_field", func() any {
|
|
return field.Fields{
|
|
field.Int("int_value", math.MaxInt),
|
|
field.Uint8("uint8_value", math.MaxUint8),
|
|
}
|
|
}))
|
|
|
|
// Output:
|
|
// msg="error message" grous_field.err="i/o timeout" grous_field.bool=false level=error
|
|
// msg="error message" valuer_field.int_value=9223372036854775807 valuer_field.uint8_value=255 level=warning
|
|
}
|
|
|