Skip to content

Commit 95ee516

Browse files
committed
Add in debug-mode file:line markers to logs.
This will help with debugging when trying to look for the source of errors.
1 parent b2e8e5a commit 95ee516

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

log/log.go

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@
3131
// limitations under the License.
3232
package log
3333

34+
import (
35+
"fmt"
36+
"runtime"
37+
)
38+
3439
var (
3540
internalLog *logMux
41+
debugMode bool
3642
)
3743

3844
/*
@@ -61,24 +67,57 @@ so that we have a very simple API to get started with.
6167
*/
6268

6369
func Info(message string, v ...interface{}) {
64-
internalLog.Info(message, v...)
70+
// Prepend the log message with information about the calling function.
71+
var fileMessage string
72+
if debugMode {
73+
_, f, l, _ := runtime.Caller(1)
74+
fileMessage = fmt.Sprintf("(%s:%d) %s", f, l, message)
75+
} else {
76+
fileMessage = message
77+
}
78+
internalLog.Info(fileMessage, v...)
6579
}
6680

6781
func Warning(message string, v ...interface{}) {
68-
internalLog.Warning(message, v...)
82+
// Prepend the log message with information about the calling function.
83+
var fileMessage string
84+
if debugMode {
85+
_, f, l, _ := runtime.Caller(1)
86+
fileMessage = fmt.Sprintf("(%s:%d) %s", f, l, message)
87+
} else {
88+
fileMessage = message
89+
}
90+
internalLog.Warning(fileMessage, v...)
6991
}
7092

7193
func Error(message string, v ...interface{}) {
72-
internalLog.Error(message, v...)
94+
// Prepend the log message with information about the calling function.
95+
var fileMessage string
96+
if debugMode {
97+
_, f, l, _ := runtime.Caller(1)
98+
fileMessage = fmt.Sprintf("(%s:%d) %s", f, l, message)
99+
} else {
100+
fileMessage = message
101+
}
102+
internalLog.Error(fileMessage, v...)
73103
}
74104

75105
func Debug(message string, v ...interface{}) {
76-
internalLog.Debug(message, v...)
106+
// Prepend the log message with information about the calling function.
107+
var fileMessage string
108+
if debugMode {
109+
_, f, l, _ := runtime.Caller(1)
110+
fileMessage = fmt.Sprintf("(%s:%d) %s", f, l, message)
111+
} else {
112+
fileMessage = message
113+
}
114+
internalLog.Debug(fileMessage, v...)
77115
}
78116

79117
/*
80118
DebugMode sets the debug mode option for this built-in logger.
81119
*/
82120
func DebugMode(status bool) {
83121
internalLog.DebugMode(status)
122+
debugMode = status
84123
}

0 commit comments

Comments
 (0)