基於 logger sdk-logger 封裝

kenuo發表於2019-07-09
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 日誌")
}

by JeffreyBool blog :point_right: link

相關文章