[TOC] # 简介 **注意权限问题** beego 的日志处理是基于 logs 模块搭建的,内置了一个变量`BeeLogger`,默认已经是`logs.BeeLogger`类型,初始化了 console,也就是默认输出到`console` 导包 ~~~ import ( "github.com/astaxie/beego/logs" ) ~~~ 然后添加输出引擎(log 支持同时输出到多个引擎),这里我们以 console 为例,第一个参数是引擎名(包括:console、file、conn、smtp、es、multifile) ~~~ logs.SetLogger("console") ~~~ 添加输出引擎也支持第二个参数,用来表示配置信息,详细的配置请看下面介绍: ~~~ logs.SetLogger(logs.AdapterFile,`{"filename":"project.log","level":7,"maxlines":0,"maxsize":0,"daily":true,"maxdays":10,"color":true}`) ~~~ ~~~ beego.Emergency("this is emergency") //M beego.Alert("this is alert") //A beego.Critical("this is critical") //C beego.Error("this is error") //E beego.Warning("this is warning") //w beego.Notice("this is notice") //N beego.Informational("this is informational") //I beego.Debug("this is debug") //D ~~~ # 输出到文件 ~~~ func (this *AdminController) Post() { beego.BeeLogger.DelLogger("file") //an official log.Logger l := logs.GetLogger() l.Println("this is a message of http") //an official log.Logger with prefix ORM logs.GetLogger("ORM").Println("this is a message of orm") logs.Debug("my book is bought in the year of ", 2016) logs.Info("this %s cat is %v years old", "yellow", 3) logs.Warn("json is a type of kv like", map[string]int{"key": 2016}) logs.Error(1024, "is a very", "good game") logs.Critical("oh,crash") beego.SetLogger("file", `{"filename":"logs/test.log"}`) fmt.Println("=============") this.Ctx.WriteString("-------abc----------") } ~~~ # 其他 一般推荐使用通用方式进行日志,但依然支持单独声明来使用独立的日志 ~~~ package main import ( "github.com/astaxie/beego/logs" ) func main() { log := logs.NewLogger() log.SetLogger(logs.AdapterConsole) log.Debug("this is a debug message") } ~~~ ## 异步输出日志[](https://beego.me/docs/module/logs.md#%E5%BC%82%E6%AD%A5%E8%BE%93%E5%87%BA%E6%97%A5%E5%BF%97) 为了提升性能, 可以设置异步输出: ~~~ logs.Async() ~~~ 异步输出允许设置缓冲 chan 的大小 ~~~ logs.Async(1e3) ~~~ 还有其他的看文档吧