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.

142 lines
3.7 KiB

3 years ago
package log_test
import (
"context"
"fmt"
"os"
3 years ago
"sync/atomic"
3 years ago
"gitoa.ru/go-4devs/log"
3 years ago
"gitoa.ru/go-4devs/log/entry"
"gitoa.ru/go-4devs/log/field"
"gitoa.ru/go-4devs/log/level"
3 years ago
)
//nolint:gochecknoglobals
var ctx = context.Background()
3 years ago
func setStdout() {
// set stout for example by default stderror
log.SetLogger(log.New(log.WithStdout()).With(log.WithLevel("level", level.Debug)))
}
3 years ago
func ExampleNew() {
logger := log.New(log.WithStdout())
logger.Info(ctx, "same message")
// Output: msg="same message"
}
func ExampleInfo() {
3 years ago
setStdout()
3 years ago
log.Info(ctx, "same message")
// Output: msg="same message" level=info
}
func ExampleErrKV() {
3 years ago
setStdout()
log.ErrKVs(ctx, "same message", "key", "addition value")
3 years ago
// 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() {
3 years ago
logger := log.New(log.WithStdout()).With(log.WithLevel("level", level.Debug))
logger.DebugKVs(ctx, "same message", "error", os.ErrNotExist)
3 years ago
// Output: msg="same message" error=file does not exist level=debug
}
func ExampleNew_level() {
3 years ago
logger := log.New(log.WithStdout()).With(log.WithLevel("level", level.Error))
3 years ago
logger.Info(ctx, "same message")
// Output:
logger.Err(ctx, "same error message")
// Output: msg="same error message" level=error
}
func ExampleNew_jsonFormat() {
logger := log.New(log.WithStdout(), log.WithJSONFormat()).
With(
3 years ago
log.WithLevel("level", level.Debug),
3 years ago
log.GoVersion("go-version"),
)
logger.Err(ctx, "same error message")
3 years ago
// Output: {"go-version":"go1.15.2","level":"error","msg":"same error message"}
3 years ago
}
3 years ago
func ExampleNew_textEncoding() {
3 years ago
logger := log.With(
3 years ago
log.New(log.WithStdout()),
log.WithLevel("level", level.Debug),
3 years ago
log.GoVersion("go-version"),
)
logger.Err(ctx, "same error message")
3 years ago
logger.InfoKVs(ctx, "same info message", "api-version", 0.1)
3 years ago
// Output:
3 years ago
// msg="same error message" level=error go-version=go1.15.2
// msg="same info message" api-version=0.1 level=info go-version=go1.15.2
3 years ago
}
type ctxKey string
func (c ctxKey) String() string {
return string(c)
}
3 years ago
func levelInfo(ctx context.Context, entry *entry.Entry, handler log.Logger) (int, error) {
return handler(ctx, entry.Add(field.String("level", entry.Level().String())))
3 years ago
}
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")
3 years ago
// Output: msg="same message" level=info requestID=6a5fa048-7181-11ea-bc55-0242ac130003 api=0.1.0 go=go1.15.2
3 years ago
}
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")
3 years ago
// Output: msg="same message" level=info client=http api=0.1.0 go=go1.15.2
3 years ago
}
func ExamplePrint() {
3 years ago
setStdout()
3 years ago
log.Print("same message")
// Output: msg="same message" level=info
}
3 years ago
func ExampleWithClosure() {
cnt := int32(0)
closure := func() string {
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.WithClosure)
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
}