Skip to content

Commit

Permalink
Merge pull request #51 from hashicorp/b-intercept-caller
Browse files Browse the repository at this point in the history
Adjust runtime caller offset if necessary
  • Loading branch information
Evan Phoenix authored Oct 25, 2019
2 parents f0d74a5 + b012279 commit 2348337
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
33 changes: 33 additions & 0 deletions interceptlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,37 @@ func TestInterceptLogger(t *testing.T) {
rest = output[dataIdx+1:]
assert.Equal(t, "[DEBUG] with_name: test log\n", rest)
})

t.Run("includes the caller location", func(t *testing.T) {
var buf bytes.Buffer
var sbuf bytes.Buffer

logger := NewInterceptLogger(&LoggerOptions{
Name: "test",
Output: &buf,
IncludeLocation: true,
})

sink := NewSinkAdapter(&LoggerOptions{
IncludeLocation: true,
Level: Debug,
Output: &sbuf,
})
logger.RegisterSink(sink)
defer logger.DeregisterSink(sink)

logger.Info("this is test", "who", "programmer", "why", "testing is fun")

str := buf.String()
dataIdx := strings.IndexByte(str, ' ')
rest := str[dataIdx+1:]

// This test will break if you move this around, it's line dependent, just fyi
assert.Equal(t, "[INFO] go-hclog/interceptlogger_test.go:280: test: this is test: who=programmer why=\"testing is fun\"\n", rest)

str = sbuf.String()
dataIdx = strings.IndexByte(str, ' ')
rest = str[dataIdx+1:]
assert.Equal(t, "[INFO] go-hclog/interceptlogger_test.go:280: test: this is test: who=programmer why=\"testing is fun\"\n", rest)
})
}
15 changes: 14 additions & 1 deletion intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"os"
"reflect"
"regexp"
"runtime"
"sort"
"strconv"
Expand Down Expand Up @@ -167,6 +168,8 @@ func trimCallerPath(path string) string {
return path[idx+1:]
}

var logImplFile = regexp.MustCompile(`github.com/hashicorp/go-hclog/.+logger.go$`)

// Non-JSON logging format function
func (l *intLogger) log(t time.Time, name string, level Level, msg string, args ...interface{}) {
l.writer.WriteString(t.Format(l.timeFormat))
Expand All @@ -179,8 +182,18 @@ func (l *intLogger) log(t time.Time, name string, level Level, msg string, args
l.writer.WriteString("[?????]")
}

offset := 3
if l.caller {
if _, file, line, ok := runtime.Caller(3); ok {
// Check if the caller is inside our package and inside
// a logger implementation file
if _, file, _, ok := runtime.Caller(3); ok {
match := logImplFile.MatchString(file)
if match {
offset = 4
}
}

if _, file, line, ok := runtime.Caller(offset); ok {
l.writer.WriteByte(' ')
l.writer.WriteString(trimCallerPath(file))
l.writer.WriteByte(':')
Expand Down
2 changes: 1 addition & 1 deletion logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestLogger(t *testing.T) {
rest := str[dataIdx+1:]

// This test will break if you move this around, it's line dependent, just fyi
assert.Equal(t, "[INFO] go-hclog/logger_test.go:146: test: this is test: who=programmer why=\"testing is fun\"\n", rest)
assert.Equal(t, "[INFO] go-hclog/logger_test.go:129: test: this is test: who=programmer why=\"testing is fun\"\n", rest)
})

t.Run("prefixes the name", func(t *testing.T) {
Expand Down

0 comments on commit 2348337

Please sign in to comment.