Files
fn-serverless/api/runner/func_logger.go
2017-04-19 09:49:12 -06:00

42 lines
1.0 KiB
Go

package runner
import (
"bufio"
"io"
"context"
"github.com/Sirupsen/logrus"
"github.com/kumokit/runner/common"
)
type FuncLogger interface {
Writer(context.Context, string, string, string, string) io.Writer
}
// FuncLogger reads STDERR output from a container and outputs it in a parseable structured log format, see: https://github.com/kumokit/functions/issues/76
type DefaultFuncLogger struct {
}
func NewFuncLogger() FuncLogger {
return &DefaultFuncLogger{}
}
func (l *DefaultFuncLogger) Writer(ctx context.Context, appName, path, image, reqID string) io.Writer {
r, w := io.Pipe()
log := common.Logger(ctx)
log = log.WithFields(logrus.Fields{"user_log": true, "app_name": appName, "path": path, "image": image, "call_id": reqID})
go func(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
log.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
log.WithError(err).Println("There was an error with the scanner in attached container")
}
}(r)
return w
}