From 62dd5a0023dc25365e30595f77e38e2a3d61dd38 Mon Sep 17 00:00:00 2001 From: David Arroyo Date: Sat, 30 Jul 2016 19:46:45 -0400 Subject: [PATCH] Add log.Logger's Output method to Logger&Entry The standard library's *log.Logger type has the method Output(calldepth int, s string) error Because it is so minimal, many libraries chose to target it as an interface, which users could implement to receive additional log messages: type Logger { Output(int, string) error } While it is not overwhelmingly used, it is out there. Implementing this interface allows us to use logrus.Logger and logrus.Entry types to receive log messages from such libraries. I chose to log such messages at the "Info" level, copying the Print* methods. The "calldepth" argument is ignored. --- entry.go | 5 +++++ logger.go | 5 +++++ logrus_test.go | 9 +++++++++ 3 files changed, 19 insertions(+) diff --git a/entry.go b/entry.go index 54bfc57d7..92584aa2d 100644 --- a/entry.go +++ b/entry.go @@ -254,6 +254,11 @@ func (entry *Entry) Panicln(args ...interface{}) { } } +func (entry *Entry) Output(calldepth int, s string) error { + entry.Infoln(s) + return nil +} + // Sprintlnn => Sprint no newline. This is to get the behavior of how // fmt.Sprintln where spaces are always added between operands, regardless of // their type. Instead of vendoring the Sprintln implementation to spare a diff --git a/logger.go b/logger.go index 9052a8065..0ac2f67f5 100644 --- a/logger.go +++ b/logger.go @@ -210,3 +210,8 @@ func (logger *Logger) Panicln(args ...interface{}) { NewEntry(logger).Panicln(args...) } } + +func (logger *Logger) Output(calldepth int, s string) error { + NewEntry(logger).Println(s) + return nil +} diff --git a/logrus_test.go b/logrus_test.go index bfc478055..5d3915da3 100644 --- a/logrus_test.go +++ b/logrus_test.go @@ -83,6 +83,15 @@ func TestWarn(t *testing.T) { }) } +func TestOutput(t *testing.T) { + LogAndAssertJSON(t, func(log *Logger) { + log.Output(2, "test") + }, func(fields Fields) { + assert.Equal(t, fields["msg"], "test") + assert.Equal(t, fields["level"], "info") + }) +} + func TestInfolnShouldAddSpacesBetweenStrings(t *testing.T) { LogAndAssertJSON(t, func(log *Logger) { log.Infoln("test", "test")