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.
36 lines
1.1 KiB
36 lines
1.1 KiB
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))))
|
|
}
|
|
}
|
|
|
|
func ExampleFormatWithBracket() {
|
|
ctx := context.Background()
|
|
logger := log.New(log.WithFormat(log.FormatWithBracket()), log.WithStdout()).With(
|
|
log.WithSource(10, filepath.Base),
|
|
exampleWithTime(log.KeyTime, time.RFC3339), //log.WithTime(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-19T06:14:07+03:00 [info] writer_example_test.go:29 "imfo message" num=42
|
|
// 2038-01-19T06:14:07+03:00 [error][service_name] writer_example_test.go:32 "error message"
|
|
}
|
|
|