-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_logger.go
59 lines (48 loc) · 1.78 KB
/
error_logger.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
package BetterErrors
import (
"fmt"
"runtime"
"github.com/logrusorgru/aurora"
)
type ErrorLogger struct {
IncludeSensitiveInfo bool
ExitMessage string
}
func NewErrorLogger() *ErrorLogger {
return &ErrorLogger{
IncludeSensitiveInfo: true,
ExitMessage: "↑ Something went wrong! ↑",
}
}
func (logger *ErrorLogger) SetIncludeSensitiveInfo(includeSensitiveInfo bool) {
logger.IncludeSensitiveInfo = includeSensitiveInfo
}
func (logger *ErrorLogger) SetExitMessage(exitMessage string) {
logger.ExitMessage = exitMessage
}
func (logger *ErrorLogger) LogError(err error) {
if err != nil {
pc, filename, line, _ := runtime.Caller(1)
funcName := runtime.FuncForPC(pc).Name()
sourceInfo := fmt.Sprintf("[%s:%d]", filename, line)
os := runtime.GOOS
goVersion := runtime.Version()
if logger.IncludeSensitiveInfo {
fmt.Printf("╭─ [%s] in %s %s %s\n", aurora.Red(aurora.Bold("error")), aurora.Blue(funcName), aurora.Green(sourceInfo), aurora.Cyan("↙"))
} else {
fmt.Printf("╭─ %s Error Information [%s] %s\n", aurora.Magenta("─"), aurora.Blue(funcName), aurora.Magenta("─"))
}
fmt.Printf("│ %v\n", err)
fmt.Printf("│\n")
fmt.Printf("│ %s Something went wrong! \n", aurora.Cyan("↑"))
fmt.Printf("│ %s Please check the error message above for details.\n", aurora.Cyan("→"))
if logger.IncludeSensitiveInfo {
fmt.Printf("│ %s User OS: %s\n", aurora.Cyan("→"), os)
fmt.Printf("│ %s Go Version: %s\n", aurora.Cyan("→"), goVersion)
} else {
fmt.Printf("│ %s User OS and Go Version information omitted for privacy.\n", aurora.Cyan("→"))
}
fmt.Println("╰───────────────────────────────────")
//log.Fatalf(logger.ExitMessage)
}
}