Switch from basic logger to logrus

This commit is contained in:
Bryan Boreham committed 2015-07-01 12:26:23 +01:00
1 parent badfdee5e4
commit c786e7eb5a
2 files changed
+55 -15

No files matched your search

+54 -14
View File
@@ -1,24 +1,44 @@
package common package common
import ( import (
"bytes"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"strings"
"github.com/Sirupsen/logrus"
) )
const ( type textFormatter struct {
standardLogFlags = log.Ldate | log.Ltime | log.Lmicroseconds }
)
// Largely taken from // Based off logrus.TextFormatter, which behaves completely
// http://www.goinggo.net/2013/11/using-log-package-in-go.html // differently when you don't want colored output
func (f *textFormatter) Format(entry *logrus.Entry) ([]byte, error) {
b := &bytes.Buffer{}
levelText := strings.ToUpper(entry.Level.String())[0:4]
timeStamp := entry.Time.Format("2006/01/02 15:04:05.000000")
fmt.Fprintf(b, "%s: %s %-44s ", levelText, timeStamp, entry.Message)
for k, v := range entry.Data {
fmt.Fprintf(b, " %s=%v", k, v)
}
b.WriteByte('\n')
return b.Bytes(), nil
}
var ( var (
Debug *log.Logger standardTextFormatter = &textFormatter{}
Info *log.Logger )
Warning *log.Logger
Error *log.Logger var (
Debug *logrus.Logger
Info *logrus.Logger
Warning *logrus.Logger
Error *logrus.Logger
debugF bool debugF bool
) )
@@ -27,10 +47,30 @@ func InitLogging(debugHandle io.Writer,
warningHandle io.Writer, warningHandle io.Writer,
errorHandle io.Writer) { errorHandle io.Writer) {
Debug = log.New(debugHandle, "DEBUG: ", standardLogFlags) Debug = &logrus.Logger{
Info = log.New(infoHandle, "INFO: ", standardLogFlags) Out: debugHandle,
Warning = log.New(warningHandle, "WARNING: ", standardLogFlags) Formatter: standardTextFormatter,
Error = log.New(errorHandle, "ERROR: ", standardLogFlags) Hooks: make(logrus.LevelHooks),
Level: logrus.DebugLevel,
}
Info = &logrus.Logger{
Out: infoHandle,
Formatter: standardTextFormatter,
Hooks: make(logrus.LevelHooks),
Level: logrus.InfoLevel,
}
Warning = &logrus.Logger{
Out: warningHandle,
Formatter: standardTextFormatter,
Hooks: make(logrus.LevelHooks),
Level: logrus.WarnLevel,
}
Error = &logrus.Logger{
Out: errorHandle,
Formatter: standardTextFormatter,
Hooks: make(logrus.LevelHooks),
Level: logrus.ErrorLevel,
}
} }
func InitDefaultLogging(debug bool) { func InitDefaultLogging(debug bool) {
+1 -1
View File
@@ -2,10 +2,10 @@ package nameserver
import ( import (
"fmt" "fmt"
"log"
"net" "net"
"net/http" "net/http"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/miekg/dns" "github.com/miekg/dns"
. "github.com/weaveworks/weave/common" . "github.com/weaveworks/weave/common"