You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.7 KiB

  1. package log
  2. import (
  3. "fmt"
  4. "github.com/pkg/errors"
  5. )
  6. // NewTracingLogger enables tracing by wrapping all errors (if they
  7. // implement stackTracer interface) in tracedError.
  8. //
  9. // All errors returned by https://github.com/pkg/errors implement stackTracer
  10. // interface.
  11. //
  12. // For debugging purposes only as it doubles the amount of allocations.
  13. func NewTracingLogger(next Logger) Logger {
  14. return &tracingLogger{
  15. next: next,
  16. }
  17. }
  18. type stackTracer interface {
  19. error
  20. StackTrace() errors.StackTrace
  21. }
  22. type tracingLogger struct {
  23. next Logger
  24. }
  25. func (l *tracingLogger) Info(msg string, keyvals ...interface{}) error {
  26. return l.next.Info(msg, formatErrors(keyvals)...)
  27. }
  28. func (l *tracingLogger) Debug(msg string, keyvals ...interface{}) error {
  29. return l.next.Debug(msg, formatErrors(keyvals)...)
  30. }
  31. func (l *tracingLogger) Error(msg string, keyvals ...interface{}) error {
  32. return l.next.Error(msg, formatErrors(keyvals)...)
  33. }
  34. func (l *tracingLogger) With(keyvals ...interface{}) Logger {
  35. return &tracingLogger{next: l.next.With(formatErrors(keyvals)...)}
  36. }
  37. func formatErrors(keyvals []interface{}) []interface{} {
  38. newKeyvals := make([]interface{}, len(keyvals))
  39. copy(newKeyvals, keyvals)
  40. for i := 0; i < len(newKeyvals)-1; i += 2 {
  41. if err, ok := newKeyvals[i+1].(stackTracer); ok {
  42. newKeyvals[i+1] = tracedError{err}
  43. }
  44. }
  45. return newKeyvals
  46. }
  47. // tracedError wraps a stackTracer and just makes the Error() result
  48. // always return a full stack trace.
  49. type tracedError struct {
  50. wrapped stackTracer
  51. }
  52. var _ stackTracer = tracedError{}
  53. func (t tracedError) StackTrace() errors.StackTrace {
  54. return t.wrapped.StackTrace()
  55. }
  56. func (t tracedError) Cause() error {
  57. return t.wrapped
  58. }
  59. func (t tracedError) Error() string {
  60. return fmt.Sprintf("%+v", t.wrapped)
  61. }