aboutsummaryrefslogtreecommitdiff
path: root/logger.go
diff options
context:
space:
mode:
Diffstat (limited to 'logger.go')
-rw-r--r--logger.go16
1 files changed, 15 insertions, 1 deletions
diff --git a/logger.go b/logger.go
index 9ac23c8..5cdc1a5 100644
--- a/logger.go
+++ b/logger.go
@@ -3,6 +3,7 @@ package logger
import (
"fmt"
"os"
+ "runtime"
"strings"
"sync"
"time"
@@ -90,11 +91,24 @@ func (l *Logger) Info(args ...interface{}) *Logger {
// Debug logs a debug message.
func (l *Logger) Debug(args ...interface{}) *Logger {
if l.debug {
- return l.log(args...)
+ return l.WithSource().log(args...)
}
return l
}
+// WithSource annotates the log message with the source that called logger.
+func (l *Logger) WithSource() *Logger {
+ _, file, line, ok := runtime.Caller(2)
+ if !ok {
+ file = "<???>"
+ line = 1
+ } else {
+ slash := strings.LastIndex(file, "/")
+ file = file[slash+1:]
+ }
+ return l.Field("source", fmt.Sprintf("%s:%d", file, line))
+}
+
// IsDebug determines the debug status for a logger instance.
// Use this to conditionally execute blocks of code depending on the log verbosity.
func (l *Logger) IsDebug() bool { return l.debug }