Skip to content

Commit 79fd0af

Browse files
Fix caller depth when using .Log method (#72)
1 parent 592cd35 commit 79fd0af

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

logger.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,50 @@ func (l *Logger) onError(error error, entry Entry) {
5151

5252
// Debug will log a debug message
5353
func (l *Logger) Debug(message string, fields ...Field) {
54-
l.Log(message, DebugLevel, fields...)
54+
l.log(message, DebugLevel, fields...)
5555
}
5656

5757
// Info will log a info message
5858
func (l *Logger) Info(message string, fields ...Field) {
59-
l.Log(message, InfoLevel, fields...)
59+
l.log(message, InfoLevel, fields...)
6060
}
6161

6262
// Notice will log a notice message
6363
func (l *Logger) Notice(message string, fields ...Field) {
64-
l.Log(message, NoticeLevel, fields...)
64+
l.log(message, NoticeLevel, fields...)
6565
}
6666

6767
// Warning will log a warning message
6868
func (l *Logger) Warning(message string, fields ...Field) {
69-
l.Log(message, WarningLevel, fields...)
69+
l.log(message, WarningLevel, fields...)
7070
}
7171

7272
// Error will log a message
7373
func (l *Logger) Error(message string, fields ...Field) {
74-
l.Log(message, ErrorLevel, fields...)
74+
l.log(message, ErrorLevel, fields...)
7575
}
7676

7777
// Critical will log a critical message
7878
func (l *Logger) Critical(message string, fields ...Field) {
79-
l.Log(message, CriticalLevel, fields...)
79+
l.log(message, CriticalLevel, fields...)
8080
}
8181

8282
// Alert will log a alert message
8383
func (l *Logger) Alert(message string, fields ...Field) {
84-
l.Log(message, AlertLevel, fields...)
84+
l.log(message, AlertLevel, fields...)
8585
}
8686

8787
// Emergency will log a emergency message
8888
func (l *Logger) Emergency(message string, fields ...Field) {
89-
l.Log(message, EmergencyLevel, fields...)
89+
l.log(message, EmergencyLevel, fields...)
9090
}
9191

9292
// Log will log a message with a given level
9393
func (l *Logger) Log(message string, level Level, fields ...Field) {
94+
l.log(message, level, fields...)
95+
}
96+
97+
func (l *Logger) log(message string, level Level, fields ...Field) {
9498
entry := Entry{message, level, NewContext(fields...)}
9599
if err := l.handler(entry); err != nil {
96100
l.onError(err, entry)

middleware/caller_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package middleware_test
33
import (
44
"testing"
55

6+
"github.com/gol4ng/logger/handler"
67
"github.com/stretchr/testify/assert"
78

89
"github.com/gol4ng/logger"
@@ -16,7 +17,7 @@ func TestCaller_Handle(t *testing.T) {
1617
contextStr := entry.Context.String()
1718
assert.Contains(t, contextStr, "<file:")
1819
assert.Contains(t, contextStr, "caller_test.go")
19-
assert.Contains(t, contextStr, "<line:31>")
20+
assert.Contains(t, contextStr, "<line:32>")
2021

2122
return nil
2223
}
@@ -30,3 +31,23 @@ func TestCaller_Handle(t *testing.T) {
3031
}
3132
assert.Nil(t, caller(mockHandler)(logEntry))
3233
}
34+
35+
func TestCaller_Handle_SameCallerDepth(t *testing.T) {
36+
caller := middleware.Caller(3)
37+
m := handler.NewMemory()
38+
l := logger.NewLogger(caller(m.Handle))
39+
40+
l.Debug("my_fake_debug_message")
41+
l.Log("my_fake_debug_message", logger.DebugLevel)
42+
43+
entries := m.GetEntries()
44+
assert.Len(t, entries, 2)
45+
46+
entry1 := entries[0]
47+
assert.Contains(t, (*entry1.Context)["file"].Value, "caller_test.go")
48+
49+
entry2 := entries[1]
50+
assert.Contains(t, (*entry2.Context)["file"].Value, "caller_test.go")
51+
52+
assert.Equal(t, (*entry1.Context)["file"].Value, (*entry2.Context)["file"].Value, "This 2 logs must have the same context file value")
53+
}

0 commit comments

Comments
 (0)