diff --git a/example/log.go b/example/log.go index aaac0f1..f313ca8 100644 --- a/example/log.go +++ b/example/log.go @@ -16,7 +16,7 @@ func main() { log.Err(ctx, "error message", 42) service(ctx, log.Log()) - logger := log.New(log.WithJSONFormat()).With(log.WithSource(10)) + 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) } diff --git a/global.go b/global.go index 5be042a..7114b45 100644 --- a/global.go +++ b/global.go @@ -10,7 +10,7 @@ import ( //nolint:gochecknoglobals,gomnd var global = With(New(), - WithSource(2), + WithSource(2, TrimPath), WithLevel(KeyLevel, level.Debug), WithExit(level.Alert), WithPanic(level.Emergency), diff --git a/global_example_test.go b/global_example_test.go index 5044998..79ee466 100644 --- a/global_example_test.go +++ b/global_example_test.go @@ -2,6 +2,7 @@ package log_test import ( "context" + "path/filepath" "gitoa.ru/go-4devs/log" "gitoa.ru/go-4devs/log/level" @@ -9,7 +10,7 @@ import ( func ExampleDebug() { logger := log.New(log.WithStdout()).With( - log.WithSource(2), + log.WithSource(2, filepath.Base), log.WithLevel(log.KeyLevel, level.Debug), log.WithExit(level.Alert), log.WithPanic(level.Emergency), @@ -20,5 +21,5 @@ func ExampleDebug() { ctx := context.Background() log.Debug(ctx, "debug message") // Output: - // msg="debug message" source=log/global_example_test.go:21 level=debug + // msg="debug message" source=global_example_test.go:22 level=debug } diff --git a/logger_example_caller_test.go b/logger_example_caller_test.go index 884d30c..b98a2e5 100644 --- a/logger_example_caller_test.go +++ b/logger_example_caller_test.go @@ -1,6 +1,8 @@ package log_test import ( + "path/filepath" + "gitoa.ru/go-4devs/log" "gitoa.ru/go-4devs/log/level" ) @@ -8,14 +10,14 @@ import ( func ExampleNew_withCaller() { logger := log.New(log.WithStdout()).With( log.WithLevel(log.KeyLevel, level.Debug), - log.WithSource(3), + log.WithSource(3, filepath.Base), ) logger.Err(ctx, "same error message") logger.InfoKVs(ctx, "same info message", "api-version", 0.1) _, _ = logger.Write([]byte("same write message")) // Output: - // msg="same error message" level=error source=log/logger_example_caller_test.go:13 - // msg="same info message" api-version=0.1 level=info source=log/logger_example_caller_test.go:14 - // msg="same write message" level=info source=log/logger_example_caller_test.go:15 + // 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:16 + // msg="same write message" level=info source=logger_example_caller_test.go:17 } diff --git a/source.go b/source.go index caa8c8e..b033765 100644 --- a/source.go +++ b/source.go @@ -11,7 +11,7 @@ import ( "gitoa.ru/go-4devs/log/field" ) -func WithSource(items int) Middleware { +func WithSource(items int, trimPath func(string) string) Middleware { const ( skip = 4 funcPrefix = "gitoa.ru/go-4devs/log.Logger" @@ -59,7 +59,7 @@ func WithSource(items int) Middleware { } } -func trimPath(file string) string { +func TrimPath(file string) string { idx := strings.LastIndexByte(file, '/') if idx == -1 { return filepath.Base(file) diff --git a/source_example_test.go b/source_example_test.go index c780f3a..5af93fa 100644 --- a/source_example_test.go +++ b/source_example_test.go @@ -2,24 +2,25 @@ package log_test import ( "context" + "path/filepath" "gitoa.ru/go-4devs/log" ) func ExampleWithSource() { ctx := context.Background() - logger := log.New(log.WithStdout()).With(log.WithSource(1)) + logger := log.New(log.WithStdout()).With(log.WithSource(1, filepath.Base)) logger.Debug(ctx, "debug message") // Output: - // msg="debug message" source=log/source_example_test.go:13 + // msg="debug message" source=source_example_test.go:14 } func ExampleWithSource_json() { ctx := context.Background() - logger := log.New(log.WithStdout(), log.WithJSONFormat()).With(log.WithSource(2)) + logger := log.New(log.WithStdout(), log.WithJSONFormat()).With(log.WithSource(2, filepath.Base)) logger.Debug(ctx, "debug message") // Output: - // {"msg":"debug message","source":{"file":"log/source_example_test.go","line":22,"func":"log_test.ExampleWithSource_json"}} + // {"msg":"debug message","source":{"file":"source_example_test.go","line":23,"func":"log_test.ExampleWithSource_json"}} }