Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make log level configurable #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ 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 @@ -121,7 +116,7 @@ func (b *Batch) Close() {

select {
case <-done:
b.Log.Warn("Done")
b.Log.Info("Done")
b.Semaphore.Lock()
b.Stop()
close(b.Item)
Expand Down
14 changes: 11 additions & 3 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package main
import (
"flag"
"fmt"

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

// Resources Structure
Expand All @@ -19,13 +21,19 @@ func main() {
var rFlag, mFlag int
flag.IntVar(&rFlag, "r", 10, "No of resources")
flag.IntVar(&mFlag, "m", 10, "Maximum items")
debugFlag := flag.Bool("d", false, "Debug mode")
flag.Parse()

logLevel := batchLog.Info
if *debugFlag {
logLevel = batchLog.Debug
}

logs := log.New()

logs.Infoln("Batch Processing Example !")

b := batch.NewBatch(batch.WithMaxItems(uint64(mFlag)))
b := batch.NewBatch(batch.WithMaxItems(uint64(mFlag)), batch.WithLogLevel(logLevel))

b.StartBatchProcessing()

Expand Down Expand Up @@ -58,4 +66,4 @@ func main() {
}
}
b.Close()
}
}
9 changes: 8 additions & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
type LogLevel int

const (
Info LogLevel = iota
Error LogLevel = iota
Info
Debug
)

Expand All @@ -25,6 +26,8 @@ func NewLogger() *Logger {
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
// only log errors by default
log.SetLevel(logrus.ErrorLevel)

return &Logger{
log: log,
Expand All @@ -34,6 +37,10 @@ func NewLogger() *Logger {
func (l *Logger) SetLogLevel(level LogLevel) {
if level == Debug {
l.log.Level = logrus.DebugLevel
} else if level == Info {
l.log.Level = logrus.InfoLevel
} else {
l.log.Level = logrus.ErrorLevel
}
}

Expand Down
12 changes: 11 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package batch

import "time"
import (
"time"

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

type BatchOptions func(b *BatchProducer)

Expand All @@ -15,3 +19,9 @@ func WithMaxWait(maxWait time.Duration) BatchOptions {
b.MaxWait = maxWait
}
}

func WithLogLevel(level log.LogLevel) BatchOptions {
return func(b *BatchProducer) {
b.Log.SetLogLevel(level)
}
}
12 changes: 6 additions & 6 deletions producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,25 @@ func (p *BatchProducer) WatchProducer() {
case item := <-p.Watcher:

item.BatchNo = int(p.getBatchNo())
p.Log.Debugln("BatchProducer", "Id=", item.Id, "Batch Break=", item.Id / int(p.MaxItems), "BatchNo=",item.BatchNo, "Item=", item.Item)
p.Log.Debugln("BatchProducer", "Id=", item.Id, "Batch Break=", item.Id/int(p.MaxItems), "BatchNo=", item.BatchNo, "Item=", item.Item)

items = append(items, *item)

items = append(items, *item)

if (item.Id / int(p.MaxItems)) == item.BatchNo {
p.Log.Infoln("BatchReady", "BatchNo=", item.BatchNo)
items = p.releaseBatch(items)
p.createBatchNo()
}

case <-time.After(p.MaxWait):
p.Log.Infoln("MaxWait", "Items=", len(items))
if len(items) == 0 {
continue
}

items = p.releaseBatch(items)
case <-p.Quit:
p.Log.Warn("Quit BatchProducer")
p.Log.Info("Quit BatchProducer")

return
}
Expand Down