-
Notifications
You must be signed in to change notification settings - Fork 153
/
logger.go
102 lines (82 loc) · 2.13 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package internal
import (
"fmt"
"io"
"log"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/weaveworks/weave-gitops/pkg/logger"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type CLILogger struct {
stdout io.Writer
}
func NewCLILogger(writer io.Writer) logger.Logger {
return CLILogger{
stdout: writer,
}
}
func Logr() logr.Logger {
l, err := zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.InfoLevel),
Development: false,
DisableCaller: true,
DisableStacktrace: true,
Encoding: "console",
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "msg",
},
OutputPaths: []string{"stdout"},
}.Build()
if err != nil {
log.Fatalf("Couldn't set up logger: %v", err)
}
return zapr.NewLogger(l)
}
func (l CLILogger) Println(format string, a ...interface{}) {
fmt.Fprintln(l.stdout, fmt.Sprintf(format, a...))
}
func (l CLILogger) Printf(format string, a ...interface{}) {
fmt.Fprintf(l.stdout, format, a...)
}
func (l CLILogger) Infow(msg string, kv ...interface{}) {
var s, k string
for _, v := range kv {
if k == "" {
k = fmt.Sprintf("%s", v)
continue
}
s += fmt.Sprintf(" %s=%v ", k, v)
k = ""
}
fmt.Fprintf(l.stdout, msg+" - %s\n", s)
}
func (l CLILogger) Actionf(format string, a ...interface{}) {
fmt.Fprintln(l.stdout, `►`, fmt.Sprintf(format, a...))
}
func (l CLILogger) Generatef(format string, a ...interface{}) {
fmt.Fprintln(l.stdout, `✚`, fmt.Sprintf(format, a...))
}
func (l CLILogger) Waitingf(format string, a ...interface{}) {
fmt.Fprintln(l.stdout, `◎`, fmt.Sprintf(format, a...))
}
func (l CLILogger) Successf(format string, a ...interface{}) {
fmt.Fprintln(l.stdout, `✔`, fmt.Sprintf(format, a...))
}
func (l CLILogger) Warningf(format string, a ...interface{}) {
fmt.Fprintln(l.stdout, `⚠️`, fmt.Sprintf(format, a...))
}
func (l CLILogger) Failuref(format string, a ...interface{}) {
fmt.Fprintln(l.stdout, `✗`, fmt.Sprintf(format, a...))
}
func (l CLILogger) Write(p []byte) (n int, err error) {
n, err = l.stdout.Write(p)
if err != nil {
return n, err
}
if n != len(p) {
return n, io.ErrShortWrite
}
return len(p), nil
}