123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- package base
- import (
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/sirupsen/logrus"
- "os"
- "strings"
- "time"
- )
- // const values
- const (
- NOLOGFlAG string = "NOLOGFlAG"
- TimestampFormat string = "2003-01-02 15:04:05.000"
- )
- // getLogLevel for API
- func getLogLevel(loglevel string) (logrus.Level, bool) {
- switch loglevel {
- case "debug":
- return logrus.DebugLevel, true
- case "info":
- return logrus.InfoLevel, true
- case "warn":
- return logrus.WarnLevel, true
- case "error":
- return logrus.ErrorLevel, true
- case "fatal":
- return logrus.FatalLevel, true
- case "panic":
- return logrus.PanicLevel, true
- default:
- return 0, false
- }
- }
- // InitLog for API
- func InitLog(loglevel string) {
- logrus.SetFormatter(&logrus.JSONFormatter{
- TimestampFormat: TimestampFormat,
- })
- logrus.SetOutput(os.Stdout)
- if level, exist := getLogLevel(loglevel); exist {
- logrus.SetLevel(level)
- } else {
- if GetJHYEnv() == ENV_DEV {
- logrus.SetLevel(logrus.DebugLevel)
- } else if GetJHYEnv() == ENV_TEST {
- logrus.SetLevel(logrus.DebugLevel)
- } else if GetJHYEnv() == ENV_PRODUCT {
- logrus.SetLevel(logrus.InfoLevel)
- }
- }
- }
- // Logger for API
- func Logger(ctx *gin.Context) *logrus.Entry {
- entry := logrus.NewEntry(logrus.StandardLogger()).WithFields(logrus.Fields{
- "requestId": GetRequestId(ctx),
- })
- return entry
- }
- // DebugLogger for API
- func DebugLogger(ctx *gin.Context, value ...interface{}) {
- if GetNoLogFlag(ctx) == true {
- return
- }
- if ctx != nil {
- Logger(ctx).Debug(value...)
- } else {
- logrus.Debug(value...)
- }
- }
- // DebugfLogger for API
- func DebugfLogger(ctx *gin.Context, format string, value ...interface{}) {
- if GetNoLogFlag(ctx) == true {
- return
- }
- if ctx != nil {
- Logger(ctx).Debugf(format, value...)
- } else {
- logrus.Debugf(format, value...)
- }
- }
- // InfoLogger for API
- func InfoLogger(ctx *gin.Context, value ...interface{}) {
- if GetNoLogFlag(ctx) == true {
- return
- }
- if ctx != nil {
- Logger(ctx).Info(value...)
- } else {
- logrus.Info(value...)
- }
- }
- // InfofLogger for API
- func InfofLogger(ctx *gin.Context, format string, value ...interface{}) {
- if GetNoLogFlag(ctx) == true {
- return
- }
- if ctx != nil {
- Logger(ctx).Infof(format, value...)
- } else {
- logrus.Infof(format, value...)
- }
- }
- // WarnLogger for API
- func WarnLogger(ctx *gin.Context, value ...interface{}) {
- if GetNoLogFlag(ctx) == true {
- return
- }
- if ctx != nil {
- Logger(ctx).Warn(value...)
- } else {
- logrus.Warn(value...)
- }
- }
- // WarnfLogger for API
- func WarnfLogger(ctx *gin.Context, format string, value ...interface{}) {
- if GetNoLogFlag(ctx) == true {
- return
- }
- if ctx != nil {
- Logger(ctx).Warnf(format, value...)
- } else {
- logrus.Warnf(format, value...)
- }
- }
- // ErrorLogger for API
- func ErrorLogger(ctx *gin.Context, value ...interface{}) {
- if ctx != nil {
- Logger(ctx).Error(value...)
- } else {
- logrus.Error(value...)
- }
- }
- // ErrorfLogger for API
- func ErrorfLogger(ctx *gin.Context, format string, value ...interface{}) {
- if ctx != nil {
- Logger(ctx).Errorf(format, value...)
- } else {
- logrus.Errorf(format, value...)
- }
- }
- // PanicLogger for API
- func PanicLogger(ctx *gin.Context, value ...interface{}) {
- if ctx != nil {
- Logger(ctx).Panic(value...)
- } else {
- logrus.Panic(value...)
- }
- }
- // PanicfLogger for API
- func PanicfLogger(ctx *gin.Context, format string, value ...interface{}) {
- if ctx != nil {
- Logger(ctx).Panicf(format, value...)
- } else {
- logrus.Panicf(format, value...)
- }
- }
- // SetNoLogFlag for API
- func SetNoLogFlag(ctx *gin.Context) {
- ctx.Set(NOLOGFlAG, true)
- }
- // GetNoLogFlag for API
- func GetNoLogFlag(ctx *gin.Context) bool {
- if ctx == nil {
- return false
- }
- flag, ok := ctx.Get(NOLOGFlAG)
- return ok == true && flag == true
- }
- // StackLogger for API
- func StackLogger(ctx *gin.Context, err error) {
- if !strings.Contains(fmt.Sprintf("%+v", err), "\n") {
- return
- }
- var info []byte
- if ctx != nil {
- info, _ = json.Marshal(map[string]interface{}{"time": time.Now().Format("2006-01-02 15:04:05"), "level": "error", "module": "errorstack", "requestId": GetRequestId(ctx)})
- } else {
- info, _ = json.Marshal(map[string]interface{}{"time": time.Now().Format("2006-01-02 15:04:05"), "level": "error", "module": "errorstack"})
- }
- fmt.Printf("%s\n-------------------stack-start-------------------\n%+v\n-------------------stack-end-------------------\n", string(info), err)
- }
|