feat: export util func

master v0.0.7
Evan Chen 2021-11-08 11:58:05 +08:00
parent 2bbc6ae9ff
commit 20a2c6e2a7
3 changed files with 8 additions and 8 deletions

4
log.go
View File

@ -107,13 +107,13 @@ func (l *Logger) output(lev Llevel, depth int, fields H, v ...interface{}) {
Message: msg, Message: msg,
Level: lev, Level: lev,
Time: time.Now(), Time: time.Now(),
Caller: caller(depth), Caller: Caller(depth),
Color: l.color, Color: l.color,
} }
switch lev { switch lev {
case Lerror: case Lerror:
if !PROD { if !PROD {
data.Stack = stack() data.Stack = Stack()
} }
// disable stack trace on debug // disable stack trace on debug
// case Ldebug: // case Ldebug:

View File

@ -210,7 +210,7 @@ func BenchmarkStackSTDStr(b *testing.B) {
l.SetErrOutput(io.Discard) l.SetErrOutput(io.Discard)
l.SetOutput(io.Discard) l.SetOutput(io.Discard)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
l.Info(stack()) l.Info(Stack())
} }
} }
@ -220,7 +220,7 @@ func BenchmarkStackCust(b *testing.B) {
l.SetErrOutput(io.Discard) l.SetErrOutput(io.Discard)
l.SetOutput(io.Discard) l.SetOutput(io.Discard)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
l.Info(Stack(3)) l.Info(StackSkip(3))
} }
} }
@ -230,6 +230,6 @@ func BenchmarkStackCustStr(b *testing.B) {
l.SetErrOutput(io.Discard) l.SetErrOutput(io.Discard)
l.SetOutput(io.Discard) l.SetOutput(io.Discard)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
l.Info(string(Stack(3))) l.Info(string(StackSkip(3)))
} }
} }

View File

@ -7,7 +7,7 @@ import (
"runtime" "runtime"
) )
func caller(depth int) string { func Caller(depth int) string {
_, file, line, _ := runtime.Caller(depth) _, file, line, _ := runtime.Caller(depth)
short := file short := file
for i := len(file) - 1; i > 0; i-- { for i := len(file) - 1; i > 0; i-- {
@ -19,7 +19,7 @@ func caller(depth int) string {
return fmt.Sprintf("%s:%d", short, line) return fmt.Sprintf("%s:%d", short, line)
} }
func stack() string { func Stack() string {
buf := make([]byte, 1024) buf := make([]byte, 1024)
for { for {
n := runtime.Stack(buf, false) n := runtime.Stack(buf, false)
@ -49,7 +49,7 @@ var (
) )
// Stack returns a nicely formatted stack frame, skipping skip frames. // Stack returns a nicely formatted stack frame, skipping skip frames.
func Stack(skip int) []byte { func StackSkip(skip int) []byte {
buf := new(bytes.Buffer) // the returned data buf := new(bytes.Buffer) // the returned data
// As we loop, we open files and read them. These variables record the currently // As we loop, we open files and read them. These variables record the currently
// loaded file. // loaded file.