Skip to content

Commit

Permalink
feat(NSQ): configurable log levels (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakame authored Jan 4, 2025
1 parent b05bfb1 commit 84d913e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions nsq.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func NewWorker(opts ...Option) *Worker {
panic(err)
}

w.p.SetLoggerLevel(nsq.LogLevel(w.opts.logLvl))

Check failure on line 47 in nsq.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)

return w
}

Expand All @@ -62,6 +64,8 @@ func (w *Worker) startConsumer() (err error) {
return
}

w.q.SetLoggerLevel(nsq.LogLevel(w.opts.logLvl))

Check failure on line 67 in nsq.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary conversion (unconvert)

w.q.AddHandler(nsq.HandlerFunc(func(msg *nsq.Message) error {
if len(msg.Body) == 0 {
// Returning nil will automatically send a FIN command to NSQ to mark the message as processed.
Expand Down
20 changes: 20 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import (

"github.com/golang-queue/queue"
"github.com/golang-queue/queue/core"

nsq "github.com/nsqio/go-nsq"
)

// Log levels (same as [nsq.LogLevel])
const (
LogLevelDebug nsq.LogLevel = iota
LogLevelInfo
LogLevelWarning
LogLevelError
LogLevelMax = iota - 1 // convenience - match highest log level
)

// An Option configures a mutex.
Expand All @@ -27,6 +38,7 @@ type Options struct {
channel string
runFunc func(context.Context, core.QueuedMessage) error
logger queue.Logger
logLvl nsq.LogLevel
}

// WithAddr setup the addr of NSQ
Expand Down Expand Up @@ -71,6 +83,13 @@ func WithLogger(l queue.Logger) Option {
})
}

// WithLogLevel set custom [nsq] log level
func WithLogLevel(lvl nsq.LogLevel) Option {
return OptionFunc(func(o *Options) {
o.logLvl = lvl
})
}

func newOptions(opts ...Option) Options {
defaultOpts := Options{
addr: "127.0.0.1:4150",
Expand All @@ -79,6 +98,7 @@ func newOptions(opts ...Option) Options {
maxInFlight: 1,

logger: queue.NewLogger(),
logLvl: LogLevelInfo,
runFunc: func(context.Context, core.QueuedMessage) error {
return nil
},
Expand Down

0 comments on commit 84d913e

Please sign in to comment.