基于 logger sdk-logger 封装 
                                                    
                        
                    
                    
  
                    
                    package logger
import (
    "io/ioutil"
    "os"
    "path/filepath"
    "sync"
    "github.com/pkg/errors"
    "github.com/rifflock/lfshook"
    "github.com/sirupsen/logrus"
)
var loggerIns *logger
type logger struct {
    loggerMap *sync.Map
    logPath   string
    env       string //环境变量
}
type Config struct {
    Env  string //环境变量
    Path string //日志文件路径
}
//初始化
func InitLogger(config Config) error {
    if config.Path == "" {
        return errors.New("config path require")
    }
    if config.Env == "" {
        return errors.New("config env require")
    }
    loggerIns = &logger{
        loggerMap: new(sync.Map),
        env:       config.Env,
        logPath:   config.Path,
    }
    return nil
}
//获取实例
func GetLogger(arg ...string) *logrus.Logger {
    logName := "all"
    if len(arg) > 0 {
        logName = arg[0]
    }
    if log, ok := loggerIns.loggerMap.Load(logName); ok {
        return log.(*logrus.Logger)
    }
    newLog, _ := newLogger(logName)
    loggerIns.loggerMap.Store(logName, newLog)
    return newLog
}
func newLogger(logName string) (*logrus.Logger, error) {
    if logName == "" {
        return nil, errors.New("logName require")
    }
    var logger *logrus.Logger
    if _, err := os.Stat(loggerIns.logPath); os.IsNotExist(err) {
        if err := os.MkdirAll(loggerIns.logPath, os.ModePerm); err != nil {
            return nil, err
        }
    }
    hook := lfshook.NewHook(lfshook.PathMap{
        logrus.InfoLevel:  filepath.Join(loggerIns.logPath, logName) + ".out.log",
        logrus.ErrorLevel: filepath.Join(loggerIns.logPath, logName) + ".err.log",
        logrus.DebugLevel: filepath.Join(loggerIns.logPath, logName) + ".debug.log",
        logrus.TraceLevel: filepath.Join(loggerIns.logPath, logName) + ".trace.log",
    }, &logrus.JSONFormatter{TimestampFormat: "2006-01-02 15:04:05"})
    logger = logrus.New()
    if loggerIns.env == "online" {
        logger.Out = ioutil.Discard
        logger.SetLevel(logrus.InfoLevel)
    } else {
        logger.Out = ioutil.Discard
        logger.SetLevel(logrus.DebugLevel)
    }
    logger.Hooks.Add(hook)
    return logger, nil
}测试文件
package logger_test
import (
    "testing"
    "xxxx/sdk-logger"
)
func TestMain(t *testing.M) {
    config := logger.Config{
        Env:  "dev",
        Path: "./log",
    }
    if err := logger.InitLogger(config); err != nil {
        panic(err)
    }
    t.Run()
}
func TestGetLogger(t *testing.T) {
    logger.GetLogger().Infof("测试日志呀")
    logger.GetLogger().WithField("title","测试 key").Debugf("这是 debug 日志")
}本作品采用《CC 协议》,转载必须注明作者和本文链接
                                
                                
                                    by JeffreyBool  blog :point_right: link
                                
                            
                        
                     
           JeffreyBool 的个人博客
 JeffreyBool 的个人博客
         
             
             
           
           关于 LearnKu
                关于 LearnKu
               
                     
                     
                     粤公网安备 44030502004330号
 粤公网安备 44030502004330号 
 
推荐文章: