From bde3e96ad9750c505449ced2bfd43ecdbbfd91fb Mon Sep 17 00:00:00 2001 From: Harlow Ward Date: Fri, 22 May 2015 23:38:06 -0700 Subject: [PATCH] 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 --- README.md | 10 ++-------- default_logger.go | 21 ++++++++------------- discard_logger.go | 15 +++++++++++++++ logger.go | 6 ++++++ 4 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 discard_logger.go diff --git a/README.md b/README.md index c461443..8f66c43 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,6 @@ package main import ( "fmt" - "log" - "net/http" "os" "code.google.com/p/gcfg" @@ -75,7 +73,7 @@ func newS3Pipeline(cfg Config) *connector.Pipeline { AppName: cfg.Pipeline.Name, StreamName: cfg.Kinesis.StreamName, } - e := &connector.S3ManifestEmitter{ + e := &connector.S3Emitter{ S3Bucket: cfg.S3.BucketName, } return &connector.Pipeline{ @@ -83,7 +81,6 @@ func newS3Pipeline(cfg Config) *connector.Pipeline { Checkpoint: c, Emitter: e, Filter: f, - Logger: log.New(os.Stdout, "KINESIS: ", log.Ldate|log.Ltime|log.Lshortfile), StreamName: cfg.Kinesis.StreamName, Transformer: t, } @@ -94,19 +91,16 @@ func main() { var cfg Config err := gcfg.ReadFileInto(&cfg, "pipeline.cfg") - // Set up kinesis client + // Set up kinesis client and stream accessKey := os.Getenv("AWS_ACCESS_KEY") secretKey := os.Getenv("AWS_SECRET_KEY") ksis := kinesis.New(accessKey, secretKey, kinesis.Region{}) - - // Create and wait for streams connector.CreateStream(ksis, cfg.Kinesis.StreamName, cfg.Kinesis.ShardCount) // Fetch stream info args := kinesis.NewArgs() args.Add("StreamName", cfg.Kinesis.StreamName) streamInfo, err := ksis.DescribeStream(args) - if err != nil { fmt.Printf("Unable to connect to %s stream. Aborting.", cfg.Kinesis.StreamName) return diff --git a/default_logger.go b/default_logger.go index 2679cc4..1825723 100644 --- a/default_logger.go +++ b/default_logger.go @@ -2,25 +2,20 @@ package connector import "log" -// the default implementation for a logger -// for this package. -type DefaultLogger struct { -} +// DefaultLogger is an implementation for std lib logger. +type DefaultLogger struct{} +// Fatalf is equivalent to Printf() followed by a call to os.Exit(1). func (l *DefaultLogger) Fatalf(format string, v ...interface{}) { 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{}) { log.Printf(format, v...) } -// make sure that there is a default logger instance -// initialized, so that we don't end up with panics +// Make sure that there is a default logger instance initialized, so +// that we don't end up with panics. 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 -} diff --git a/discard_logger.go b/discard_logger.go new file mode 100644 index 0000000..1f39f0d --- /dev/null +++ b/discard_logger.go @@ -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{}) {} diff --git a/logger.go b/logger.go index 916807f..103f023 100644 --- a/logger.go +++ b/logger.go @@ -6,3 +6,9 @@ type Logger interface { Fatalf(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 +}