-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
log.go
222 lines (189 loc) · 5.25 KB
/
log.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package common
import (
"fmt"
"os"
"strconv"
"strings"
"sync"
"github.com/hashicorp/go-multierror"
)
// ErrWithExitCode wraps error and exposes an ExitCode method
type ErrWithExitCode interface {
ExitCode() int
}
type writer struct {
mu *sync.Mutex
source string
}
// Write prints the data to either stdout or stderr using the log helper functions
func (w *writer) Write(bytes []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
if w.source == "stdout" {
for _, line := range strings.Split(string(bytes), "\n") {
if line == "" {
continue
}
LogVerboseQuiet(line)
}
} else {
for _, line := range strings.Split(string(bytes), "\n") {
if line == "" {
continue
}
LogVerboseStderrQuiet(line)
}
}
return len(bytes), nil
}
// LogFail is the failure log formatter
// prints text to stderr and exits with status 1
func LogFail(text string) {
fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", text))
os.Exit(1)
}
// LogFailWithError is the failure log formatter
// prints text to stderr and exits with the specified exit code
func LogFailWithError(err error) {
if err == nil {
return
}
if merr, ok := err.(*multierror.Error); ok {
for _, e := range merr.Errors {
fmt.Fprintf(os.Stderr, " ! %s\n", e.Error())
}
} else {
fmt.Fprintf(os.Stderr, " ! %s\n", err.Error())
}
if errExit, ok := err.(ErrWithExitCode); ok {
os.Exit(errExit.ExitCode())
}
os.Exit(1)
}
// LogFailWithErrorQuiet is the failure log formatter (with quiet option)
// prints text to stderr and exits with the specified exit code
// The error message is not printed if DOKKU_QUIET_OUTPUT has any value
func LogFailWithErrorQuiet(err error) {
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", err.Error()))
}
if errExit, ok := err.(ErrWithExitCode); ok {
os.Exit(errExit.ExitCode())
}
os.Exit(1)
}
// LogFailQuiet is the failure log formatter (with quiet option)
// prints text to stderr and exits with status 1
func LogFailQuiet(text string) {
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", text))
}
os.Exit(1)
}
// Log is the log formatter
func Log(text string) {
fmt.Println(text)
}
// LogQuiet is the log formatter (with quiet option)
func LogQuiet(text string) {
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
fmt.Println(text)
}
}
// LogInfo1 is the info1 header formatter
func LogInfo1(text string) {
fmt.Println(fmt.Sprintf("-----> %s", text))
}
// LogInfo1Quiet is the info1 header formatter (with quiet option)
func LogInfo1Quiet(text string) {
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
LogInfo1(text)
}
}
// LogInfo2 is the info2 header formatter
func LogInfo2(text string) {
fmt.Println(fmt.Sprintf("=====> %s", text))
}
// LogInfo2Quiet is the info2 header formatter (with quiet option)
func LogInfo2Quiet(text string) {
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
LogInfo2(text)
}
}
// LogVerbose is the verbose log formatter
// prints indented text to stdout
func LogVerbose(text string) {
fmt.Println(fmt.Sprintf(" %s", text))
}
// LogVerboseStderr is the verbose log formatter
// prints indented text to stderr
func LogVerboseStderr(text string) {
fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", text))
}
// LogVerboseQuiet is the verbose log formatter
// prints indented text to stdout (with quiet option)
func LogVerboseQuiet(text string) {
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
LogVerbose(text)
}
}
// LogVerboseStderrQuiet is the verbose log formatter
// prints indented text to stderr (with quiet option)
func LogVerboseStderrQuiet(text string) {
if os.Getenv("DOKKU_QUIET_OUTPUT") == "" {
LogVerboseStderr(text)
}
}
// LogVerboseQuietContainerLogs is the verbose log formatter for container logs
func LogVerboseQuietContainerLogs(containerID string) {
LogVerboseQuietContainerLogsTail(containerID, 0, false)
}
// LogVerboseQuietContainerLogsTail is the verbose log formatter for container logs with tail mode enabled
func LogVerboseQuietContainerLogsTail(containerID string, lines int, tail bool) {
args := []string{"container", "logs", containerID}
if lines > 0 {
args = append(args, "--tail", strconv.Itoa(lines))
}
if tail {
args = append(args, "--follow")
}
var mu sync.Mutex
result, err := CallExecCommand(ExecCommandInput{
Command: DockerBin(),
Args: args,
DisableStdioBuffer: true,
StdoutWriter: &writer{
mu: &mu,
source: "stdout",
},
StderrWriter: &writer{
mu: &mu,
source: "stderr",
},
})
if err != nil {
LogExclaim(fmt.Sprintf("Failed to fetch container logs: %s", containerID))
return
}
if !tail && result.ExitCode != 0 {
LogExclaim(fmt.Sprintf("Failed to fetch container logs: %s", containerID))
}
}
// LogWarn is the warning log formatter
func LogWarn(text string) {
fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", text))
}
// LogExclaim is the log exclaim formatter
func LogExclaim(text string) {
fmt.Println(fmt.Sprintf(" ! %s", text))
}
// LogStderr is the stderr log formatter
func LogStderr(text string) {
fmt.Fprintln(os.Stderr, text)
}
// LogDebug is the debug log formatter
func LogDebug(text string) {
if os.Getenv("DOKKU_TRACE") == "1" {
fmt.Fprintln(os.Stderr, fmt.Sprintf(" ? %s", strings.TrimPrefix(text, " ? ")))
}
}