Reorg logging package
This moves the logger interface into the root package and makes the log subpackage specific to logrus. Also this makes the Logger interface type also return other Loggers, such that we can completely encapsulate logrus.Loggers.pull/141/head
parent
62a3ae9197
commit
964ba68f39
|
@ -7,7 +7,6 @@ import (
|
|||
"net/url"
|
||||
|
||||
"github.com/influxdata/mrfusion"
|
||||
"github.com/influxdata/mrfusion/log"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
@ -16,13 +15,13 @@ import (
|
|||
type Client struct {
|
||||
URL *url.URL
|
||||
|
||||
lg log.Logger
|
||||
lg mrfusion.Logger
|
||||
}
|
||||
|
||||
// NewClient initializes an HTTP Client for InfluxDB. UDP, although supported
|
||||
// for querying InfluxDB, is not supported here to remove the need to
|
||||
// explicitly Close the client.
|
||||
func NewClient(host string, lg log.Logger) (*Client, error) {
|
||||
func NewClient(host string, lg mrfusion.Logger) (*Client, error) {
|
||||
l := lg.WithField("host", host)
|
||||
u, err := url.Parse(host)
|
||||
if err != nil {
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package mrfusion
|
||||
|
||||
// Logger represents an abstracted structured logging implementation. It
|
||||
// provides methods to trigger log messages at various alert levels and a
|
||||
// WithField method to set keys for a structured log message.
|
||||
type Logger interface {
|
||||
Info(...interface{})
|
||||
Warn(...interface{})
|
||||
Debug(...interface{})
|
||||
Panic(...interface{})
|
||||
Error(...interface{})
|
||||
|
||||
WithField(string, interface{}) Logger
|
||||
}
|
44
log/log.go
44
log/log.go
|
@ -4,23 +4,47 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/influxdata/mrfusion"
|
||||
)
|
||||
|
||||
type Logger interface {
|
||||
Info(...interface{})
|
||||
Warn(...interface{})
|
||||
Debug(...interface{})
|
||||
Panic(...interface{})
|
||||
Error(...interface{})
|
||||
|
||||
WithField(string, interface{}) *logrus.Entry
|
||||
// LogrusLogger is a mrfusion.Logger that uses logrus to process logs
|
||||
type logrusLogger struct {
|
||||
l *logrus.Entry
|
||||
}
|
||||
|
||||
func New() Logger {
|
||||
return &logrus.Logger{
|
||||
func (ll *logrusLogger) Info(items ...interface{}) {
|
||||
ll.l.Info(items...)
|
||||
}
|
||||
|
||||
func (ll *logrusLogger) Warn(items ...interface{}) {
|
||||
ll.l.Warn(items...)
|
||||
}
|
||||
|
||||
func (ll *logrusLogger) Debug(items ...interface{}) {
|
||||
ll.l.Debug(items...)
|
||||
}
|
||||
|
||||
func (ll *logrusLogger) Panic(items ...interface{}) {
|
||||
ll.l.Panic(items...)
|
||||
}
|
||||
|
||||
func (ll *logrusLogger) Error(items ...interface{}) {
|
||||
ll.l.Error(items...)
|
||||
}
|
||||
|
||||
func (ll *logrusLogger) WithField(key string, value interface{}) mrfusion.Logger {
|
||||
return &logrusLogger{ll.l.WithField(key, value)}
|
||||
}
|
||||
|
||||
func New() mrfusion.Logger {
|
||||
logger := &logrus.Logger{
|
||||
Out: os.Stderr,
|
||||
Formatter: new(logrus.TextFormatter),
|
||||
Hooks: make(logrus.LevelHooks),
|
||||
Level: logrus.DebugLevel,
|
||||
}
|
||||
|
||||
return &logrusLogger{
|
||||
l: logrus.NewEntry(logger),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue