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,
Level: lev,
Time: time.Now(),
Caller: caller(depth),
Caller: Caller(depth),
Color: l.color,
}
switch lev {
case Lerror:
if !PROD {
data.Stack = stack()
data.Stack = Stack()
}
// disable stack trace on debug
// case Ldebug:

View File

@ -210,7 +210,7 @@ func BenchmarkStackSTDStr(b *testing.B) {
l.SetErrOutput(io.Discard)
l.SetOutput(io.Discard)
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.SetOutput(io.Discard)
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.SetOutput(io.Discard)
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"
)
func caller(depth int) string {
func Caller(depth int) string {
_, file, line, _ := runtime.Caller(depth)
short := file
for i := len(file) - 1; i > 0; i-- {
@ -19,7 +19,7 @@ func caller(depth int) string {
return fmt.Sprintf("%s:%d", short, line)
}
func stack() string {
func Stack() string {
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, false)
@ -49,7 +49,7 @@ var (
)
// 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
// As we loop, we open files and read them. These variables record the currently
// loaded file.