发布于 4年前

Go语言日志框架logrus封装

logrus在github上是一个star数比较高的go日志框架,简单封装如下:

package log
import (
    "fmt"
    "github.com/Sirupsen/logrus"
    "runtime"
    "strings"
)
var logger = logrus.New()
// 封装logrus.Fields
type Fields logrus.Fields
func SetLogLevel(level logrus.Level) {
    logger.Level = level
}
func SetLogFormatter(formatter logrus.Formatter) {
    logger.Formatter = formatter
}
// Debug
func Debug(args ...interface{}) {
    if logger.Level >= logrus.DebugLevel {
        entry := logger.WithFields(logrus.Fields{})
        entry.Data["file"] = fileInfo(2)
        entry.Debug(args)
    }
}
// 带有field的Debug
func DebugWithFields(l interface{}, f Fields) {
    if logger.Level >= logrus.DebugLevel {
        entry := logger.WithFields(logrus.Fields(f))
        entry.Data["file"] = fileInfo(2)
        entry.Debug(l)
    }
}
// Info
func Info(args ...interface{}) {
    if logger.Level >= logrus.InfoLevel {
        entry := logger.WithFields(logrus.Fields{})
        entry.Data["file"] = fileInfo(2)
        entry.Info(args...)
    }
}
// 带有field的Info
func InfoWithFields(l interface{}, f Fields) {
    if logger.Level >= logrus.InfoLevel {
        entry := logger.WithFields(logrus.Fields(f))
        entry.Data["file"] = fileInfo(2)
        entry.Info(l)
    }
}
// Warn
func Warn(args ...interface{}) {
    if logger.Level >= logrus.WarnLevel {
        entry := logger.WithFields(logrus.Fields{})
        entry.Data["file"] = fileInfo(2)
        entry.Warn(args...)
    }
}
// 带有Field的Warn
func WarnWithFields(l interface{}, f Fields) {
    if logger.Level >= logrus.WarnLevel {
        entry := logger.WithFields(logrus.Fields(f))
        entry.Data["file"] = fileInfo(2)
        entry.Warn(l)
    }
}
// Error
func Error(args ...interface{}) {
    if logger.Level >= logrus.ErrorLevel {
        entry := logger.WithFields(logrus.Fields{})
        entry.Data["file"] = fileInfo(2)
        entry.Error(args...)
    }
}
// 带有Fields的Error
func ErrorWithFields(l interface{}, f Fields) {
    if logger.Level >= logrus.ErrorLevel {
        entry := logger.WithFields(logrus.Fields(f))
        entry.Data["file"] = fileInfo(2)
        entry.Error(l)
    }
}
// Fatal
func Fatal(args ...interface{}) {
    if logger.Level >= logrus.FatalLevel {
        entry := logger.WithFields(logrus.Fields{})
        entry.Data["file"] = fileInfo(2)
        entry.Fatal(args...)
    }
}
// 带有Field的Fatal
func FatalWithFields(l interface{}, f Fields) {
    if logger.Level >= logrus.FatalLevel {
        entry := logger.WithFields(logrus.Fields(f))
        entry.Data["file"] = fileInfo(2)
        entry.Fatal(l)
    }
}
// Panic
func Panic(args ...interface{}) {
    if logger.Level >= logrus.PanicLevel {
        entry := logger.WithFields(logrus.Fields{})
        entry.Data["file"] = fileInfo(2)
        entry.Panic(args...)
    }
}
// 带有Field的Panic
func PanicWithFields(l interface{}, f Fields) {
    if logger.Level >= logrus.PanicLevel {
        entry := logger.WithFields(logrus.Fields(f))
        entry.Data["file"] = fileInfo(2)
        entry.Panic(l)
    }
}
func fileInfo(skip int) string {
    _, file, line, ok := runtime.Caller(skip)
    if !ok {
        file = "<???>"
        line = 1
    } else {
        slash := strings.LastIndex(file, "/")
        if slash >= 0 {
            file = file[slash+1:]
        }
    }
    return fmt.Sprintf("%s:%d", file, line)
}
©2020 edoou.com   京ICP备16001874号-3