Skip to content

Commit

Permalink
Add an AdditionalLocationOffset option to LoggerOptions.
Browse files Browse the repository at this point in the history
Add the ability for wrappers of hclog to have the file and line number
point to their caller, instead of the wrappers, by specifying additional
offsets for the callstack when creating a logger.
  • Loading branch information
paddycarver committed May 10, 2021
1 parent 38301c5 commit ee9ecbd
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func newLogger(opts *LoggerOptions) *intLogger {
independentLevels: opts.IndependentLevels,
}
if opts.IncludeLocation {
l.callerOffset = offsetIntLogger
l.callerOffset = offsetIntLogger + opts.AdditionalLocationOffset
}

if l.json {
Expand Down
4 changes: 4 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ type LoggerOptions struct {
// Include file and line information in each log line
IncludeLocation bool

// AdditionalLocationOffset is the number of additional stack levels to skip
// when finding the file and line information for the log line
AdditionalLocationOffset int

// The time format to use instead of the default
TimeFormat string

Expand Down
54 changes: 54 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,30 @@ func TestLogger(t *testing.T) {
assert.Equal(t, "[INFO] go-hclog/logger_test.go:169: test: this is test: who=programmer why=\"testing is fun\"\n", rest)
})

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

logMe := func(l Logger) {
l.Info("this is test", "who", "programmer", "why", "testing is fun")
}

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

logMe(logger)

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/logger_test.go:193: test: this is test: who=programmer why=\"testing is fun\"\n", rest)
})

t.Run("prefixes the name", func(t *testing.T) {
var buf bytes.Buffer

Expand Down Expand Up @@ -805,6 +829,36 @@ func TestLogger_JSON(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%v:%d", file, line-1), raw["@caller"])
})

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

logMe := func(l Logger) {
l.Info("this is test", "who", "programmer", "why", "testing is fun")
}

logger := New(&LoggerOptions{
Name: "test",
Output: &buf,
JSONFormat: true,
IncludeLocation: true,
AdditionalLocationOffset: 1,
})

logMe(logger)
_, file, line, ok := runtime.Caller(0)
require.True(t, ok)

b := buf.Bytes()

var raw map[string]interface{}
if err := json.Unmarshal(b, &raw); err != nil {
t.Fatal(err)
}

assert.Equal(t, "this is test", raw["@message"])
assert.Equal(t, fmt.Sprintf("%v:%d", file, line-1), raw["@caller"])
})

t.Run("handles non-serializable entries", func(t *testing.T) {
var buf bytes.Buffer

Expand Down
29 changes: 29 additions & 0 deletions stdlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,32 @@ func TestFromStandardLogger(t *testing.T) {
prefix := "test-stdlib-log "
require.Equal(t, prefix, actual[:16])
}

func TestFromStandardLogger_helper(t *testing.T) {
var buf bytes.Buffer

sl := log.New(&buf, "test-stdlib-log ", log.Ltime)

hl := FromStandardLogger(sl, &LoggerOptions{
Name: "hclog-inner",
IncludeLocation: true,
AdditionalLocationOffset: 1,
})

helper := func() {
hl.Info("this is a test", "name", "tester", "count", 1)
}

helper()
_, file, line, ok := runtime.Caller(0)
require.True(t, ok)

actual := buf.String()
suffix := fmt.Sprintf(
"[INFO] go-hclog/%s:%d: hclog-inner: this is a test: name=tester count=1\n",
filepath.Base(file), line-1)
require.Equal(t, suffix, actual[25:])

prefix := "test-stdlib-log "
require.Equal(t, prefix, actual[:16])
}

0 comments on commit ee9ecbd

Please sign in to comment.