/
log.go
221 lines (191 loc) · 6.29 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
/*
Copyright 2021 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Includes code from github.com/sirupsen/logrus (MIT License)
package log
import (
"context"
"fmt"
"io"
stdlog "log"
ggcrlogs "github.com/google/go-containerregistry/pkg/logs"
"github.com/sirupsen/logrus"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/constants"
)
// Logging levels. Defining our own so we can encapsulate the underlying logger implementation.
const (
// PanicLevel level, highest level of severity. Logs and then calls panic with the
// message passed to Debug, Info, ...
PanicLevel Level = iota
// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
// logging level is set to Panic.
FatalLevel
// ErrorLevel level. Logs. Used for errors that should definitely be noted.
// Commonly used for hooks to send errors to an error tracking service.
ErrorLevel
// WarnLevel level. Non-critical entries that deserve eyes.
WarnLevel
// InfoLevel level. General operational entries about what's going on inside the
// application.
InfoLevel
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
DebugLevel
// TraceLevel level. Designates finer-grained informational events than the Debug.
TraceLevel
)
// Level type for logging levels
type Level uint32
// AllLevels exposes all logging levels
var AllLevels = []Level{
PanicLevel,
FatalLevel,
ErrorLevel,
WarnLevel,
InfoLevel,
DebugLevel,
TraceLevel,
}
// DefaultLogLevel for the global Skaffold logger
const DefaultLogLevel = WarnLevel
type contextKey struct{}
var ContextKey = contextKey{}
// logger is the global logrus.Logger for Skaffold
// TODO: Make this not global.
var logger = New()
type EventContext struct {
Task constants.Phase
Subtask string
}
// String converts the Level to a string. E.g. PanicLevel becomes "panic".
func (level Level) String() string {
switch level {
case TraceLevel:
return "trace"
case DebugLevel:
return "debug"
case InfoLevel:
return "info"
case WarnLevel:
return "warning"
case ErrorLevel:
return "error"
case FatalLevel:
return "fatal"
case PanicLevel:
return "panic"
}
return "unknown"
}
// Entry takes an context.Context and constructs a logrus.Entry from it, adding
// fields for task and subtask information
func Entry(ctx context.Context) *logrus.Entry {
val := ctx.Value(ContextKey)
if eventContext, ok := val.(EventContext); ok {
return logger.WithFields(logrus.Fields{
"task": eventContext.Task,
"subtask": eventContext.Subtask,
})
}
// Use constants.DevLoop as the default task, as it's the highest level task we
// can default to if one isn't specified.
return logger.WithFields(logrus.Fields{
"task": constants.DevLoop,
"subtask": constants.SubtaskIDNone,
})
}
// Returns the output used by the logger to write its content.
func GetWriter() io.Writer {
return logger.Out
}
// IsDebugLevelEnabled returns true if debug level log is enabled.
func IsDebugLevelEnabled() bool {
return logger.IsLevelEnabled(logrus.DebugLevel)
}
// IsTraceLevelEnabled returns true if trace level log is enabled.
func IsTraceLevelEnabled() bool {
return logger.IsLevelEnabled(logrus.TraceLevel)
}
// New returns a new logrus.Logger.
// We use a new instance instead of the default logrus singleton to avoid clashes with dependencies that also use logrus.
func New() *logrus.Logger {
return logrus.New()
}
// KanikoLogLevel makes sure kaniko logs at least at Info level and at most Debug level (trace doesn't work with Kaniko)
func KanikoLogLevel() logrus.Level {
if GetLevel() <= InfoLevel {
return logrus.InfoLevel
}
return logrus.DebugLevel
}
// SetupLogs sets up logrus logger for skaffold command line
func SetupLogs(stdErr io.Writer, level string, timestamp bool, hook logrus.Hook) error {
logger.SetOutput(stdErr)
lvl, err := logrus.ParseLevel(level)
if err != nil {
return fmt.Errorf("parsing log level: %w", err)
}
logger.SetLevel(lvl)
logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: timestamp,
})
logger.AddHook(hook)
setupStdLog(logger, lvl, stdlog.Default())
setupGGCRLogging(logger, lvl)
return nil
}
// AddHook adds a hook to the global Skaffold logger.
func AddHook(hook logrus.Hook) {
logger.AddHook(hook)
}
// SetLevel sets the global Skaffold logger level.
func SetLevel(level Level) {
logger.SetLevel(logrus.AllLevels[level])
}
// GetLevel returns the global Skaffold logger level.
func GetLevel() Level {
return AllLevels[logger.GetLevel()]
}
// setupStdLog writes Go's standard library `log` messages to logrus at Info level.
//
// This function uses SetFlags() to standardize the output format.
func setupStdLog(logger *logrus.Logger, lvl logrus.Level, stdlogger *stdlog.Logger) {
stdlogger.SetFlags(0)
if lvl >= logrus.InfoLevel {
stdlogger.SetOutput(logger.WriterLevel(logrus.InfoLevel))
}
}
// setupGGCRLogging enables go-containerregistry logging, mapping its levels to our levels.
//
// The mapping is:
// - ggcr Warn -> Skaffold Error
// - ggcr Progress -> Skaffold Info
// - ggcr Debug -> Skaffold Trace
//
// The reasons for this mapping are:
// - `ggcr` defines `Warn` as "non-fatal errors": https://github.com/google/go-containerregistry/blob/main/pkg/logs/logs.go#L24
// - `ggcr` `Debug` logging is _very_ verbose and includes HTTP requests and responses, with non-sensitive headers and non-binary payloads.
//
// This function uses SetFlags() to standardize the output format.
func setupGGCRLogging(logger *logrus.Logger, lvl logrus.Level) {
if lvl >= logrus.ErrorLevel {
ggcrlogs.Warn.SetOutput(logger.WriterLevel(logrus.ErrorLevel))
ggcrlogs.Warn.SetFlags(0)
}
if lvl >= logrus.InfoLevel {
ggcrlogs.Progress.SetOutput(logger.WriterLevel(logrus.InfoLevel))
ggcrlogs.Progress.SetFlags(0)
}
if lvl >= logrus.TraceLevel {
ggcrlogs.Debug.SetOutput(logger.WriterLevel(logrus.TraceLevel))
ggcrlogs.Debug.SetFlags(0)
}
}