logger.go 1.4 KB
//package constant

//var Logger log.Logger

// 日志输出
//func init() {
//	Logger = logrus.NewLogrusLogger()
//	Logger.SetServiceName(constant.SERVICE_NAME)
//	Logger.SetLevel(constant.LOG_LEVEL)
//}


package log

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/constant"
	"path/filepath"
	"strconv"
)

var Logger *logs.BeeLogger

func init() {
	Logger = NewBeegoLogger(LoggerConfig{
		Filename: constant.LOG_File,
		Level:    constant.LOG_LEVEL,
	})
}

type LoggerConfig struct {
	Level      string `json:"level,omitempty"`
	Filename   string `json:"filename,omitempty"`
	MaxSize    int    `json:"max_size,omitempty"`
	MaxBackups int    `json:"max_backups,omitempty"`
	MaxAge     int    `json:"max_age,omitempty"`
	Compress   bool   `json:"compress,omitempty"`
}

func NewBeegoLogger(conf LoggerConfig) *logs.BeeLogger {
	filename := `{"filename":"` + filepath.ToSlash(conf.Filename) + `"}`

	log := logs.GetBeeLogger()

	log.SetLogger(logs.AdapterFile, filename)
	ilv, err := strconv.Atoi(conf.Level)
	if err != nil {
		ilv = logs.LevelDebug
	}
	log.SetLevel(ilv)
	log.EnableFuncCallDepth(true)
	log.SetLogFuncCallDepth(5)
	log.SetPrefix(constant.LOG_PREFIX)
	return log
}

func Debug(args ...interface{}) {
	beego.Debug(args...)
}
func Info(args ...interface{}) {
	beego.Info(args...)
}
func Warn(args ...interface{}) {
	beego.Warn(args...)
}
func Error(args ...interface{}) {
	beego.Error(args...)
}