-
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.
Decoupled logrus and added a logger interface
- `producer.Config` now takes a producer.Logger interface instead of a Logrus instance. - If not logger is provided to config, producer uses a standard library logger by default (`producer.StandardLogger`) - `loggers` sub package ships implementations for zap and logrus.
- Loading branch information
Showing
8 changed files
with
163 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,21 @@ | ||
package producer | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// Logger represents a simple interface used by kinesis-producer to handle logging | ||
type Logger interface { | ||
Info(msg string, values ...Value) | ||
Error(msg string, err error, values ...Value) | ||
} | ||
|
||
// Value represents a key:value pair used by the Logger interface | ||
type Value struct { | ||
Name string | ||
Value interface{} | ||
} | ||
|
||
func (v Value) String() string { | ||
return fmt.Sprintf(" %s=%s", v.Name, v.Value) | ||
} |
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 loggers | ||
|
||
import ( | ||
producer "github.com/a8m/kinesis-producer" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// Logrus implements a logurs.Logger for kinesis-producer | ||
type Logrus struct { | ||
Logger *logrus.Logger | ||
} | ||
|
||
// Info logs a message | ||
func (l *Logrus) Info(msg string, args ...producer.Value) { | ||
l.Logger.WithFields(l.valuesToFields(args...)).Info(msg) | ||
} | ||
|
||
// Error logs an error | ||
func (l *Logrus) Error(msg string, err error, args ...producer.Value) { | ||
l.Logger.WithError(err).WithFields(l.valuesToFields(args...)).Error(msg) | ||
} | ||
|
||
func (l *Logrus) valuesToFields(values ...producer.Value) 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 loggers | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
|
||
producer "github.com/a8m/kinesis-producer" | ||
) | ||
|
||
// Zap implements a logurs.Logger for kinesis-producer | ||
type Zap struct { | ||
Logger *zap.Logger | ||
} | ||
|
||
// Info logs a message | ||
func (l *Zap) Info(msg string, values ...producer.Value) { | ||
l.Logger.Info(msg, l.valuesToFields(values)...) | ||
} | ||
|
||
// Error logs an error | ||
func (l *Zap) Error(msg string, err error, values ...producer.Value) { | ||
fields := l.valuesToFields(values) | ||
fields = append(fields, zap.Error(err)) | ||
l.Logger.Info(msg, fields...) | ||
} | ||
|
||
func (l *Zap) valuesToFields(values []producer.Value) []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
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,30 @@ | ||
package producer | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
) | ||
|
||
// StandardLogger implements the Logger interface using standard library loggers | ||
type StandardLogger struct { | ||
Logger *log.Logger | ||
} | ||
|
||
// Info prints log message to screen | ||
func (l *StandardLogger) Info(msg string, values ...Value) { | ||
l.Logger.Print(msg, l.valuesToString(values...)) | ||
} | ||
|
||
// Error prints log message to screen | ||
func (l *StandardLogger) Error(msg string, err error, values ...Value) { | ||
l.Logger.Print(msg, l.valuesToString(values...), err) | ||
} | ||
|
||
func (l *StandardLogger) valuesToString(values ...Value) string { | ||
parts := make([]string, len(values)) | ||
for i, v := range values { | ||
parts[i] = fmt.Sprint(v) | ||
} | ||
return strings.Join(parts, ", ") | ||
} |