增加日志初始化的处理
This commit is contained in:
parent
d25382b87a
commit
c150262e50
90
client.go
90
client.go
@ -11,13 +11,30 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.zhangdeman.cn/zhangdeman/util"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/logger"
|
"git.zhangdeman.cn/zhangdeman/logger"
|
||||||
redisInstance "github.com/go-redis/redis/v8"
|
redisInstance "github.com/go-redis/redis/v8"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Client 连接实例
|
||||||
|
Client ClientInterface
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
Client = &OwnClient{
|
||||||
|
lock: &sync.RWMutex{},
|
||||||
|
instanceTable: make(map[string]*RealClient),
|
||||||
|
confTable: make(map[string]Options),
|
||||||
|
parseErrorFunc: defaultParseError,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// defaultParseError ...
|
// defaultParseError ...
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
@ -67,8 +84,9 @@ type RealClient struct {
|
|||||||
//
|
//
|
||||||
// Date : 5:05 下午 2021/2/27
|
// Date : 5:05 下午 2021/2/27
|
||||||
func NewClient(config map[string]Options, parseErrorFunc func(err error) error) (ClientInterface, error) {
|
func NewClient(config map[string]Options, parseErrorFunc func(err error) error) (ClientInterface, error) {
|
||||||
c := &Client{
|
c := &OwnClient{
|
||||||
instanceTable: make(map[string]*RealClient),
|
instanceTable: make(map[string]*RealClient),
|
||||||
|
loggerTable: make(map[string]*zap.Logger),
|
||||||
confTable: config,
|
confTable: config,
|
||||||
parseErrorFunc: parseErrorFunc,
|
parseErrorFunc: parseErrorFunc,
|
||||||
}
|
}
|
||||||
@ -78,26 +96,42 @@ func NewClient(config map[string]Options, parseErrorFunc func(err error) error)
|
|||||||
return c, c.init()
|
return c, c.init()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client 包装的redis client
|
// OwnClient 包装的redis client
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 4:52 下午 2021/2/27
|
// Date : 4:52 下午 2021/2/27
|
||||||
type Client struct {
|
type OwnClient struct {
|
||||||
|
lock *sync.RWMutex // 锁
|
||||||
|
loggerTable map[string]*zap.Logger // 日志实例
|
||||||
instanceTable map[string]*RealClient // redis 实例
|
instanceTable map[string]*RealClient // redis 实例
|
||||||
confTable map[string]Options // redis 配置
|
confTable map[string]Options // redis 配置
|
||||||
parseErrorFunc func(err error) error // 解析err的function,解析执行结果是否为失败,有的场景,执行成功,返回 redis:nil / redis:<nil>
|
parseErrorFunc func(err error) error // 解析err的function,解析执行结果是否为失败,有的场景,执行成功,返回 redis:nil / redis:<nil>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// loadConfig 载入配置文件
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 14:31 2022/6/15
|
||||||
|
func (c *OwnClient) loadConfig(cfgPath string) (*FullConfig, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
cfg FullConfig
|
||||||
|
)
|
||||||
|
|
||||||
|
if err = util.File.ReadJSONContent(cfgPath, &cfg); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &cfg, nil
|
||||||
|
}
|
||||||
|
|
||||||
// init 初始化redis连接
|
// init 初始化redis连接
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 5:31 下午 2021/2/27
|
// Date : 5:31 下午 2021/2/27
|
||||||
func (c *Client) init() error {
|
func (c *OwnClient) init() error {
|
||||||
var (
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
for flag, conf := range c.confTable {
|
for flag, conf := range c.confTable {
|
||||||
c.instanceTable[flag] = &RealClient{
|
c.instanceTable[flag] = &RealClient{
|
||||||
@ -106,9 +140,6 @@ func (c *Client) init() error {
|
|||||||
Logger: nil,
|
Logger: nil,
|
||||||
LoggerFieldConfig: conf.LoggerFieldConfig,
|
LoggerFieldConfig: conf.LoggerFieldConfig,
|
||||||
}
|
}
|
||||||
if c.instanceTable[flag].Logger, err = c.getLogger(conf.Logger); nil != err {
|
|
||||||
return LoggerInitFail(flag, err)
|
|
||||||
}
|
|
||||||
if nil == c.instanceTable[flag].LoggerFieldConfig {
|
if nil == c.instanceTable[flag].LoggerFieldConfig {
|
||||||
c.instanceTable[flag].LoggerFieldConfig = &LogFieldConfig{
|
c.instanceTable[flag].LoggerFieldConfig = &LogFieldConfig{
|
||||||
Message: "",
|
Message: "",
|
||||||
@ -133,36 +164,15 @@ func (c *Client) init() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// getLogger ...
|
// initLogger 初始化日志
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 7:07 下午 2021/2/27
|
// Date : 7:07 下午 2021/2/27
|
||||||
func (c *Client) getLogger(conf *LoggerConfig) (*zap.Logger, error) {
|
func (c *OwnClient) initLogger(flag string, conf *logger.InputLogConfig) error {
|
||||||
if nil == conf || nil == conf.SplitConfig {
|
var err error
|
||||||
return nil, nil
|
c.loggerTable[flag], err = logger.GetLogInstanceFromInputConfig(conf)
|
||||||
}
|
return LoggerInitFail(flag, err)
|
||||||
optionFuncList := make([]logger.SetLoggerOptionFunc, 0)
|
|
||||||
if conf.ConsoleOutput {
|
|
||||||
optionFuncList = append(optionFuncList, logger.WithConsoleOutput())
|
|
||||||
}
|
|
||||||
|
|
||||||
if conf.Encoder != nil {
|
|
||||||
optionFuncList = append(optionFuncList, logger.WithEncoder(conf.Encoder))
|
|
||||||
}
|
|
||||||
|
|
||||||
splitConfigFuncList := []logger.SetRotateLogConfigFunc{
|
|
||||||
logger.WithTimeIntervalType(conf.SplitConfig.TimeIntervalType),
|
|
||||||
logger.WithDivisionChar(conf.SplitConfig.DivisionChar),
|
|
||||||
logger.WithMaxAge(conf.SplitConfig.MaxAge),
|
|
||||||
}
|
|
||||||
|
|
||||||
splitConfig, _ := logger.NewRotateLogConfig(conf.SplitConfig.LogPath, conf.SplitConfig.LogFileName, splitConfigFuncList...)
|
|
||||||
return logger.NewLogger(
|
|
||||||
conf.LoggerLevel,
|
|
||||||
splitConfig,
|
|
||||||
optionFuncList...,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRedisClient 获取redis实例
|
// GetRedisClient 获取redis实例
|
||||||
@ -170,7 +180,7 @@ func (c *Client) getLogger(conf *LoggerConfig) (*zap.Logger, error) {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 5:16 下午 2021/2/27
|
// Date : 5:16 下午 2021/2/27
|
||||||
func (c *Client) GetRedisClient(flag string) (*RealClient, error) {
|
func (c *OwnClient) GetRedisClient(flag string) (*RealClient, error) {
|
||||||
redisClient, exist := c.instanceTable[flag]
|
redisClient, exist := c.instanceTable[flag]
|
||||||
if !exist {
|
if !exist {
|
||||||
return nil, FlagNotFound(flag)
|
return nil, FlagNotFound(flag)
|
||||||
@ -183,7 +193,7 @@ func (c *Client) GetRedisClient(flag string) (*RealClient, error) {
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 8:52 下午 2021/2/27
|
// Date : 8:52 下午 2021/2/27
|
||||||
func (c *Client) log(ctx *Context, realClient *RealClient, cmdResult redisInstance.Cmder, startTime int64, finishTime int64) {
|
func (c *OwnClient) log(ctx *Context, realClient *RealClient, cmdResult redisInstance.Cmder, startTime int64, finishTime int64) {
|
||||||
if nil == realClient || nil == realClient.Logger {
|
if nil == realClient || nil == realClient.Logger {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -201,7 +211,7 @@ func (c *Client) log(ctx *Context, realClient *RealClient, cmdResult redisInstan
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 9:41 下午 2021/2/27
|
// Date : 9:41 下午 2021/2/27
|
||||||
func (c *Client) CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (string, error) {
|
func (c *OwnClient) CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (string, error) {
|
||||||
var (
|
var (
|
||||||
realClient *RealClient
|
realClient *RealClient
|
||||||
err error
|
err error
|
||||||
@ -228,7 +238,7 @@ func (c *Client) CommandProxy(ctx *Context, flag string, cmd string, param ...in
|
|||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
//
|
//
|
||||||
// Date : 10:00 下午 2021/2/27
|
// Date : 10:00 下午 2021/2/27
|
||||||
func (c *Client) CommandProxyWithReceiver(ctx *Context, flag string, receiver interface{}, cmd string, param ...interface{}) error {
|
func (c *OwnClient) CommandProxyWithReceiver(ctx *Context, flag string, receiver interface{}, cmd string, param ...interface{}) error {
|
||||||
if nil == receiver {
|
if nil == receiver {
|
||||||
return ReceiverISNIL()
|
return ReceiverISNIL()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user