-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkoan.go
64 lines (54 loc) · 1.55 KB
/
koan.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
// Package koan provides utility logging functions designed
// to standardise and colorize the logged output
package koan
import (
"fmt"
color "github.com/TwiN/go-color"
"log"
)
// Logger will hold configuration specific to the logger package
type Logger struct {
DEBUG bool
}
const (
INFOPREFIX = "INFO"
WARNPREFIX = "WARN"
ERRPREFIX = "ERROR"
FATALERRPREFIX = "FATAL ERROR"
DEBUGPREFIX = "DEBUG"
)
var (
INFOCOLOR = color.Gray
WARNCOLOR = color.Yellow
ERRCOLOR = color.Red
DEBUGCOLOR = color.Green
)
// Info provides logging with an Info prefix
func (*Logger) Info(msg string) {
log.Println(write(INFOPREFIX, msg, INFOCOLOR))
}
// Warn provides logging with a Warning prefix
func (*Logger) Warn(msg string) {
log.Println(write(WARNPREFIX, msg, WARNCOLOR))
}
// Debug provides additional logging if DEBUG is set on/true
func (l *Logger) Debug(msg string) {
if l.DEBUG {
log.Println(write(DEBUGPREFIX, msg, DEBUGCOLOR))
}
}
// Error provides logging with an Error prefix
func (*Logger) Error(msg string, err error) {
errMsg := fmt.Sprintf("%s (%v)", msg, err)
log.Println(write(ERRPREFIX, errMsg, ERRCOLOR))
}
// FatalError provides logging with a FatalError prefix and terminates execution
func (*Logger) FatalError(msg string, err error) {
errMsg := fmt.Sprintf("%s (%v)", msg, err)
log.Fatalln(write(FATALERRPREFIX, errMsg, ERRCOLOR))
}
// utility to construct colorized log output
func write(prefix string, output, outColor string) string {
out := fmt.Sprintf("%s: %s", prefix, output)
return color.Colorize(outColor, out)
}