diff --git a/entry.go b/entry.go index a77c4b0ed..03bfd774a 100644 --- a/entry.go +++ b/entry.go @@ -5,7 +5,11 @@ import ( "fmt" "io" "os" + "runtime" + "strconv" + "strings" "time" + "path/filepath" ) // An entry is the final or intermediate Logrus logging entry. It contains all @@ -74,6 +78,11 @@ func (entry *Entry) log(level Level, msg string) { entry.Time = time.Now() entry.Level = level entry.Message = msg + entry.Data["caller"] = context() + + // if level == WarnLevel || level == InfoLevel { + // entry.Data["trace"] = trace() + // } if err := entry.Logger.Hooks.Fire(level, entry); err != nil { entry.Logger.mu.Lock() @@ -246,3 +255,19 @@ func (entry *Entry) sprintlnn(args ...interface{}) string { msg := fmt.Sprintln(args...) return msg[:len(msg)-1] } + +// Captures where the log call came from and formats it for output +func context() string { + if _, file, line, ok := runtime.Caller(4); ok { + return strings.Join([]string{filepath.Base(file), strconv.Itoa(line)}, ":") + } + // not sure what the convention should be here + return "unavailable" +} + +// handles getting the stack trace and returns it as a string +func trace() string { + stack := make([]byte, 2048) + size := runtime.Stack(stack, false) + return string(stack[:size]) +} diff --git a/logrus_test.go b/logrus_test.go index 15157d172..fab0a6d43 100644 --- a/logrus_test.go +++ b/logrus_test.go @@ -57,6 +57,7 @@ func TestPrint(t *testing.T) { }, func(fields Fields) { assert.Equal(t, fields["msg"], "test") assert.Equal(t, fields["level"], "info") + assert.Equal(t, fields["caller"], "logrus_test.go:56") }) } @@ -66,6 +67,7 @@ func TestInfo(t *testing.T) { }, func(fields Fields) { assert.Equal(t, fields["msg"], "test") assert.Equal(t, fields["level"], "info") + assert.Equal(t, fields["caller"], "logrus_test.go:66") }) } diff --git a/logtext.go b/logtext.go deleted file mode 100644 index 0cc9b2c22..000000000 --- a/logtext.go +++ /dev/null @@ -1,48 +0,0 @@ -package logtext - -// A Logrus hook that adds filename/line number/stack trace to our log outputs - -import ( - "github.com/Sirupsen/logrus" - "runtime" -) - -// Log depth is how many levels to ascend to find where the actual log call occurred -// while debugOnly sets whether or not stack traces should be printed outside of -// debug prints -type Logtext struct{ - Formatter logrus.Formatter - LogDepth int - DebugOnly bool -} - -func NewLogtext(formatter logrus.Formatter, debugOnly bool) *Logtext { - return &Logtext{ - LogDepth: 4, - Formatter: formatter, - DebugOnly: debugOnly, - } -} - -// Creates a hook to be added to an instance of logger. This is called with -func (hook *Logtext) Format(entry *logrus.Entry) ([]byte, error) { - - if _, file, line, ok := runtime.Caller(hook.LogDepth); ok { - entry.Data["line"] = line - entry.Data["file"] = file - } - - if !hook.DebugOnly || entry.Level == logrus.DebugLevel { - stack := getTrace() - entry.Data["stack"] = stack - } - - return hook.Formatter.Format(entry) -} - -// handles getting the stack trace and returns it as a string -func getTrace() string { - stack := make([]byte, 2048) - size := runtime.Stack(stack, false) - return string(stack[:size]) -}