配置beego的logs模块

本文介绍了一种改进Beego框架日志配置的方法,通过自定义结构体简化配置过程,并提供了一个具体的实现示例。

beego的logs模块挺好用,不过在配置logs模块的参数时,使用的是json字符串进行配置:

logs.SetLogger(logs.AdapterFile,`{"filename":"project.log","level":7,"maxlines":0,"maxsize":0,"daily":true,"maxdays":10}`)

这样手工拼个json字符串我感觉还是不太方便,为什么不搞个struct呢?比较纳闷!跟进代码后发现,beego其实是有定义struct的,但都是不可导出,比如控制台日志的配置如下(console.go):

// consoleWriter implements LoggerInterface and writes messages to terminal.
type consoleWriter struct {
	lg       *logWriter
	Level    int  `json:"level"`
	Colorful bool `json:"color"` //this filed is useful only when system's terminal supports color
}

文件日志的配置如下(file.go):


// fileLogWriter implements LoggerInterface.
// It writes messages by lines limit, file size limit, or time frequency.
type fileLogWriter struct {
	sync.RWMutex // write log order by order and  atomic incr maxLinesCurLines and maxSizeCurSize
	// The opened file
	Filename   string `json:"filename"`
	fileWriter *os.File

	// Rotate at line
	MaxLines         int `json:"maxlines"`
	maxLinesCurLines int

	// Rotate at size
	MaxSize        int `json:"maxsize"`
	maxSizeCurSize int

	// Rotate daily
	Daily         bool  `json:"daily"`
	MaxDays       int64 `json:"maxdays"`
	dailyOpenDate int
	dailyOpenTime time.Time

	Rotate bool `json:"rotate"`

	Level int `json:"level"`

	Perm string `json:"perm"`

	RotatePerm string `json:"rotateperm"`

	fileNameOnly, suffix string // like "project.log", project is fileNameOnly and .log is suffix
}

既然如此,那么只好自己来定义了:

// beego 日志配置结构体
type LoggerConfig struct {
	FileName            string `json:"filename"`
	Level               int    `json:"level"`    // 日志保存的时候的级别,默认是 Trace 级别
	Maxlines            int    `json:"maxlines"` // 每个文件保存的最大行数,默认值 1000000
	Maxsize             int    `json:"maxsize"`  // 每个文件保存的最大尺寸,默认值是 1 << 28, //256 MB
	Daily               bool   `json:"daily"`    // 是否按照每天 logrotate,默认是 true
	Maxdays             int    `json:"maxdays"`  // 文件最多保存多少天,默认保存 7 天
	Rotate              bool   `json:"rotate"`   // 是否开启 logrotate,默认是 true
	Perm                string `json:"perm"`     // 日志文件权限
	RotatePerm          string `json:"rotateperm"`
	EnableFuncCallDepth bool   `json:"-"` // 输出文件名和行号
	LogFuncCallDepth    int    `json:"-"` // 函数调用层级
}

我这里定义的是文件日志的配置strcut(我想大多数程序都是使用文件日志比较多吧),在使用时,先给LoggerConfig的对象赋值,然后再将它转为json字符串:

func main() {
	var logCfg = LoggerConfig{
		FileName:            "./daily.log",
		Level:               7,
		EnableFuncCallDepth: true,
		LogFuncCallDepth:    3,
		RotatePerm:          "777",
		Perm:                "777",
	}

	// 设置beego log库的配置
	b, _ := json.Marshal(&logCfg)
	logs.SetLogger(logs.AdapterFile, string(b))
	logs.EnableFuncCallDepth(logCfg.EnableFuncCallDepth)
	logs.SetLogFuncCallDepth(logCfg.LogFuncCallDepth)

	logs.Info("program start...")
    
    // do something
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值