-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
53 lines (47 loc) · 1.19 KB
/
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
package scv
import (
"fmt"
"io"
)
type Logger struct {
Verbose bool
OutStream io.Writer
ErrStream io.Writer
}
func (logger *Logger) Debug(a string) (n int, err error) {
if logger.Verbose {
format, _ := logger.Format("lightGray", "debug")
n, err = fmt.Fprintf(logger.OutStream, format, a)
return n, err
}
return n, err
}
func (logger *Logger) Info(a string) (n int, err error) {
n, err = fmt.Fprintln(logger.OutStream, a)
return n, err
}
func (logger *Logger) Fatal(a interface{}) (n int, err error) {
format, _ := logger.Format("red", "fatal")
n, err = fmt.Fprintf(logger.ErrStream, format, a)
return n, err
}
func (logger *Logger) Format(colorName string, level string) (format string, err error) {
ansiColors := map[string]string{
"normal": "\033[m",
"black": "\033[30m",
"red": "\033[31m",
"green": "\033[32m",
"yellow": "\033[33m",
"blue": "\033[34m",
"magenta": "\033[35m",
"cyan": "\033[36m",
"lightGray": "\033[37m",
}
ansiColor, hasKey := ansiColors[colorName]
if !hasKey {
err = fmt.Errorf("Unsupported colorName")
return format, err
}
format = ansiColor + level + ": %s" + ansiColors["normal"] + "\n"
return format, err
}