-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from owais/decouple-logrus
Decoupled logrus and added a logger interface
- Loading branch information
Showing
7 changed files
with
177 additions
and
30 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package producer | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
) | ||
|
||
// Logger represents a simple interface used by kinesis-producer to handle logging | ||
type Logger interface { | ||
Info(msg string, values ...LogValue) | ||
Error(msg string, err error, values ...LogValue) | ||
} | ||
|
||
// LogValue represents a key:value pair used by the Logger interface | ||
type LogValue struct { | ||
Name string | ||
Value interface{} | ||
} | ||
|
||
func (v LogValue) String() string { | ||
return fmt.Sprintf(" %s=%s", v.Name, v.Value) | ||
} | ||
|
||
// StdLogger implements the Logger interface using standard library loggers | ||
type StdLogger struct { | ||
Logger *log.Logger | ||
} | ||
|
||
// Info prints log message | ||
func (l *StdLogger) Info(msg string, values ...LogValue) { | ||
l.Logger.Print(msg, l.valuesToString(values...)) | ||
} | ||
|
||
// Error prints log message | ||
func (l *StdLogger) Error(msg string, err error, values ...LogValue) { | ||
l.Logger.Print(msg, l.valuesToString(values...), err) | ||
} | ||
|
||
func (l *StdLogger) valuesToString(values ...LogValue) string { | ||
parts := make([]string, len(values)) | ||
for i, v := range values { | ||
parts[i] = fmt.Sprint(v) | ||
} | ||
return strings.Join(parts, ", ") | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package kplogrus | ||
|
||
import ( | ||
producer "github.com/a8m/kinesis-producer" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Logger implements a logurs.Logger logger for kinesis-producer | ||
type Logger struct { | ||
Logger *logrus.Logger | ||
} | ||
|
||
// Info logs a message | ||
func (l *Logger) Info(msg string, args ...producer.LogValue) { | ||
l.Logger.WithFields(l.valuesToFields(args...)).Info(msg) | ||
} | ||
|
||
// Error logs an error | ||
func (l *Logger) Error(msg string, err error, args ...producer.LogValue) { | ||
l.Logger.WithError(err).WithFields(l.valuesToFields(args...)).Error(msg) | ||
} | ||
|
||
func (l *Logger) valuesToFields(values ...producer.LogValue) logrus.Fields { | ||
fields := logrus.Fields{} | ||
for _, v := range values { | ||
fields[v.Name] = v.Value | ||
} | ||
return fields | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package kpzap | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
|
||
producer "github.com/a8m/kinesis-producer" | ||
) | ||
|
||
// Logger implements a zap.Logger logger for kinesis-producer | ||
type Logger struct { | ||
Logger *zap.Logger | ||
} | ||
|
||
// Info logs a message | ||
func (l *Logger) Info(msg string, values ...producer.LogValue) { | ||
l.Logger.Info(msg, l.valuesToFields(values)...) | ||
} | ||
|
||
// Error logs an error | ||
func (l *Logger) Error(msg string, err error, values ...producer.LogValue) { | ||
fields := l.valuesToFields(values) | ||
fields = append(fields, zap.Error(err)) | ||
l.Logger.Info(msg, fields...) | ||
} | ||
|
||
func (l *Logger) valuesToFields(values []producer.LogValue) []zap.Field { | ||
fields := make([]zap.Field, len(values)) | ||
for i, v := range values { | ||
fields[i] = zap.Any(v.Name, v.Value) | ||
} | ||
return fields | ||
} |
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