Browse Source

add trim path

pull/14/head
andrey 11 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) log.Err(ctx, "error message", 42)
service(ctx, log.Log()) 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")) logger.AlertKV(ctx, "alert message new logger", field.String("string", "value"))
service(ctx, logger) service(ctx, logger)
} }

2
global.go

@ -10,7 +10,7 @@ import (
//nolint:gochecknoglobals,gomnd //nolint:gochecknoglobals,gomnd
var global = With(New(), var global = With(New(),
WithSource(2), WithSource(2, TrimPath),
WithLevel(KeyLevel, level.Debug), WithLevel(KeyLevel, level.Debug),
WithExit(level.Alert), WithExit(level.Alert),
WithPanic(level.Emergency), WithPanic(level.Emergency),

5
global_example_test.go

@ -2,6 +2,7 @@ package log_test
import ( import (
"context" "context"
"path/filepath"
"gitoa.ru/go-4devs/log" "gitoa.ru/go-4devs/log"
"gitoa.ru/go-4devs/log/level" "gitoa.ru/go-4devs/log/level"
@ -9,7 +10,7 @@ import (
func ExampleDebug() { func ExampleDebug() {
logger := log.New(log.WithStdout()).With( logger := log.New(log.WithStdout()).With(
log.WithSource(2), log.WithSource(2, filepath.Base),
log.WithLevel(log.KeyLevel, level.Debug), log.WithLevel(log.KeyLevel, level.Debug),
log.WithExit(level.Alert), log.WithExit(level.Alert),
log.WithPanic(level.Emergency), log.WithPanic(level.Emergency),
@ -20,5 +21,5 @@ func ExampleDebug() {
ctx := context.Background() ctx := context.Background()
log.Debug(ctx, "debug message") log.Debug(ctx, "debug message")
// Output: // 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 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"
) )
@ -8,14 +10,14 @@ import (
func ExampleNew_withCaller() { func ExampleNew_withCaller() {
logger := log.New(log.WithStdout()).With( logger := log.New(log.WithStdout()).With(
log.WithLevel(log.KeyLevel, level.Debug), log.WithLevel(log.KeyLevel, level.Debug),
log.WithSource(3), log.WithSource(3, filepath.Base),
) )
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=log/logger_example_caller_test.go:13 // msg="same error message" level=error source=logger_example_caller_test.go:15
// msg="same info message" api-version=0.1 level=info source=log/logger_example_caller_test.go:14 // msg="same info message" api-version=0.1 level=info source=logger_example_caller_test.go:16
// msg="same write message" level=info source=log/logger_example_caller_test.go:15 // 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" "gitoa.ru/go-4devs/log/field"
) )
func WithSource(items int) Middleware { func WithSource(items int, trimPath func(string) string) Middleware {
const ( const (
skip = 4 skip = 4
funcPrefix = "gitoa.ru/go-4devs/log.Logger" 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, '/') idx := strings.LastIndexByte(file, '/')
if idx == -1 { if idx == -1 {
return filepath.Base(file) return filepath.Base(file)

9
source_example_test.go

@ -2,24 +2,25 @@ package log_test
import ( import (
"context" "context"
"path/filepath"
"gitoa.ru/go-4devs/log" "gitoa.ru/go-4devs/log"
) )
func ExampleWithSource() { func ExampleWithSource() {
ctx := context.Background() 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") logger.Debug(ctx, "debug message")
// Output: // Output:
// msg="debug message" source=log/source_example_test.go:13 // msg="debug message" source=source_example_test.go:14
} }
func ExampleWithSource_json() { func ExampleWithSource_json() {
ctx := context.Background() 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") logger.Debug(ctx, "debug message")
// Output: // 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