Skip to content

Commit

Permalink
Wrap Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Deeptiman committed Aug 1, 2021
1 parent bc35f0c commit f98a4d4
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 22 deletions.
16 changes: 11 additions & 5 deletions batch.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package batch

import (
log "github.com/sirupsen/logrus"
log "github.com/Deeptiman/go-batch/logger"
"time"
)

Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down
8 changes: 4 additions & 4 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package batch

import (
"context"
log "github.com/sirupsen/logrus"
log "github.com/Deeptiman/go-batch/logger"
"os"
"os/signal"
"sync"
Expand Down Expand Up @@ -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(),
}
}

Expand Down Expand Up @@ -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():
Expand All @@ -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()

Expand Down
110 changes: 110 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 6 additions & 8 deletions producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package batch
import (
"sync/atomic"
"time"

log "github.com/sirupsen/logrus"
log "github.com/Deeptiman/go-batch/logger"
)

var (
Expand Down Expand Up @@ -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(),
}
}

Expand All @@ -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
}
Expand Down
6 changes: 1 addition & 5 deletions supply.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
}
Expand Down

0 comments on commit f98a4d4

Please sign in to comment.