Skip to content
New issue

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

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

Already on GitHub? # to your account

Also set global logging when initializing logrus #122

Merged
merged 2 commits into from
Sep 3, 2018
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
19 changes: 11 additions & 8 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ import (
"fmt"
"os"

log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"

"github.com/weaveworks/promrus"
)

// Setup configures logging output to stderr, sets the log level and sets the formatter.
// Setup configures a global logrus logger to output to stderr.
// It populates the standard logrus logger as well as the global logging instance.
func Setup(logLevel string) error {
log.SetOutput(os.Stderr)
level, err := log.ParseLevel(logLevel)
level, err := logrus.ParseLevel(logLevel)
if err != nil {
return fmt.Errorf("Error parsing log level: %v", err)
return fmt.Errorf("error parsing log level: %v", err)
}
log.SetLevel(level)
log.SetFormatter(&textFormatter{})
hook, err := promrus.NewPrometheusHook() // Expose number of log messages as Prometheus metrics.
if err != nil {
return err
}
log.AddHook(hook)
logrus.SetOutput(os.Stderr)
logrus.SetLevel(level)
logrus.SetFormatter(&textFormatter{})
logrus.AddHook(hook)
SetGlobal(Logrus(logrus.StandardLogger()))
return nil
}
14 changes: 7 additions & 7 deletions logging/logrus.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@ type logrusLogger struct {
}

func (l logrusLogger) WithField(key string, value interface{}) Interface {
return logusEntry{
return logrusEntry{
Entry: l.Logger.WithField(key, value),
}
}

func (l logrusLogger) WithFields(fields Fields) Interface {
return logusEntry{
return logrusEntry{
Entry: l.Logger.WithFields(map[string]interface{}(fields)),
}
}

type logusEntry struct {
type logrusEntry struct {
*logrus.Entry
}

func (l logusEntry) WithField(key string, value interface{}) Interface {
return logusEntry{
func (l logrusEntry) WithField(key string, value interface{}) Interface {
return logrusEntry{
Entry: l.Entry.WithField(key, value),
}
}

func (l logusEntry) WithFields(fields Fields) Interface {
return logusEntry{
func (l logrusEntry) WithFields(fields Fields) Interface {
return logrusEntry{
Entry: l.Entry.WithFields(map[string]interface{}(fields)),
}
}
Expand Down