chore(logx): add custom logger in queue (#627)

This commit is contained in:
Bo-Yi Wu 2021-08-11 11:13:29 +08:00 committed by GitHub
parent c81a316047
commit 24cf872b31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 0 deletions

45
logx/log_interface.go Normal file
View File

@ -0,0 +1,45 @@
package logx
import (
"fmt"
"github.com/sirupsen/logrus"
)
// QueueLogger for simple logger.
func QueueLogger() DefaultQueueLogger {
return DefaultQueueLogger{
accessLogger: LogAccess,
errorLogger: LogError,
}
}
// DefaultQueueLogger for queue custom logger
type DefaultQueueLogger struct {
accessLogger *logrus.Logger
errorLogger *logrus.Logger
}
func (l DefaultQueueLogger) Infof(format string, args ...interface{}) {
l.accessLogger.Printf(format, args...)
}
func (l DefaultQueueLogger) Errorf(format string, args ...interface{}) {
l.errorLogger.Printf(format, args...)
}
func (l DefaultQueueLogger) Fatalf(format string, args ...interface{}) {
l.errorLogger.Fatalf(format, args...)
}
func (l DefaultQueueLogger) Info(args ...interface{}) {
l.accessLogger.Println(fmt.Sprint(args...))
}
func (l DefaultQueueLogger) Error(args ...interface{}) {
l.errorLogger.Println(fmt.Sprint(args...))
}
func (l DefaultQueueLogger) Fatal(args ...interface{}) {
l.errorLogger.Println(fmt.Sprint(args...))
}

View File

@ -328,6 +328,7 @@ func main() {
w = simple.NewWorker( w = simple.NewWorker(
simple.WithQueueNum(int(cfg.Core.QueueNum)), simple.WithQueueNum(int(cfg.Core.QueueNum)),
simple.WithRunFunc(notify.Run(cfg)), simple.WithRunFunc(notify.Run(cfg)),
simple.WithLogger(logx.QueueLogger()),
) )
case core.NSQ: case core.NSQ:
w = nsq.NewWorker( w = nsq.NewWorker(
@ -336,6 +337,7 @@ func main() {
nsq.WithChannel(cfg.Queue.NSQ.Channel), nsq.WithChannel(cfg.Queue.NSQ.Channel),
nsq.WithMaxInFlight(int(cfg.Core.WorkerNum)), nsq.WithMaxInFlight(int(cfg.Core.WorkerNum)),
nsq.WithRunFunc(notify.Run(cfg)), nsq.WithRunFunc(notify.Run(cfg)),
nsq.WithLogger(logx.QueueLogger()),
) )
case core.NATS: case core.NATS:
w = nats.NewWorker( w = nats.NewWorker(
@ -343,6 +345,7 @@ func main() {
nats.WithSubj(cfg.Queue.NATS.Subj), nats.WithSubj(cfg.Queue.NATS.Subj),
nats.WithQueue(cfg.Queue.NATS.Queue), nats.WithQueue(cfg.Queue.NATS.Queue),
nats.WithRunFunc(notify.Run(cfg)), nats.WithRunFunc(notify.Run(cfg)),
nats.WithLogger(logx.QueueLogger()),
) )
default: default:
logx.LogError.Fatalf("we don't support queue engine: %s", cfg.Queue.Engine) logx.LogError.Fatalf("we don't support queue engine: %s", cfg.Queue.Engine)
@ -350,6 +353,7 @@ func main() {
q, err := queue.NewQueue( q, err := queue.NewQueue(
queue.WithWorker(w), queue.WithWorker(w),
queue.WithLogger(logx.QueueLogger()),
queue.WithWorkerCount(int(cfg.Core.WorkerNum)), queue.WithWorkerCount(int(cfg.Core.WorkerNum)),
) )
if err != nil { if err != nil {

View File

@ -13,6 +13,7 @@ import (
"github.com/appleboy/gorush/config" "github.com/appleboy/gorush/config"
"github.com/appleboy/gorush/core" "github.com/appleboy/gorush/core"
"github.com/appleboy/gorush/logx"
"github.com/appleboy/gorush/notify" "github.com/appleboy/gorush/notify"
"github.com/appleboy/gorush/status" "github.com/appleboy/gorush/status"
@ -52,6 +53,7 @@ func TestMain(m *testing.M) {
q, _ = queue.NewQueue( q, _ = queue.NewQueue(
queue.WithWorker(w), queue.WithWorker(w),
queue.WithWorkerCount(4), queue.WithWorkerCount(4),
queue.WithLogger(logx.QueueLogger()),
) )
q.Start() q.Start()