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
[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
import(
"github.com/apex/log"
"github.com/apex/log/handlers/json"
"github.com/apex/log/handlers/text"
)
func main() {
// ...
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
Get the package source:

View file

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

View file

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