Skip to content

Commit f98a4d4

Browse files
committed
Wrap Logger
1 parent bc35f0c commit f98a4d4

File tree

5 files changed

+132
-22
lines changed

5 files changed

+132
-22
lines changed

batch.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package batch
22

33
import (
4-
log "github.com/sirupsen/logrus"
4+
log "github.com/Deeptiman/go-batch/logger"
55
"time"
66
)
77

@@ -21,7 +21,7 @@ type Batch struct {
2121
Islocked bool
2222
Producer *BatchProducer
2323
Consumer *BatchConsumer
24-
Log *log.Logger
24+
Log *log.Logger
2525
}
2626

2727
// NewBatch creates a new Batch object with BatchProducer & BatchConsumer. The BatchOptions
@@ -30,9 +30,9 @@ func NewBatch(opts ...BatchOptions) *Batch {
3030

3131
b := &Batch{
3232
Item: make(chan interface{}),
33-
Log: log.New(),
33+
Log: log.NewLogger(),
3434
}
35-
35+
3636
c := NewBatchConsumer()
3737

3838
p := NewBatchProducer(c.ConsumerFunc)
@@ -95,6 +95,11 @@ func (b *Batch) ReadItems() {
9595
}
9696
}
9797

98+
// SetLogLevel [Info:Debug]
99+
func (b *Batch) SetDebugLogLevel() {
100+
b.Log.SetLogLevel(log.Debug)
101+
}
102+
98103
// StopProducer to exit the Producer line.
99104
func (b *Batch) StopProducer() {
100105
b.Producer.Quit <- true
@@ -107,7 +112,8 @@ func (b *Batch) Stop() {
107112

108113
// Close is the exit function to terminate the batch processing.
109114
func (b *Batch) Close() {
110-
b.Log.WithFields(log.Fields{"Remaining Items": len(items)}).Warn("CheckRemainingItems")
115+
//b.Log.WithFields(log.Fields{"Remaining Items": len(items)}).Warn("CheckRemainingItems")
116+
b.Log.Infoln("CheckRemainingItems", "Remaining=", len(items))
111117

112118
done := make(chan bool)
113119

consumer.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package batch
22

33
import (
44
"context"
5-
log "github.com/sirupsen/logrus"
5+
log "github.com/Deeptiman/go-batch/logger"
66
"os"
77
"os/signal"
88
"sync"
@@ -61,7 +61,7 @@ func NewBatchConsumer() *BatchConsumer {
6161
Workerline: &sync.WaitGroup{},
6262
TerminateCh: make(chan os.Signal, 1),
6363
Quit: make(chan bool, 1),
64-
Log: log.New(),
64+
Log: log.NewLogger(),
6565
}
6666
}
6767

@@ -123,7 +123,7 @@ func (c *BatchConsumer) ConsumerBatch(ctx context.Context) {
123123
for {
124124
select {
125125
case batchItems := <-c.ConsumerCh:
126-
c.Log.WithFields(log.Fields{"Receive Batch Items": len(batchItems)}).Info("BatchConsumer")
126+
c.Log.Infoln("BatchConsumer", "Receive Batch Items:", len(batchItems))
127127

128128
c.BatchWorkerCh <- batchItems
129129
case <-ctx.Done():
@@ -145,7 +145,7 @@ func (c *BatchConsumer) WorkerFunc(index int) {
145145

146146
for batch := range c.BatchWorkerCh {
147147

148-
c.Log.WithFields(log.Fields{"Worker": index, "Batch": len(batch)}).Warn("Workerline")
148+
c.Log.Debugln("Workerline", "Worker=", index, "Batch=", len(batch))
149149

150150
go c.GetBatchSupply()
151151

logger/logger.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package logger
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
)
6+
7+
type LogLevel int
8+
9+
const (
10+
Info LogLevel = iota
11+
Debug
12+
)
13+
14+
type Logger struct {
15+
log *logrus.Logger
16+
}
17+
18+
func NewLogger() *Logger {
19+
log := logrus.New()
20+
21+
log.SetFormatter(&logrus.TextFormatter{
22+
DisableColors: false,
23+
ForceColors: true,
24+
DisableTimestamp: true,
25+
TimestampFormat: "2006-01-02 15:04:05",
26+
FullTimestamp: true,
27+
})
28+
29+
return &Logger{
30+
log: log,
31+
}
32+
}
33+
34+
func (l *Logger) SetLogLevel(level LogLevel) {
35+
if level == Debug {
36+
l.log.Level = logrus.DebugLevel
37+
}
38+
}
39+
40+
func (l *Logger) Trace(format string, args ...interface{}) {
41+
42+
}
43+
44+
func (l *Logger) Debug(args ...interface{}) {
45+
l.log.Debug(args...)
46+
}
47+
48+
func (l *Logger) Debugf(format string, args ...interface{}) {
49+
l.log.Debugf(format, args...)
50+
}
51+
52+
func (l *Logger) Debugln(args ...interface{}) {
53+
l.log.Debugln(args...)
54+
}
55+
56+
func (l *Logger) Info(args ...interface{}) {
57+
l.log.Info(args...)
58+
}
59+
60+
func (l *Logger) Infof(format string, args ...interface{}) {
61+
l.log.Infof(format, args...)
62+
}
63+
64+
func (l *Logger) Infoln(args ...interface{}) {
65+
l.log.Infoln(args...)
66+
}
67+
68+
func (l *Logger) Warn(format string, args ...interface{}) {
69+
l.log.Warn(args...)
70+
}
71+
72+
func (l *Logger) Warnf(format string, args ...interface{}) {
73+
l.log.Warnf(format, args...)
74+
}
75+
76+
func (l *Logger) Warnln(format string, args ...interface{}) {
77+
l.log.Warnln(args...)
78+
}
79+
80+
func (l *Logger) Fatal(format string, args ...interface{}) {
81+
l.log.Fatal(args...)
82+
}
83+
84+
func (l *Logger) Fatalf(format string, args ...interface{}) {
85+
l.log.Fatalf(format, args...)
86+
}
87+
88+
func (l *Logger) Fatalln(format string, args ...interface{}) {
89+
l.log.Fatalln(args...)
90+
}
91+
92+
func (l *Logger) Error(format string, args ...interface{}) {
93+
l.log.Error(args...)
94+
}
95+
96+
func (l *Logger) Errorf(format string, args ...interface{}) {
97+
l.log.Errorf(format, args...)
98+
}
99+
100+
func (l *Logger) Errorln(format string, args ...interface{}) {
101+
l.log.Errorln(args...)
102+
}
103+
104+
func (l *Logger) WithField(key string, value interface{}) {
105+
l.log.WithField(key, value)
106+
}
107+
108+
func (l *Logger) WithFields(fields logrus.Fields) {
109+
l.log.WithFields(fields)
110+
}

producer.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package batch
33
import (
44
"sync/atomic"
55
"time"
6-
7-
log "github.com/sirupsen/logrus"
6+
log "github.com/Deeptiman/go-batch/logger"
87
)
98

109
var (
@@ -53,7 +52,7 @@ func NewBatchProducer(callBackFn ConsumerFunc, opts ...BatchOptions) *BatchProdu
5352
MaxWait: DefaultMaxWait,
5453
BatchNo: DefaultBatchNo,
5554
Quit: make(chan bool),
56-
Log: log.New(),
55+
Log: log.NewLogger(),
5756
}
5857
}
5958

@@ -73,19 +72,18 @@ func (p *BatchProducer) WatchProducer() {
7372
case item := <-p.Watcher:
7473

7574
item.BatchNo = int(p.getBatchNo())
76-
p.Log.WithFields(log.Fields{"Id": item.Id, "Batch_Break": item.Id / int(p.MaxItems), "BatchNo": item.BatchNo, "Item": item.Item}).Info("BatchProducer")
77-
75+
p.Log.Debugln("BatchProducer", "Id=", item.Id, "Batch Break=", item.Id / int(p.MaxItems), "BatchNo=",item.BatchNo, "Item=", item.Item)
76+
7877
items = append(items, *item)
7978

8079
if (item.Id / int(p.MaxItems)) == item.BatchNo {
81-
p.Log.WithFields(log.Fields{"Item Size": len(items), "MaxItems": p.MaxItems}).Warn("BatchReady")
80+
p.Log.Infoln("BatchReady", "BatchNo=", item.BatchNo)
8281
items = p.releaseBatch(items)
8382
p.createBatchNo()
8483
}
8584

8685
case <-time.After(p.MaxWait):
87-
p.Log.WithFields(log.Fields{"Items": len(items)}).Warn("MaxWait")
88-
86+
p.Log.Infoln("MaxWait", "Items=", len(items))
8987
if len(items) == 0 {
9088
return
9189
}

supply.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package batch
22

3-
import (
4-
log "github.com/sirupsen/logrus"
5-
)
6-
73
// GetBatchSupply request the WorkerChannel for the released []BatchItems. The BatchSupplyChannel
84
// works as a bidirectional channel to request/response for the final []BatchItems product.
95
// The ClientSupplyChannel will send the []BatchItems to the client.
@@ -17,7 +13,7 @@ func (c *BatchConsumer) GetBatchSupply() {
1713

1814
select {
1915
case supply := <-supplyCh:
20-
c.Log.WithFields(log.Fields{"Supply": len(supply)}).Warn("BatchSupply")
16+
c.Log.Debugln("BatchSupply", "Supply=", len(supply))
2117

2218
c.Supply.ClientSupplyCh <- supply
2319
}

0 commit comments

Comments
 (0)