forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
82 lines (66 loc) · 2.02 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
// Package logging defines the minimal interface that loggers must support to be used by pack.
package logging
import (
"fmt"
"io"
"github.com/buildpack/pack/internal/style"
)
// Logger defines behavior required by a logging package used by pack libraries
type Logger interface {
Debug(msg string)
Debugf(fmt string, v ...interface{})
Info(msg string)
Infof(fmt string, v ...interface{})
Warn(msg string)
Warnf(fmt string, v ...interface{})
Error(msg string)
Errorf(fmt string, v ...interface{})
Writer() io.Writer
IsVerbose() bool
}
// WithInfoErrorWriter is an optional interface for loggers that want to support a separate writer for errors and standard logging.
// the DebugInfoWriter should write to stderr if quiet is false.
type WithInfoErrorWriter interface {
InfoErrorWriter() io.Writer
}
// WithInfoWriter is an optional interface what will return a writer that will write raw output if quiet is false.
type WithInfoWriter interface {
InfoWriter() io.Writer
}
// GetInfoErrorWriter will return an ErrorWriter, typically stderr if one exists, otherwise the standard logger writer
// will be returned.
func GetInfoErrorWriter(l Logger) io.Writer {
if er, ok := l.(WithInfoErrorWriter); ok {
return er.InfoErrorWriter()
}
return l.Writer()
}
// GetInfoWriter returns a writer
// See WithInfoWriter
func GetInfoWriter(l Logger) io.Writer {
if ew, ok := l.(WithInfoWriter); ok {
return ew.InfoWriter()
}
return l.Writer()
}
// PrefixWriter will prefix writes
type PrefixWriter struct {
out io.Writer
prefix string
}
// NewPrefixWriter writes by w will be prefixed
func NewPrefixWriter(w io.Writer, prefix string) *PrefixWriter {
return &PrefixWriter{
out: w,
prefix: fmt.Sprintf("[%s] ", style.Prefix(prefix)),
}
}
// Writes bytes to the embedded log function
func (w *PrefixWriter) Write(buf []byte) (int, error) {
_, _ = fmt.Fprint(w.out, w.prefix+string(buf))
return len(buf), nil
}
// Tip logs a tip.
func Tip(l Logger, format string, v ...interface{}) {
l.Infof(style.Tip("Tip: ")+format, v...)
}