-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
errlog.go
105 lines (98 loc) · 2.64 KB
/
errlog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Package errlog provides a simple object to enhance Go source code debugging
//
// Example result:
//
//
// $ go run myfailingapp.go
// Program starting
// error in main.main: something failed here
// line 13 of /Users/snwfdhmp/go/src/github.com/snwfdhmp/sandbox/testerr.go
// 9: func main() {
// 10: fmt.Println("Program starting")
// 11: err := errors.New("something failed here")
// 12:
// 13: errlog.Debug(err)
// 14:
// 15: fmt.Println("End of the program")
// 16: }
// exit status 1
//
//
// You can configure your own logger with these options :
//
//
// type Config struct {
// LinesBefore int
// LinesAfter int
// PrintStack bool
// PrintSource bool
// PrintError bool
// ExitOnDebugSuccess bool
// }
//
//
// Example :
//
//
// debug := errlog.NewLogger(&errlog.Config{
// LinesBefore: 2,
// LinesAfter: 1,
// PrintError: true,
// PrintSource: true,
// PrintStack: false,
// ExitOnDebugSuccess: true,
// })
//
// // ...
// if err != nil {
// debug.Debug(err)
// return
// }
//
// Outputs :
//
// Error in main.someBigFunction(): I'm failing for no reason
// line 41 of /Users/snwfdhmp/go/src/github.com/snwfdhmp/sandbox/testerr.go:41
// 33: func someBigFunction() {
// ...
// 40: if err := someNastyFunction(); err != nil {
// 41: debug.Debug(err)
// 42: return
// 43: }
// exit status 1
//
package errlog
import (
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
var (
debugMode = false
fs = afero.NewOsFs() //fs is at package level because I think it needn't be scoped to loggers
)
//SetDebugMode sets debug mode to On if toggle==true or Off if toggle==false. It changes log level an so displays more logs about whats happening. Useful for debugging.
func SetDebugMode(toggle bool) {
if toggle {
logrus.SetLevel(logrus.DebugLevel)
} else {
logrus.SetLevel(logrus.InfoLevel)
}
debugMode = toggle
}
//Debug is a shortcut for DefaultLogger.Debug.
func Debug(uErr error) bool {
DefaultLogger.Overload(1) // Prevents from adding this func to the stack trace
return DefaultLogger.Debug(uErr)
}
//PrintStack pretty prints the current stack trace
func PrintStack() {
DefaultLogger.printStack(parseStackTrace(1))
}
//PrintRawStack prints the current stack trace unparsed
func PrintRawStack() {
DefaultLogger.Printf("%#v", parseStackTrace(1))
}
//PrintStackMinus prints the current stack trace minus the amount of depth in parameter
func PrintStackMinus(depthToRemove int) {
DefaultLogger.printStack(parseStackTrace(1 + depthToRemove))
}