Add Discard Logger

For situations where we don't want any logs produced by the application.

* Remove references to Logger from README
* Add implementation of DiscardLogger
This commit is contained in:
Harlow Ward 2015-05-22 23:38:06 -07:00
parent ff1cff0293
commit bde3e96ad9
4 changed files with 31 additions and 21 deletions

View file

@ -42,8 +42,6 @@ package main
import ( import (
"fmt" "fmt"
"log"
"net/http"
"os" "os"
"code.google.com/p/gcfg" "code.google.com/p/gcfg"
@ -75,7 +73,7 @@ func newS3Pipeline(cfg Config) *connector.Pipeline {
AppName: cfg.Pipeline.Name, AppName: cfg.Pipeline.Name,
StreamName: cfg.Kinesis.StreamName, StreamName: cfg.Kinesis.StreamName,
} }
e := &connector.S3ManifestEmitter{ e := &connector.S3Emitter{
S3Bucket: cfg.S3.BucketName, S3Bucket: cfg.S3.BucketName,
} }
return &connector.Pipeline{ return &connector.Pipeline{
@ -83,7 +81,6 @@ func newS3Pipeline(cfg Config) *connector.Pipeline {
Checkpoint: c, Checkpoint: c,
Emitter: e, Emitter: e,
Filter: f, Filter: f,
Logger: log.New(os.Stdout, "KINESIS: ", log.Ldate|log.Ltime|log.Lshortfile),
StreamName: cfg.Kinesis.StreamName, StreamName: cfg.Kinesis.StreamName,
Transformer: t, Transformer: t,
} }
@ -94,19 +91,16 @@ func main() {
var cfg Config var cfg Config
err := gcfg.ReadFileInto(&cfg, "pipeline.cfg") err := gcfg.ReadFileInto(&cfg, "pipeline.cfg")
// Set up kinesis client // Set up kinesis client and stream
accessKey := os.Getenv("AWS_ACCESS_KEY") accessKey := os.Getenv("AWS_ACCESS_KEY")
secretKey := os.Getenv("AWS_SECRET_KEY") secretKey := os.Getenv("AWS_SECRET_KEY")
ksis := kinesis.New(accessKey, secretKey, kinesis.Region{}) ksis := kinesis.New(accessKey, secretKey, kinesis.Region{})
// Create and wait for streams
connector.CreateStream(ksis, cfg.Kinesis.StreamName, cfg.Kinesis.ShardCount) connector.CreateStream(ksis, cfg.Kinesis.StreamName, cfg.Kinesis.ShardCount)
// Fetch stream info // Fetch stream info
args := kinesis.NewArgs() args := kinesis.NewArgs()
args.Add("StreamName", cfg.Kinesis.StreamName) args.Add("StreamName", cfg.Kinesis.StreamName)
streamInfo, err := ksis.DescribeStream(args) streamInfo, err := ksis.DescribeStream(args)
if err != nil { if err != nil {
fmt.Printf("Unable to connect to %s stream. Aborting.", cfg.Kinesis.StreamName) fmt.Printf("Unable to connect to %s stream. Aborting.", cfg.Kinesis.StreamName)
return return

View file

@ -2,25 +2,20 @@ package connector
import "log" import "log"
// the default implementation for a logger // DefaultLogger is an implementation for std lib logger.
// for this package. type DefaultLogger struct{}
type DefaultLogger struct {
}
// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
func (l *DefaultLogger) Fatalf(format string, v ...interface{}) { func (l *DefaultLogger) Fatalf(format string, v ...interface{}) {
log.Fatalf(format, v...) log.Fatalf(format, v...)
} }
// Printf calls Output to print to the standard logger. Arguments are
// handled in the manner of fmt.Printf.
func (l *DefaultLogger) Printf(format string, v ...interface{}) { func (l *DefaultLogger) Printf(format string, v ...interface{}) {
log.Printf(format, v...) log.Printf(format, v...)
} }
// make sure that there is a default logger instance // Make sure that there is a default logger instance initialized, so
// initialized, so that we don't end up with panics // that we don't end up with panics.
var logger Logger = &DefaultLogger{} var logger Logger = &DefaultLogger{}
// expose the ability to change the logger so that
// external packages can control the logging for
// this package
func SetLogger(l Logger) {
logger = l
}

15
discard_logger.go Normal file
View file

@ -0,0 +1,15 @@
package connector
import "os"
// DiscardLogger is the an implementation of a Logger that does not
// send any output. It can be used in scenarios when logging is not desired.
type DiscardLogger struct{}
// Fatalf is equivalent to Printf() followed by a call to os.Exit(1).
func (l *DiscardLogger) Fatalf(format string, v ...interface{}) {
os.Exit(1)
}
// Printf is noop and does not have any output.
func (l *DiscardLogger) Printf(format string, v ...interface{}) {}

View file

@ -6,3 +6,9 @@ type Logger interface {
Fatalf(format string, v ...interface{}) Fatalf(format string, v ...interface{})
Printf(format string, v ...interface{}) Printf(format string, v ...interface{})
} }
// SetLogger adds the ability to change the logger so that external packages
// can control the logging for this package
func SetLogger(l Logger) {
logger = l
}