/-
Created
October 8, 2015 03:32
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/entry.go b/entry.go | |
index 9ae900b..7843f36 100644 | |
--- a/entry.go | |
+++ b/entry.go | |
@@ -78,6 +78,19 @@ func (entry *Entry) WithFields(fields Fields) *Entry { | |
return &Entry{Logger: entry.Logger, Data: data} | |
} | |
+func (entry *Entry) WithContext() *Entry { | |
+ return entry.WithField("caller", context()) | |
+} | |
+ | |
+// Captures where the log call came from and formats it for output | |
+func context() string { | |
+ if _, file, line, ok := runtime.Caller(2); ok { | |
+ return strings.Join([]string{filepath.Base(file), strconv.Itoa(line)}, ":") | |
+ } | |
+ // not sure what the convention should be here | |
+ return "unavailable" | |
+} | |
+ | |
// This function is not declared with a pointer value because otherwise | |
// race conditions will occur when using multiple goroutines | |
func (entry Entry) log(level Level, msg string) { | |
diff --git a/logger.go b/logger.go | |
index fd9804c..1380f6b 100644 | |
--- a/logger.go | |
+++ b/logger.go | |
@@ -64,6 +64,10 @@ func (logger *Logger) WithFields(fields Fields) *Entry { | |
return NewEntry(logger).WithFields(fields) | |
} | |
+func (logger *Logger) WithContext() *Entry { | |
+ return NewEntry(logger).WithField("caller", context()) | |
+} | |
+ | |
func (logger *Logger) Debugf(format string, args ...interface{}) { | |
if logger.Level >= DebugLevel { | |
NewEntry(logger).Debugf(format, args...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment