-
Notifications
You must be signed in to change notification settings - Fork 10
/
logger.go
59 lines (51 loc) · 1.33 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
package taskrunner
import (
"context"
"fmt"
"io"
"os"
)
// LoggerFromContext gets the logger from the context.
func LoggerFromContext(ctx context.Context) *Logger {
if logger, ok := ctx.Value(loggerKey{}).(*Logger); ok {
return logger
}
return nil
}
type loggerKey struct{}
type LogProvider func(task *Task) (*Logger, error)
type Logger struct {
Stdout io.Writer
Stderr io.Writer
}
// eventLogger is an io.Writer that publishes to the taskrunner
// executor-level logger.
type eventLogger struct {
executor *Executor
task *Task
stream TaskLogEventStream
}
// Write implements io.Writer.
func (l *eventLogger) Write(p []byte) (int, error) {
l.executor.publishEvent(&TaskLogEvent{
simpleEvent: l.executor.taskExecution(l.task).simpleEvent(),
Message: string(p),
Stream: l.stream,
})
return len(p), nil
}
// Logf writes a log message to the stdout logger for the task.
// It will append a newline if one is not present at the end.
func Logf(ctx context.Context, f string, args ...interface{}) {
logger := LoggerFromContext(ctx)
if logger == nil {
fmt.Fprintln(os.Stderr, "🟡 Warning: There was no logger found in context, so falling back to `fmt.Printf`")
fmt.Printf(f, args...)
return
}
s := fmt.Sprintf(f, args...)
if len(s) == 0 || s[len(s)-1] != '\n' {
s += "\n"
}
fmt.Fprint(logger.Stdout, s)
}