Commit 337dccb6 authored by Josh Roppo's avatar Josh Roppo

Adding ability to utilize Logrus library for structured logging

If a Logrus logger is intialized it will be used to log messages to the
configured output. This allows usage of Logrus's JSON Formatter.
parent a029bd18
......@@ -8,6 +8,8 @@ import (
"strings"
"sync"
"time"
"github.com/Sirupsen/logrus"
)
const (
......@@ -44,6 +46,7 @@ var (
EMPTY struct{}
ErrLogLevel int = ERROR
logger *log.Logger
rus *logrus.Logger
loggerErr *log.Logger
LogColor = map[int]string{FATAL: "\033[0m\033[37m",
ERROR: "\033[0m\033[31m",
......@@ -78,6 +81,24 @@ func SetupLoggingLong(lvl string) {
SetLogger(log.New(os.Stderr, "", log.LstdFlags|log.Llongfile|log.Lmicroseconds), strings.ToLower(lvl))
}
func SetupLogrus(lvl string) {
loglvl, err := logrus.ParseLevel(lvl)
if err != nil {
fmt.Printf("error parsing log level: %v", err)
}
rus = &logrus.Logger{
Out: os.Stdout,
Formatter: new(logrus.JSONFormatter),
Hooks: make(logrus.LevelHooks),
Level: loglvl,
}
}
func GetRus() *logrus.Logger {
return rus
}
// Setup colorized output if this is a terminal
func SetColorIfTerminal() {
if IsTerminal() {
......@@ -434,10 +455,31 @@ func DoLog(depth, logLvl int, msg string) {
if escapeNewlines {
msg = EscapeNewlines(msg)
}
if ErrLogLevel >= logLvl && loggerErr != nil {
loggerErr.Output(depth, LogPrefix[logLvl]+msg+postFix)
} else if LogLevel >= logLvl && logger != nil {
logger.Output(depth, LogPrefix[logLvl]+msg+postFix)
if rus == nil {
// Use standard logger
if ErrLogLevel >= logLvl && loggerErr != nil {
loggerErr.Output(depth, LogPrefix[logLvl]+msg+postFix)
} else if LogLevel >= logLvl && logger != nil {
logger.Output(depth, LogPrefix[logLvl]+msg+postFix)
}
} else {
// Write logs using Logrus logger
logrusLvl := logrus.Level(logLvl) + 1
switch logrusLvl {
case logrus.FatalLevel:
rus.Fatal(msg)
case logrus.ErrorLevel:
rus.Error(msg)
case logrus.WarnLevel:
rus.Warn(msg)
case logrus.InfoLevel:
rus.Info(msg)
case logrus.DebugLevel:
rus.Debug(msg)
default:
rus.Warn("!invalid log level! " + msg)
}
}
}
......
package gou
import (
"io/ioutil"
"os"
"strings"
"testing"
"time"
)
func TestSetupLogToFile(t *testing.T) {
tmpf, err := ioutil.TempFile("", "goutest")
if err != nil {
t.Fatalf("error creating log file: %v\n", err)
}
defer os.Remove(tmpf.Name())
//SetupLoggingFile(tmpf, "debug")
logStr := "hihi"
Infof(logStr)
// Flush file buffer to disk
err = tmpf.Sync()
if err != nil {
t.Errorf("error syncing tmpf: %v", err)
}
time.Sleep(1 * time.Second)
// Read tmp file and confirm log message was written
bytes, err := ioutil.ReadFile(tmpf.Name())
if err != nil {
t.Errorf("error reading temp file[%s]: %v\n", tmpf.Name(), err)
}
logFileBytes := string(bytes)
if !strings.Contains(logFileBytes, logStr) {
t.Logf("logfile:\n%s", logFileBytes)
t.Errorf("%s not found in logfile %s\n", logStr, tmpf.Name())
}
}
func TestLogrusLogger(t *testing.T) {
SetupLogrus("debug")
Debug("Debug")
Infof("Info")
Warn("Warn")
Error("Error")
rus = nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment