Skip to content

Commit

Permalink
Add optional logging of rate-limiting headers
Browse files Browse the repository at this point in the history
* Disabled by default
* Fix #77.
  • Loading branch information
ChimeraCoder committed Mar 30, 2015
1 parent c68538a commit bdb4b76
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
37 changes: 36 additions & 1 deletion log.go
@@ -1,6 +1,12 @@
package anaconda

// Logger interface to allow us to log
import (
"log"
"os"
)

// The Logger interface provides optional logging ability for the streaming API.
// It can also be used to log the rate limiting headers if desired.
type Logger interface {
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Expand Down Expand Up @@ -54,3 +60,32 @@ func (_ silentLogger) Info(_ ...interface{}) {}
func (_ silentLogger) Infof(_ string, _ ...interface{}) {}
func (_ silentLogger) Debug(_ ...interface{}) {}
func (_ silentLogger) Debugf(format string, _ ...interface{}) {}

// BasicLogger is the equivalent of using log from the standard
// library to print to STDERR
var BasicLogger Logger

type basicLogger struct {
log *log.Logger //func New(out io.Writer, prefix string, flag int) *Logger
}

func init() {
BasicLogger = &basicLogger{log: log.New(os.Stderr, log.Prefix(), log.LstdFlags)}
}

func (l basicLogger) Fatal(items ...interface{}) { l.log.Fatal(items) }
func (l basicLogger) Fatalf(s string, items ...interface{}) { l.log.Fatalf(s, items) }
func (l basicLogger) Panic(items ...interface{}) { l.log.Panic(items) }
func (l basicLogger) Panicf(s string, items ...interface{}) { l.log.Panicf(s, items) }
func (l basicLogger) Critical(items ...interface{}) { l.log.Print(items) }
func (l basicLogger) Criticalf(s string, items ...interface{}) { l.log.Printf(s, items) }
func (l basicLogger) Error(items ...interface{}) { l.log.Print(items) }
func (l basicLogger) Errorf(s string, items ...interface{}) { l.log.Printf(s, items) }
func (l basicLogger) Warning(items ...interface{}) { l.log.Print(items) }
func (l basicLogger) Warningf(s string, items ...interface{}) { l.log.Printf(s, items) }
func (l basicLogger) Notice(items ...interface{}) { l.log.Print(items) }
func (l basicLogger) Noticef(s string, items ...interface{}) { l.log.Printf(s, items) }
func (l basicLogger) Info(items ...interface{}) { l.log.Print(items) }
func (l basicLogger) Infof(s string, items ...interface{}) { l.log.Printf(s, items) }
func (l basicLogger) Debug(items ...interface{}) { l.log.Print(items) }
func (l basicLogger) Debugf(s string, items ...interface{}) { l.log.Printf(s, items) }
6 changes: 4 additions & 2 deletions streaming.go
Expand Up @@ -121,6 +121,9 @@ type TooManyFollow struct {

// TODO: Site Stream messages. I cant test.

// TODO: May be we could pass it a Logger interface to allow the
// stream to log in the right place ?

// Stream allows you to stream using one of the
// PublicStream* or UserStream api methods
//
Expand All @@ -139,8 +142,7 @@ type TooManyFollow struct {
//
// When finished you can call stream.Close() to terminate remote connection.
//
// May be we could pass it a Logger interface to allow the
// stream to log in the right place ?

type Stream struct {
api TwitterApi
C chan interface{}
Expand Down
3 changes: 3 additions & 0 deletions twitter.go
Expand Up @@ -73,6 +73,7 @@ type TwitterApi struct {
HttpClient *http.Client

// Currently used only for the streaming API
// and for checking rate-limiting headers
// Default logger is silent
Log Logger
}
Expand Down Expand Up @@ -249,6 +250,8 @@ func (c *TwitterApi) throttledQuery() {
if err != nil {
if apiErr, ok := err.(*ApiError); ok {
if isRateLimitError, nextWindow := apiErr.RateLimitCheck(); isRateLimitError && !c.returnRateLimitError {
c.Log.Info(apiErr.Error())

// If this is a rate-limiting error, re-add the job to the queue
// TODO it really should preserve order
go func() {
Expand Down

0 comments on commit bdb4b76

Please sign in to comment.