Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix tail library logs to use our own log format #579

Merged
merged 2 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/promtail/targets/tailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -49,6 +50,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),
Expand All @@ -59,6 +61,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
Expand Down
74 changes: 74 additions & 0 deletions pkg/util/log.go
Original file line number Diff line number Diff line change
@@ -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...))
}