Skip to content

feat(NSQ): configurable log levels #35

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

Merged
merged 1 commit into from
Jan 4, 2025
Merged
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
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))

return w
}

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

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

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