forked from mirror/logrus
Compare commits
2 Commits
master
...
caller-hoo
Author | SHA1 | Date |
---|---|---|
Simon Eskildsen | f7e0e76df0 | |
Simon Eskildsen | 1b8fbc72c3 |
|
@ -228,6 +228,9 @@ func init() {
|
||||||
* [`github.com/Sirupsen/logrus/hooks/papertrail`](https://github.com/Sirupsen/logrus/blob/master/hooks/papertrail/papertrail.go)
|
* [`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.
|
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)
|
* [`github.com/Sirupsen/logrus/hooks/syslog`](https://github.com/Sirupsen/logrus/blob/master/hooks/syslog/syslog.go)
|
||||||
Send errors to remote syslog server.
|
Send errors to remote syslog server.
|
||||||
Uses standard library `log/syslog` behind the scenes.
|
Uses standard library `log/syslog` behind the scenes.
|
||||||
|
|
|
@ -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 ""
|
||||||
|
}
|
|
@ -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))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
|
@ -223,7 +223,7 @@ func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {
|
||||||
|
|
||||||
err := json.Unmarshal(buffer.Bytes(), &fields)
|
err := json.Unmarshal(buffer.Bytes(), &fields)
|
||||||
assert.NoError(t, err, "should have decoded first message")
|
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["msg"], "looks delicious")
|
||||||
assert.Equal(t, fields["context"], "eating raw fish")
|
assert.Equal(t, fields["context"], "eating raw fish")
|
||||||
|
|
||||||
|
@ -233,7 +233,7 @@ func TestDoubleLoggingDoesntPrefixPreviousFields(t *testing.T) {
|
||||||
|
|
||||||
err = json.Unmarshal(buffer.Bytes(), &fields)
|
err = json.Unmarshal(buffer.Bytes(), &fields)
|
||||||
assert.NoError(t, err, "should have decoded second message")
|
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["msg"], "omg it is!")
|
||||||
assert.Equal(t, fields["context"], "eating raw fish")
|
assert.Equal(t, fields["context"], "eating raw fish")
|
||||||
assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")
|
assert.Nil(t, fields["fields.msg"], "should not have prefixed previous `msg` entry")
|
||||||
|
|
Loading…
Reference in New Issue