Use no-op Info logging by default

This commit is contained in:
Harlow Ward 2016-05-01 12:40:30 -07:00
parent afae1bea36
commit a12c15a191
3 changed files with 25 additions and 18 deletions

View file

@ -39,23 +39,32 @@ func main() {
### Logging ### Logging
[Apex Log](https://medium.com/@tjholowaychuk/apex-log-e8d9627f4a9a#.5x1uo1767) is used to log Info and Errors from within the libarary. The default handler is "text" and can be overrideen with other [LogHandlers](https://github.com/apex/log/tree/master/_examples) from the the Config struct: [Apex Log](https://medium.com/@tjholowaychuk/apex-log-e8d9627f4a9a#.5x1uo1767) is used for logging Info. The default handler is "discard" which is a no-op logging handler (i.e. no logs produced).
If you'd like to have the libaray produce logs the default can be overridden with other [Log Handlers](https://github.com/apex/log/tree/master/_examples). For example using the "text" log handler:
```go ```go
import( import(
"github.com/apex/log" "github.com/apex/log/handlers/text"
"github.com/apex/log/handlers/json"
) )
func main() { func main() {
// ... // ...
cfg := connector.Config{ cfg := connector.Config{
LogHandler: json.New(os.Stderr), LogHandler: text.New(os.Stderr),
} }
} }
``` ```
Which will producde the following logs:
```
INFO[0000] processing app=test shard=shardId-000000000000 stream=test
INFO[0008] emitted app=test count=500 shard=shardId-000000000000 stream=test
INFO[0012] emitted app=test count=500 shard=shardId-000000000000 stream=test
```
### Installation ### Installation
Get the package source: Get the package source:

View file

@ -1,10 +1,10 @@
package connector package connector
import ( import (
"os" "log"
"github.com/apex/log" apexlog "github.com/apex/log"
"github.com/apex/log/handlers/text" "github.com/apex/log/handlers/discard"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kinesis" "github.com/aws/aws-sdk-go/service/kinesis"
@ -17,7 +17,7 @@ const (
// NewConsumer creates a new consumer with initialied kinesis connection // NewConsumer creates a new consumer with initialied kinesis connection
func NewConsumer(appName, streamName string, cfg Config) *Consumer { func NewConsumer(appName, streamName string, cfg Config) *Consumer {
if cfg.LogHandler == nil { if cfg.LogHandler == nil {
cfg.LogHandler = text.New(os.Stderr) cfg.LogHandler = discard.New()
} }
if cfg.MaxBatchCount == 0 { if cfg.MaxBatchCount == 0 {
@ -48,7 +48,7 @@ type Consumer struct {
// Start takes a handler and then loops over each of the shards // Start takes a handler and then loops over each of the shards
// processing each one with the handler. // processing each one with the handler.
func (c *Consumer) Start(handler Handler) { func (c *Consumer) Start(handler Handler) {
log.SetHandler(c.cfg.LogHandler) apexlog.SetHandler(c.cfg.LogHandler)
resp, err := c.svc.DescribeStream( resp, err := c.svc.DescribeStream(
&kinesis.DescribeStreamInput{ &kinesis.DescribeStreamInput{
@ -57,8 +57,7 @@ func (c *Consumer) Start(handler Handler) {
) )
if err != nil { if err != nil {
log.WithError(err).Error("DescribeStream") log.Fatalf("Error DescribeStream %v", err)
os.Exit(1)
} }
for _, shard := range resp.StreamDescription.Shards { for _, shard := range resp.StreamDescription.Shards {
@ -67,7 +66,7 @@ func (c *Consumer) Start(handler Handler) {
} }
func (c *Consumer) handlerLoop(shardID string, handler Handler) { func (c *Consumer) handlerLoop(shardID string, handler Handler) {
ctx := log.WithFields(log.Fields{ ctx := apexlog.WithFields(apexlog.Fields{
"app": c.appName, "app": c.appName,
"stream": c.streamName, "stream": c.streamName,
"shard": shardID, "shard": shardID,
@ -96,8 +95,7 @@ func (c *Consumer) handlerLoop(shardID string, handler Handler) {
resp, err := c.svc.GetShardIterator(params) resp, err := c.svc.GetShardIterator(params)
if err != nil { if err != nil {
ctx.WithError(err).Error("getShardIterator") log.Fatalf("Error GetShardIterator %v", err)
os.Exit(1)
} }
shardIterator := resp.ShardIterator shardIterator := resp.ShardIterator
@ -111,8 +109,7 @@ func (c *Consumer) handlerLoop(shardID string, handler Handler) {
) )
if err != nil { if err != nil {
ctx.WithError(err).Error("getRecords") log.Fatalf("Error GetRecords %v", err)
os.Exit(1)
} }
if len(resp.Records) > 0 { if len(resp.Records) > 0 {
@ -127,8 +124,7 @@ func (c *Consumer) handlerLoop(shardID string, handler Handler) {
} }
} }
} else if resp.NextShardIterator == aws.String("") || shardIterator == resp.NextShardIterator { } else if resp.NextShardIterator == aws.String("") || shardIterator == resp.NextShardIterator {
ctx.Error("nextShardIterator") log.Fatalf("Error NextShardIterator")
os.Exit(1)
} }
shardIterator = resp.NextShardIterator shardIterator = resp.NextShardIterator

View file

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/apex/log/handlers/text"
"github.com/harlow/kinesis-connectors" "github.com/harlow/kinesis-connectors"
"github.com/harlow/kinesis-connectors/emitter/s3" "github.com/harlow/kinesis-connectors/emitter/s3"
) )
@ -26,6 +27,7 @@ func main() {
cfg := connector.Config{ cfg := connector.Config{
MaxBatchCount: 500, MaxBatchCount: 500,
LogHandler: text.New(os.Stderr),
} }
c := connector.NewConsumer(*app, *stream, cfg) c := connector.NewConsumer(*app, *stream, cfg)