Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
chore: add preset
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Apr 16, 2024
1 parent 66305b1 commit 057d32d
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 72 deletions.
72 changes: 0 additions & 72 deletions valuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,75 +309,3 @@ func FromAny(key string, vf func(context.Context) any) Valuer {
return zap.Any(key, vf(ctx))
}
}

/**************************** helper ******************************************/

func caller(depth int) (file string, line int) {
ok := false
for i := depth; i < depth+10; i++ {
_, file, line, ok = runtime.Caller(i)
if ok && !skipPackage(file, "github.com/things-go/log") {
return file, line
}
}
return file, line
}

func skipPackage(file string, skipPackages ...string) bool {
if strings.HasSuffix(file, "_test.go") {
return false
}
for _, p := range skipPackages {
if strings.Contains(file, p) {
return true
}
}
return false
}

// Caller returns a Valuer that returns a pkg/file:line description of the caller.
func Caller(depth int) Valuer {
return func(context.Context) Field {
file, line := caller(depth)
idx := strings.LastIndexByte(file, '/')
return zap.String("caller", file[idx+1:]+":"+strconv.Itoa(line))
}
}

// File returns a Valuer that returns a pkg/file:line description of the caller.
func File(depth int) Valuer {
return func(context.Context) Field {
file, line := caller(depth)
return zap.String("file", file+":"+strconv.Itoa(line))
}
}

// Package returns a Valuer that returns an immutable Valuer which key is pkg
func Package(v string) Valuer {
return ImmutString("pkg", v)
}

func App(v string) Valuer {
return ImmutString("app", v)
}
func Component(v string) Valuer {
return ImmutString("component", v)
}
func Module(v string) Valuer {
return ImmutString("module", v)
}
func Unit(v string) Valuer {
return ImmutString("unit", v)
}
func Kind(v string) Valuer {
return ImmutString("kind", v)
}
func Type(v string) Valuer {
return ImmutString("type", v)
}
func TraceId(f func(c context.Context) string) Valuer {
return FromString("traceId", f)
}
func RequestId(f func(c context.Context) string) Valuer {
return FromString("requestId", f)
}
80 changes: 80 additions & 0 deletions valuer_preset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package log

import (
"context"
"runtime"
"strconv"
"strings"

"go.uber.org/zap"
)

func caller(depth int) (file string, line int) {
ok := false
for i := depth; i < depth+10; i++ {
_, file, line, ok = runtime.Caller(i)
if ok && !skipPackage(file, "github.com/things-go/log") {
return file, line
}
}
return file, line
}

func skipPackage(file string, skipPackages ...string) bool {
if strings.HasSuffix(file, "_test.go") {
return false
}
for _, p := range skipPackages {
if strings.Contains(file, p) {
return true
}
}
return false
}

// Caller returns a Valuer that returns a pkg/file:line description of the caller.
func Caller(depth int) Valuer {
return func(context.Context) Field {
file, line := caller(depth)
idx := strings.LastIndexByte(file, '/')
return zap.String("caller", file[idx+1:]+":"+strconv.Itoa(line))
}
}

// File returns a Valuer that returns a pkg/file:line description of the caller.
func File(depth int) Valuer {
return func(context.Context) Field {
file, line := caller(depth)
return zap.String("file", file+":"+strconv.Itoa(line))
}
}

// Package returns a Valuer that returns an immutable Valuer which key is pkg
func Package(v string) Valuer {
return ImmutString("pkg", v)
}

func App(v string) Valuer {
return ImmutString("app", v)
}
func Component(v string) Valuer {
return ImmutString("component", v)
}
func Module(v string) Valuer {
return ImmutString("module", v)
}
func Unit(v string) Valuer {
return ImmutString("unit", v)
}
func Kind(v string) Valuer {
return ImmutString("kind", v)
}
func Type(v string) Valuer {
return ImmutString("type", v)
}
func TraceId(f func(c context.Context) string) Valuer {
return FromString("traceId", f)
}
func RequestId(f func(c context.Context) string) Valuer {
return FromString("requestId", f)
}

0 comments on commit 057d32d

Please sign in to comment.