Skip to content

Commit

Permalink
Also set global logging when initializing logrus (#122)
Browse files Browse the repository at this point in the history
* Refactor

- s/logusEntry/logrusEntry

* Also set global logging when initializing logrus

This PR sets the global logging instance when calling `logging.Setup(level)`.
Previously, only the standard logrus logger was set up and if anyone used
the new funcs such as `logging.Info()` it was discarded.
  • Loading branch information
rndstr authored Sep 3, 2018
1 parent 6a85bf5 commit 1d0e909
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
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

0 comments on commit 1d0e909

Please sign in to comment.