Browse Source

add trim path

pull/14/head
andrey 12 months ago
parent
commit
bfc7be67e2
  1. 2
      example/log.go
  2. 2
      global.go
  3. 5
      global_example_test.go
  4. 10
      logger_example_caller_test.go
  5. 4
      source.go
  6. 9
      source_example_test.go

2
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)
}

2
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),

5
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
}

10
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
}

4
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)

9
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"}}
}

Loading…
Cancel
Save