Skip to content

Commit

Permalink
Made file name/line number part of logrus core
Browse files Browse the repository at this point in the history
  • Loading branch information
derekdowling committed Nov 9, 2014
1 parent 82dbf65 commit f7cec57
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 48 deletions.
25 changes: 25 additions & 0 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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])
}
2 changes: 2 additions & 0 deletions logrus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}

Expand All @@ -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")
})
}

Expand Down
48 changes: 0 additions & 48 deletions logtext.go

This file was deleted.

0 comments on commit f7cec57

Please sign in to comment.