selectable global stack setting

master
Evan Chen 2021-11-08 16:45:19 +08:00
parent 20a2c6e2a7
commit c91e59456a
2 changed files with 17 additions and 12 deletions

25
log.go
View File

@ -23,6 +23,7 @@ const (
var lock sync.Mutex var lock sync.Mutex
var PROD = true var PROD = true
var STACKSKIP = 0
var LEVEL = Lerror | Linfo var LEVEL = Lerror | Linfo
type H map[string]interface{} type H map[string]interface{}
@ -107,19 +108,23 @@ 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),
Color: l.color, Color: l.color,
} }
switch lev { if !PROD {
case Lerror: switch lev {
if !PROD { case Lerror:
data.Stack = Stack() if STACKSKIP == 0 {
data.Stack = Stack()
} else {
data.Stack = StackSkip(STACKSKIP)
}
data.Caller = Caller(depth)
case Ldebug:
data.Caller = Caller(depth)
// disable stack trace on debug
// data.Stack = Stack()
} }
// disable stack trace on debug
// case Ldebug:
// if !PROD {
// data.Stack = stack()
// }
} }
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()

View File

@ -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 StackSkip(skip int) []byte { func StackSkip(skip int) string {
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.
@ -72,7 +72,7 @@ func StackSkip(skip int) []byte {
} }
fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line)) fmt.Fprintf(buf, "\t%s: %s\n", function(pc), source(lines, line))
} }
return buf.Bytes() return string(buf.Bytes())
} }
// function returns, if possible, the name of the function containing the PC. // function returns, if possible, the name of the function containing the PC.