transfer_middleware/until/mq/recover.go

41 lines
1023 B
Go
Raw Normal View History

2024-06-18 16:34:14 +08:00
package mq
import (
"fmt"
"github.com/apache/rocketmq-client-go/v2/primitive"
)
type Logger interface {
// Debugf logs a formatted debugging message.
Debugf(format string, args ...interface{})
// Infof logs a formatted informational message.
Infof(format string, args ...interface{})
// Warnf logs a formatted warning message.
Warnf(format string, args ...interface{})
// Errorf logs a formatted error message.
Errorf(format string, args ...interface{})
}
func init() {
SetRecoverLogger(nil)
}
// SetRecoverLogger 设置RocketMQ的recover的处理函数日志不设置会导致协程panic后程序退出
func SetRecoverLogger(logger Logger) {
SetRecoverHandler(func(err any) {
if logger == nil {
fmt.Printf("rocketmq 发生 panic%+v\n", err)
return
}
logger.Errorf("rocketmq 发生 panic%+v", err)
})
}
// SetRecoverHandler 设置RocketMQ的recover的处理函数不设置会导致协程panic后程序退出
func SetRecoverHandler(fn func(any)) {
primitive.PanicHandler = fn
}