-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
50 lines (43 loc) · 1.27 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package pulsarKit
import (
"github.com/apache/pulsar-client-go/pulsar"
"github.com/apache/pulsar-client-go/pulsar/log"
"github.com/richelieu-yang/chimera/v2/src/core/errorKit"
"github.com/richelieu-yang/chimera/v2/src/core/sliceKit"
"github.com/richelieu-yang/chimera/v2/src/core/strKit"
"github.com/richelieu-yang/chimera/v2/src/log/logrusKit"
"github.com/richelieu-yang/chimera/v2/src/netKit"
"github.com/sirupsen/logrus"
)
// NewClient
/*
@param logPath 客户端的日志输出(为空则输出到控制台; 不会rotate)
*/
func NewClient(addresses []string, logPath string) (pulsar.Client, error) {
/* url */
tmp, err := netKit.ProcessAddresses(addresses)
if err != nil {
return nil, err
}
url := UrlPrefix + sliceKit.Join(tmp, ",")
/* logger */
var logger log.Logger
if strKit.IsNotEmpty(logPath) {
fileLogger, err := logrusKit.NewFileLogger(logPath)
if err != nil {
return nil, err
}
// 文件日志的日志级别 与 logrus全局日志级别 保持一致
fileLogger.SetLevel(logrus.GetLevel())
logger = log.NewLoggerWithLogrus(fileLogger)
}
client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: url,
Logger: logger,
})
if err != nil {
err = errorKit.Wrap(err, "fail to new client")
return nil, err
}
return client, nil
}