diff options
| author | Felix Hanley <felix@userspace.com.au> | 2020-05-20 01:54:04 +0000 |
|---|---|---|
| committer | Felix Hanley <felix@userspace.com.au> | 2020-05-20 01:54:04 +0000 |
| commit | effdb02ac52c75468bcd0840f5e2ab8bf140b944 (patch) | |
| tree | 96e7d7756be8c15ab742138481afeff9d135909c | |
| parent | 21b31fdacf93bd5bb54937fb7c25a73b61205344 (diff) | |
| download | logger-effdb02ac52c75468bcd0840f5e2ab8bf140b944.tar.gz logger-effdb02ac52c75468bcd0840f5e2ab8bf140b944.tar.bz2 | |
| -rw-r--r-- | example_test.go | 2 | ||||
| -rw-r--r-- | logger.go | 16 | ||||
| -rw-r--r-- | logger_test.go | 1 | ||||
| -rw-r--r-- | std.go | 16 |
4 files changed, 31 insertions, 4 deletions
diff --git a/example_test.go b/example_test.go index d4af463..60f32f7 100644 --- a/example_test.go +++ b/example_test.go @@ -15,7 +15,7 @@ func ExampleDebug() { Writer(keyValue), ) log.Debug("unable to do anything") - // Output: app: unable to do anything + // Output: app: unable to do anything source=example_test.go:17 } func Example_structure() { @@ -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 } diff --git a/logger_test.go b/logger_test.go index f517d06..e1820e8 100644 --- a/logger_test.go +++ b/logger_test.go @@ -26,6 +26,7 @@ func TestLog(t *testing.T) { expected string }{ {[]interface{}{"one", "two"}, "one two"}, + {[]interface{}{"one", 9}, "one 9"}, } var buf bytes.Buffer kv, err := kv.New(kv.SetOutput(&buf)) @@ -10,8 +10,11 @@ func init() { std, _ = New() } -// Info logs an information message. -func Info(args ...interface{}) { std.Info(args...) } +// Log a message. +func Log(args ...interface{}) { std.Log(args...) } + +// Info is an alias for Log. +func Info(args ...interface{}) *Logger { return std.Log(args...) } // Debug logs a debug message. func Debug(args ...interface{}) { std.Debug(args...) } @@ -23,8 +26,17 @@ func IsDebug() bool { return std.IsDebug() } // Field enables changing the default fields for a logger instance. func Field(k string, v interface{}) *Logger { return std.Field(k, v) } +// Fields enables setting or changing the default fields for a logger instance. +func Fields(args ...interface{}) *Logger { return std.Fields(args...) } + +// FieldMap enables setting or changing the default fields for a logger instance. +func FieldMap(f map[string]interface{}) *Logger { return std.FieldMap(f) } + // Named creates a new instance of a logger with a new name. func Named(n string) *Logger { return std.Named(n) } // SetWriter sets the writer for the default logger. func SetWriter(w message.Writer) { std.writers = []message.Writer{w} } + +// WithSource annotates the log message with the source that called logger. +func WithSource() *Logger { return std.WithSource() } |
