diff --git a/batch.go b/batch.go index a62646a..53e7418 100644 --- a/batch.go +++ b/batch.go @@ -1,7 +1,7 @@ package batch import ( - log "github.com/sirupsen/logrus" + log "github.com/Deeptiman/go-batch/logger" "time" ) @@ -21,7 +21,7 @@ type Batch struct { Islocked bool Producer *BatchProducer Consumer *BatchConsumer - Log *log.Logger + Log *log.Logger } // NewBatch creates a new Batch object with BatchProducer & BatchConsumer. The BatchOptions @@ -30,9 +30,9 @@ func NewBatch(opts ...BatchOptions) *Batch { b := &Batch{ Item: make(chan interface{}), - Log: log.New(), + Log: log.NewLogger(), } - + c := NewBatchConsumer() p := NewBatchProducer(c.ConsumerFunc) @@ -95,6 +95,11 @@ func (b *Batch) ReadItems() { } } +// SetLogLevel [Info:Debug] +func (b *Batch) SetDebugLogLevel() { + b.Log.SetLogLevel(log.Debug) +} + // StopProducer to exit the Producer line. func (b *Batch) StopProducer() { b.Producer.Quit <- true @@ -107,7 +112,8 @@ func (b *Batch) Stop() { // Close is the exit function to terminate the batch processing. func (b *Batch) Close() { - b.Log.WithFields(log.Fields{"Remaining Items": len(items)}).Warn("CheckRemainingItems") + //b.Log.WithFields(log.Fields{"Remaining Items": len(items)}).Warn("CheckRemainingItems") + b.Log.Infoln("CheckRemainingItems", "Remaining=", len(items)) done := make(chan bool) diff --git a/consumer.go b/consumer.go index 7236ec7..487ccbc 100644 --- a/consumer.go +++ b/consumer.go @@ -2,7 +2,7 @@ package batch import ( "context" - log "github.com/sirupsen/logrus" + log "github.com/Deeptiman/go-batch/logger" "os" "os/signal" "sync" @@ -61,7 +61,7 @@ func NewBatchConsumer() *BatchConsumer { Workerline: &sync.WaitGroup{}, TerminateCh: make(chan os.Signal, 1), Quit: make(chan bool, 1), - Log: log.New(), + Log: log.NewLogger(), } } @@ -123,7 +123,7 @@ func (c *BatchConsumer) ConsumerBatch(ctx context.Context) { for { select { case batchItems := <-c.ConsumerCh: - c.Log.WithFields(log.Fields{"Receive Batch Items": len(batchItems)}).Info("BatchConsumer") + c.Log.Infoln("BatchConsumer", "Receive Batch Items:", len(batchItems)) c.BatchWorkerCh <- batchItems case <-ctx.Done(): @@ -145,7 +145,7 @@ func (c *BatchConsumer) WorkerFunc(index int) { for batch := range c.BatchWorkerCh { - c.Log.WithFields(log.Fields{"Worker": index, "Batch": len(batch)}).Warn("Workerline") + c.Log.Debugln("Workerline", "Worker=", index, "Batch=", len(batch)) go c.GetBatchSupply() diff --git a/logger/logger.go b/logger/logger.go new file mode 100644 index 0000000..918ec41 --- /dev/null +++ b/logger/logger.go @@ -0,0 +1,110 @@ +package logger + +import ( + "github.com/sirupsen/logrus" +) + +type LogLevel int + +const ( + Info LogLevel = iota + Debug +) + +type Logger struct { + log *logrus.Logger +} + +func NewLogger() *Logger { + log := logrus.New() + + log.SetFormatter(&logrus.TextFormatter{ + DisableColors: false, + ForceColors: true, + DisableTimestamp: true, + TimestampFormat: "2006-01-02 15:04:05", + FullTimestamp: true, + }) + + return &Logger{ + log: log, + } +} + +func (l *Logger) SetLogLevel(level LogLevel) { + if level == Debug { + l.log.Level = logrus.DebugLevel + } +} + +func (l *Logger) Trace(format string, args ...interface{}) { + +} + +func (l *Logger) Debug(args ...interface{}) { + l.log.Debug(args...) +} + +func (l *Logger) Debugf(format string, args ...interface{}) { + l.log.Debugf(format, args...) +} + +func (l *Logger) Debugln(args ...interface{}) { + l.log.Debugln(args...) +} + +func (l *Logger) Info(args ...interface{}) { + l.log.Info(args...) +} + +func (l *Logger) Infof(format string, args ...interface{}) { + l.log.Infof(format, args...) +} + +func (l *Logger) Infoln(args ...interface{}) { + l.log.Infoln(args...) +} + +func (l *Logger) Warn(format string, args ...interface{}) { + l.log.Warn(args...) +} + +func (l *Logger) Warnf(format string, args ...interface{}) { + l.log.Warnf(format, args...) +} + +func (l *Logger) Warnln(format string, args ...interface{}) { + l.log.Warnln(args...) +} + +func (l *Logger) Fatal(format string, args ...interface{}) { + l.log.Fatal(args...) +} + +func (l *Logger) Fatalf(format string, args ...interface{}) { + l.log.Fatalf(format, args...) +} + +func (l *Logger) Fatalln(format string, args ...interface{}) { + l.log.Fatalln(args...) +} + +func (l *Logger) Error(format string, args ...interface{}) { + l.log.Error(args...) +} + +func (l *Logger) Errorf(format string, args ...interface{}) { + l.log.Errorf(format, args...) +} + +func (l *Logger) Errorln(format string, args ...interface{}) { + l.log.Errorln(args...) +} + +func (l *Logger) WithField(key string, value interface{}) { + l.log.WithField(key, value) +} + +func (l *Logger) WithFields(fields logrus.Fields) { + l.log.WithFields(fields) +} diff --git a/producer.go b/producer.go index 497f3f7..18695a4 100644 --- a/producer.go +++ b/producer.go @@ -3,8 +3,7 @@ package batch import ( "sync/atomic" "time" - - log "github.com/sirupsen/logrus" + log "github.com/Deeptiman/go-batch/logger" ) var ( @@ -53,7 +52,7 @@ func NewBatchProducer(callBackFn ConsumerFunc, opts ...BatchOptions) *BatchProdu MaxWait: DefaultMaxWait, BatchNo: DefaultBatchNo, Quit: make(chan bool), - Log: log.New(), + Log: log.NewLogger(), } } @@ -73,19 +72,18 @@ func (p *BatchProducer) WatchProducer() { case item := <-p.Watcher: item.BatchNo = int(p.getBatchNo()) - p.Log.WithFields(log.Fields{"Id": item.Id, "Batch_Break": item.Id / int(p.MaxItems), "BatchNo": item.BatchNo, "Item": item.Item}).Info("BatchProducer") - + p.Log.Debugln("BatchProducer", "Id=", item.Id, "Batch Break=", item.Id / int(p.MaxItems), "BatchNo=",item.BatchNo, "Item=", item.Item) + items = append(items, *item) if (item.Id / int(p.MaxItems)) == item.BatchNo { - p.Log.WithFields(log.Fields{"Item Size": len(items), "MaxItems": p.MaxItems}).Warn("BatchReady") + p.Log.Infoln("BatchReady", "BatchNo=", item.BatchNo) items = p.releaseBatch(items) p.createBatchNo() } case <-time.After(p.MaxWait): - p.Log.WithFields(log.Fields{"Items": len(items)}).Warn("MaxWait") - + p.Log.Infoln("MaxWait", "Items=", len(items)) if len(items) == 0 { return } diff --git a/supply.go b/supply.go index 4732198..0083619 100644 --- a/supply.go +++ b/supply.go @@ -1,9 +1,5 @@ package batch -import ( - log "github.com/sirupsen/logrus" -) - // GetBatchSupply request the WorkerChannel for the released []BatchItems. The BatchSupplyChannel // works as a bidirectional channel to request/response for the final []BatchItems product. // The ClientSupplyChannel will send the []BatchItems to the client. @@ -17,7 +13,7 @@ func (c *BatchConsumer) GetBatchSupply() { select { case supply := <-supplyCh: - c.Log.WithFields(log.Fields{"Supply": len(supply)}).Warn("BatchSupply") + c.Log.Debugln("BatchSupply", "Supply=", len(supply)) c.Supply.ClientSupplyCh <- supply }