增加日志初始化的处理

This commit is contained in:
白茶清欢 2022-06-15 15:02:45 +08:00
parent d25382b87a
commit c150262e50
2 changed files with 53 additions and 41 deletions

View File

@ -11,13 +11,30 @@ import (
"encoding/json"
"fmt"
"strings"
"sync"
"time"
"git.zhangdeman.cn/zhangdeman/util"
"git.zhangdeman.cn/zhangdeman/logger"
redisInstance "github.com/go-redis/redis/v8"
"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 ...
//
// Author : go_developer@163.com<白茶清欢>
@ -67,8 +84,9 @@ type RealClient struct {
//
// Date : 5:05 下午 2021/2/27
func NewClient(config map[string]Options, parseErrorFunc func(err error) error) (ClientInterface, error) {
c := &Client{
c := &OwnClient{
instanceTable: make(map[string]*RealClient),
loggerTable: make(map[string]*zap.Logger),
confTable: config,
parseErrorFunc: parseErrorFunc,
}
@ -78,26 +96,42 @@ func NewClient(config map[string]Options, parseErrorFunc func(err error) error)
return c, c.init()
}
// Client 包装的redis client
// OwnClient 包装的redis client
//
// Author : go_developer@163.com<白茶清欢>
//
// 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 实例
confTable map[string]Options // redis 配置
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连接
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:31 下午 2021/2/27
func (c *Client) init() error {
var (
err error
)
func (c *OwnClient) init() error {
for flag, conf := range c.confTable {
c.instanceTable[flag] = &RealClient{
@ -106,9 +140,6 @@ func (c *Client) init() error {
Logger: nil,
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 {
c.instanceTable[flag].LoggerFieldConfig = &LogFieldConfig{
Message: "",
@ -133,36 +164,15 @@ func (c *Client) init() error {
return nil
}
// getLogger ...
// initLogger 初始化日志
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 7:07 下午 2021/2/27
func (c *Client) getLogger(conf *LoggerConfig) (*zap.Logger, error) {
if nil == conf || nil == conf.SplitConfig {
return nil, nil
}
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...,
)
func (c *OwnClient) initLogger(flag string, conf *logger.InputLogConfig) error {
var err error
c.loggerTable[flag], err = logger.GetLogInstanceFromInputConfig(conf)
return LoggerInitFail(flag, err)
}
// GetRedisClient 获取redis实例
@ -170,7 +180,7 @@ func (c *Client) getLogger(conf *LoggerConfig) (*zap.Logger, error) {
// Author : go_developer@163.com<白茶清欢>
//
// 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]
if !exist {
return nil, FlagNotFound(flag)
@ -183,7 +193,7 @@ func (c *Client) GetRedisClient(flag string) (*RealClient, error) {
// Author : go_developer@163.com<白茶清欢>
//
// 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 {
return
}
@ -201,7 +211,7 @@ func (c *Client) log(ctx *Context, realClient *RealClient, cmdResult redisInstan
// Author : go_developer@163.com<白茶清欢>
//
// 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 (
realClient *RealClient
err error
@ -228,7 +238,7 @@ func (c *Client) CommandProxy(ctx *Context, flag string, cmd string, param ...in
// Author : go_developer@163.com<白茶清欢>
//
// 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 {
return ReceiverISNIL()
}

View File

@ -7,7 +7,9 @@
// Date : 2022-06-15 11:52
package redis
import "git.zhangdeman.cn/zhangdeman/logger"
import (
"git.zhangdeman.cn/zhangdeman/logger"
)
// FullConfig 完整配置
//