Compare commits

...

2 Commits

Author SHA1 Message Date
Simon Eskildsen f7e0e76df0 test/logrus: remove deprecated assert.Len() calls 2014-12-25 19:50:17 +01:00
Simon Eskildsen 1b8fbc72c3 hooks/caller: add caller hook for caller info 2014-12-25 19:49:40 +01:00
4 changed files with 82 additions and 2 deletions

View File

@ -228,6 +228,9 @@ func init() {
* [`github.com/Sirupsen/logrus/hooks/papertrail`](https://github.com/Sirupsen/logrus/blob/master/hooks/papertrail/papertrail.go)
Send errors to the Papertrail hosted logging service via UDP.
* [`github.com/Sirupsen/logrus/hooks/caller`](https://github.com/Sirupsen/logrus/blob/master/hooks/caller)
Include `caller=<file>:<line>` in log entries.
* [`github.com/Sirupsen/logrus/hooks/syslog`](https://github.com/Sirupsen/logrus/blob/master/hooks/syslog/syslog.go)
Send errors to remote syslog server.
Uses standard library `log/syslog` behind the scenes.

36
hooks/caller/caller.go Normal file
View File

@ -0,0 +1,36 @@
package logrus_caller
import (
"github.com/Sirupsen/logrus"
"path/filepath"
"runtime"
"strconv"
"strings"
)
type CallerHook struct {
}
func (hook *CallerHook) Fire(entry *logrus.Entry) error {
entry.Data["caller"] = hook.caller()
return nil
}
func (hook *CallerHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.PanicLevel,
logrus.FatalLevel,
logrus.ErrorLevel,
logrus.WarnLevel,
logrus.InfoLevel,
logrus.DebugLevel,
}
}
func (hook *CallerHook) caller() string {
if _, file, line, ok := runtime.Caller(6); ok {
return strings.Join([]string{filepath.Base(file), strconv.Itoa(line)}, ":")
}
// not sure what the convention should be here
return ""
}

View File

@ -0,0 +1,41 @@
package logrus_caller
import (
"bytes"
"encoding/json"
"fmt"
"testing"
"github.com/Sirupsen/logrus"
)
func LogAndAssertJSON(t *testing.T, log func(*logrus.Logger), assertions func(fields logrus.Fields)) {
var buffer bytes.Buffer
var fields logrus.Fields
logger := logrus.New()
logger.Hooks.Add(&CallerHook{})
logger.Out = &buffer
logger.Formatter = new(logrus.JSONFormatter)
log(logger)
err := json.Unmarshal(buffer.Bytes(), &fields)
if err != nil {
t.Error("Error unmarshaling log entry")
}
assertions(fields)
}
func TestCaller(t *testing.T) {
LogAndAssertJSON(t, func(logger *logrus.Logger) {
logger.Info("Hello World")
}, func(fields logrus.Fields) {
expected := "caller_test.go:33"
if fields["caller"] != expected {
t.Error(fmt.Sprintf("Caller was %s, expected %s", fields["caller"], expected))
}
})
}

View File

@ -223,7 +223,7 @@ func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {
err := json.Unmarshal(buffer.Bytes(), &fields)
assert.NoError(t, err, "should have decoded first message")
assert.Len(t, fields, 4, "should only have msg/time/level/context fields")
assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields")
assert.Equal(t, fields["msg"], "looks delicious")
assert.Equal(t, fields["context"], "eating raw fish")
@ -233,7 +233,7 @@ func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {
err = json.Unmarshal(buffer.Bytes(), &fields)
assert.NoError(t, err, "should have decoded second message")
assert.Len(t, fields, 4, "should only have msg/time/level/context fields")
assert.Equal(t, len(fields), 4, "should only have msg/time/level/context fields")
assert.Equal(t, fields["msg"], "omg it is!")
assert.Equal(t, fields["context"], "eating raw fish")
assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")