logger.go 1.2 KB
package log

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"gitlab.fjmaimaimai.com/mmm-go/partner/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(3)
	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...)
}