-
Notifications
You must be signed in to change notification settings - Fork 31
/
logger.go
65 lines (52 loc) · 1.52 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
54
55
56
57
58
59
60
61
62
63
64
65
package libbuildpack
import (
"fmt"
"io"
"os"
"strings"
)
type Logger struct {
w io.Writer
}
const (
msgPrefix = " "
redPrefix = "\033[31;1m"
bluePrefix = "\033[34;1m"
colorSuffix = "\033[0m"
msgError = msgPrefix + redPrefix + "**ERROR**" + colorSuffix
msgWarning = msgPrefix + redPrefix + "**WARNING**" + colorSuffix
msgProtip = msgPrefix + bluePrefix + "PRO TIP:" + colorSuffix
msgDebug = msgPrefix + bluePrefix + "DEBUG:" + colorSuffix
)
func NewLogger(w io.Writer) *Logger {
return &Logger{w: w}
}
func (l *Logger) Info(format string, args ...interface{}) {
l.printWithHeader(" ", format, args...)
}
func (l *Logger) Warning(format string, args ...interface{}) {
l.printWithHeader(msgWarning, format, args...)
}
func (l *Logger) Error(format string, args ...interface{}) {
l.printWithHeader(msgError, format, args...)
}
func (l *Logger) Debug(format string, args ...interface{}) {
if os.Getenv("BP_DEBUG") != "" {
l.printWithHeader(msgDebug, format, args...)
}
}
func (l *Logger) BeginStep(format string, args ...interface{}) {
l.printWithHeader("----->", format, args...)
}
func (l *Logger) Protip(tip string, helpURL string) {
l.printWithHeader(msgProtip, "%s", tip)
l.printWithHeader(msgPrefix+"Visit", "%s", helpURL)
}
func (l *Logger) printWithHeader(header string, format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
msg = strings.Replace(msg, "\n", "\n ", -1)
fmt.Fprintf(l.w, "%s %s\n", header, msg)
}
func (l *Logger) Output() io.Writer {
return l.w
}