12 changed files with 170 additions and 47 deletions
@ -0,0 +1,33 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
|
||||
|
"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)) |
||||
|
logger.AlertKV(ctx, "alert message new logger", field.String("string", "value")) |
||||
|
service(ctx, logger) |
||||
|
} |
||||
|
|
||||
|
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") |
||||
|
} |
@ -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
|
||||
} |
} |
||||
|
Loading…
Reference in new issue