Skip to content

Commit 1700dc1

Browse files
author
Evan Phoenix
authored
Merge pull request #93 from hashicorp/paddy_location_offset
Add an AdditionalLocationOffset option to LoggerOptions.
2 parents 38301c5 + ee9ecbd commit 1700dc1

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

intlogger.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func newLogger(opts *LoggerOptions) *intLogger {
124124
independentLevels: opts.IndependentLevels,
125125
}
126126
if opts.IncludeLocation {
127-
l.callerOffset = offsetIntLogger
127+
l.callerOffset = offsetIntLogger + opts.AdditionalLocationOffset
128128
}
129129

130130
if l.json {

logger.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ type LoggerOptions struct {
235235
// Include file and line information in each log line
236236
IncludeLocation bool
237237

238+
// AdditionalLocationOffset is the number of additional stack levels to skip
239+
// when finding the file and line information for the log line
240+
AdditionalLocationOffset int
241+
238242
// The time format to use instead of the default
239243
TimeFormat string
240244

logger_test.go

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

179+
t.Run("includes the caller location excluding helper functions", func(t *testing.T) {
180+
var buf bytes.Buffer
181+
182+
logMe := func(l Logger) {
183+
l.Info("this is test", "who", "programmer", "why", "testing is fun")
184+
}
185+
186+
logger := New(&LoggerOptions{
187+
Name: "test",
188+
Output: &buf,
189+
IncludeLocation: true,
190+
AdditionalLocationOffset: 1,
191+
})
192+
193+
logMe(logger)
194+
195+
str := buf.String()
196+
dataIdx := strings.IndexByte(str, ' ')
197+
rest := str[dataIdx+1:]
198+
199+
// This test will break if you move this around, it's line dependent, just fyi
200+
assert.Equal(t, "[INFO] go-hclog/logger_test.go:193: test: this is test: who=programmer why=\"testing is fun\"\n", rest)
201+
})
202+
179203
t.Run("prefixes the name", func(t *testing.T) {
180204
var buf bytes.Buffer
181205

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

832+
t.Run("includes the caller location excluding helper functions", func(t *testing.T) {
833+
var buf bytes.Buffer
834+
835+
logMe := func(l Logger) {
836+
l.Info("this is test", "who", "programmer", "why", "testing is fun")
837+
}
838+
839+
logger := New(&LoggerOptions{
840+
Name: "test",
841+
Output: &buf,
842+
JSONFormat: true,
843+
IncludeLocation: true,
844+
AdditionalLocationOffset: 1,
845+
})
846+
847+
logMe(logger)
848+
_, file, line, ok := runtime.Caller(0)
849+
require.True(t, ok)
850+
851+
b := buf.Bytes()
852+
853+
var raw map[string]interface{}
854+
if err := json.Unmarshal(b, &raw); err != nil {
855+
t.Fatal(err)
856+
}
857+
858+
assert.Equal(t, "this is test", raw["@message"])
859+
assert.Equal(t, fmt.Sprintf("%v:%d", file, line-1), raw["@caller"])
860+
})
861+
808862
t.Run("handles non-serializable entries", func(t *testing.T) {
809863
var buf bytes.Buffer
810864

stdlog_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,32 @@ func TestFromStandardLogger(t *testing.T) {
181181
prefix := "test-stdlib-log "
182182
require.Equal(t, prefix, actual[:16])
183183
}
184+
185+
func TestFromStandardLogger_helper(t *testing.T) {
186+
var buf bytes.Buffer
187+
188+
sl := log.New(&buf, "test-stdlib-log ", log.Ltime)
189+
190+
hl := FromStandardLogger(sl, &LoggerOptions{
191+
Name: "hclog-inner",
192+
IncludeLocation: true,
193+
AdditionalLocationOffset: 1,
194+
})
195+
196+
helper := func() {
197+
hl.Info("this is a test", "name", "tester", "count", 1)
198+
}
199+
200+
helper()
201+
_, file, line, ok := runtime.Caller(0)
202+
require.True(t, ok)
203+
204+
actual := buf.String()
205+
suffix := fmt.Sprintf(
206+
"[INFO] go-hclog/%s:%d: hclog-inner: this is a test: name=tester count=1\n",
207+
filepath.Base(file), line-1)
208+
require.Equal(t, suffix, actual[25:])
209+
210+
prefix := "test-stdlib-log "
211+
require.Equal(t, prefix, actual[:16])
212+
}

0 commit comments

Comments
 (0)