-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
58 lines (47 loc) · 1.49 KB
/
main.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
package golog
import (
"fmt"
"log"
"strings"
"github.com/pkg/errors"
)
type stackTracer interface {
StackTrace() errors.StackTrace
}
// LogMessage() - Takes a log level, which is then compared to the configured log level. If the level provided
// is lower than or equal to the configured log level, the message is logged.
func (g GoLogger) LogMessage(messageLogLevel int, message ...interface{}) {
if messageLogLevel <= g.LogLevel {
fmt.Println(message...)
}
}
// CreateError() - Returns an error with a stack trace
func (e ErrorLogger) CreateError(message ...interface{}) error {
return errors.New(fmt.Sprint(message...))
}
// LogError() - Log error, regardless of logging level. Include stack trace. Provide the error type - ERROR or FATAL.
// ERROR will allow app to continue, FATAL will cause app to shut down.
func (e ErrorLogger) LogError(errorType string, err error) {
var finalError string = err.Error() + "\n"
if err, ok := err.(stackTracer); ok {
for _, f := range err.StackTrace() {
str := fmt.Sprintf("%+s", f)
// Don't include golog or runtime frame in the stack trace
if strings.Contains(str, "golog") || strings.Contains(str, "runtime") {
continue
}
// Add new line if error string doesn't end in one
if !strings.HasSuffix(str, "\n") {
str += "\n"
}
finalError += str
}
}
if errorType == ERROR {
log.Print(finalError)
} else if errorType == FATAL {
log.Fatal(finalError)
} else {
log.Printf("%s%s\n", "Invalid errorType:", errorType)
}
}