iris 系列文章 封裝 logrus 日誌

kenuo發表於2019-06-12
func Init() {
    initLog()    // 普通日誌
    initAlipay() //支付寶日誌
    initOrder()  //訂單日誌
    initWechat() //微信日誌
}

var Log *logrus.Logger

func initLog() {
    logpath := config.GetConf().Log.LoggerPath
    logname := config.GetConf().Log.LoggerFiles.Logger
    _ = util.CreateFile(logpath)
    hook := lfshook.NewHook(lfshook.PathMap{
        logrus.InfoLevel:  filepath.Join(logpath, logname) + ".log",
        logrus.ErrorLevel: filepath.Join(logpath, logname) + ".err.log",
        logrus.WarnLevel:  filepath.Join(logpath, logname) + ".debug.log",
    }, &logrus.JSONFormatter{TimestampFormat: "2006-01-02 15:04:05"})
    Log = logrus.New()
    Log.Hooks.Add(hook)
    Log.Out = ioutil.Discard
}

// WithField 結構化欄位寫入
func WithField(key string, value interface{}) *logrus.Entry {
    return Log.WithFields(map[string]interface{}{key: value})
}

func WithFields(fields map[string]interface{}) *logrus.Entry {
    return Log.WithFields(fields)
}

by JeffreyBool blog :point_right: link

相關文章