Пример #1
0
func (l *Logger) output(traceskip int, err error, t LogType, s string) error {
	var stack trace.Stack
	if l.isTraceEnabled(t) {
		stack = trace.TraceN(traceskip+1, 32)
	}

	var b bytes.Buffer
	fmt.Fprint(&b, t, " ", s)

	if len(s) == 0 || s[len(s)-1] != '\n' {
		fmt.Fprint(&b, "\n")
	}

	if err != nil {
		fmt.Fprint(&b, "[error]: ", err.Error(), "\n")
		if stack := errors.Stack(err); stack != nil {
			fmt.Fprint(&b, stack.StringWithIndent(1))
		}
	}
	if len(stack) != 0 {
		fmt.Fprint(&b, "[stack]: \n", stack.StringWithIndent(1))
	}

	s = b.String()
	l.mu.Lock()
	defer l.mu.Unlock()
	return l.log.Output(traceskip+2, s)
}
Пример #2
0
func Errorf(format string, v ...interface{}) error {
	err := fmt.Errorf(format, v...)
	if !TraceEnabled {
		return err
	}
	return &TracedError{
		Stack: trace.TraceN(1, 32),
		Cause: err,
	}
}
Пример #3
0
func Trace(err error) error {
	if err == nil || !TraceEnabled {
		return err
	}
	_, ok := err.(*TracedError)
	if ok {
		return err
	}
	return &TracedError{
		Stack: trace.TraceN(1, 32),
		Cause: err,
	}
}