diff --git a/pkg/promtail/targets/tailer.go b/pkg/promtail/targets/tailer.go index ae03628b4ffac..0075ef183e4a6 100644 --- a/pkg/promtail/targets/tailer.go +++ b/pkg/promtail/targets/tailer.go @@ -11,6 +11,7 @@ import ( "github.com/grafana/loki/pkg/promtail/api" "github.com/grafana/loki/pkg/promtail/positions" + "github.com/grafana/loki/pkg/util" ) type tailer struct { @@ -54,6 +55,7 @@ func newTailer(logger log.Logger, handler api.EntryHandler, positions *positions return nil, err } + logger = log.With(logger, "component", "tailer") tailer := &tailer{ logger: logger, handler: api.AddLabelsMiddleware(model.LabelSet{FilenameLabel: model.LabelValue(path)}).Wrap(handler), @@ -64,6 +66,8 @@ func newTailer(logger log.Logger, handler api.EntryHandler, positions *positions quit: make(chan struct{}), done: make(chan struct{}), } + tail.Logger = util.NewLogAdapater(logger) + go tailer.run() filesActive.Add(1.) return tailer, nil diff --git a/pkg/util/log.go b/pkg/util/log.go new file mode 100644 index 0000000000000..ae62d521705ab --- /dev/null +++ b/pkg/util/log.go @@ -0,0 +1,74 @@ +package util + +import ( + "fmt" + "os" + "strings" + + "github.com/go-kit/kit/log" + "github.com/go-kit/kit/log/level" +) + +type LogAdapater struct { + log.Logger +} + +func NewLogAdapater(l log.Logger) LogAdapater { + return LogAdapater{ + Logger: l, + } +} + +// Fatal implements tail.logger +func (l LogAdapater) Fatal(v ...interface{}) { + level.Error(l).Log("msg", fmt.Sprint(v...)) + os.Exit(1) +} + +// Fatalf implements tail.logger +func (l LogAdapater) Fatalf(format string, v ...interface{}) { + level.Error(l).Log("msg", fmt.Sprintf(strings.TrimSuffix(format, "\n"), v...)) + os.Exit(1) +} + +// Fatalln implements tail.logger +func (l LogAdapater) Fatalln(v ...interface{}) { + level.Error(l).Log("msg", fmt.Sprint(v...)) + os.Exit(1) +} + +// Panic implements tail.logger +func (l LogAdapater) Panic(v ...interface{}) { + s := fmt.Sprint(v...) + level.Error(l).Log("msg", s) + panic(s) +} + +// Panicf implements tail.logger +func (l LogAdapater) Panicf(format string, v ...interface{}) { + s := fmt.Sprintf(strings.TrimSuffix(format, "\n"), v...) + level.Error(l).Log("msg", s) + panic(s) +} + +// Panicln implements tail.logger +func (l LogAdapater) Panicln(v ...interface{}) { + s := fmt.Sprint(v...) + level.Error(l).Log("msg", s) + panic(s) +} + +// Print implements tail.logger +func (l LogAdapater) Print(v ...interface{}) { + level.Info(l).Log("msg", fmt.Sprint(v...)) +} + +// Printf implements tail.logger +func (l LogAdapater) Printf(format string, v ...interface{}) { + level.Info(l).Log("msg", fmt.Sprintf(strings.TrimSuffix(format, "\n"), v...)) +} + +// Println implements tail.logger +func (l LogAdapater) Println(v ...interface{}) { + level.Info(l).Log("msg", fmt.Sprint(v...)) +}