beego 日志处理

beego 的日志处理是基于 logs 模块搭建的,内置了一个变量 BeeLogger,默认已经是 logs.BeeLogger 类型,初始化了 console,也就是默认输出到 console。 使用入门 一般在程序中我们使用如下的方式进行输出: beego.Emergency("this is emergency") beego.Alert("this is alert") beego.Critical("this is critical") beego.Error("this is error") beego.Warning("this is warning") beego.Notice("this is notice") beego.Informational("this is informational") beego.Debug("this is debug") 设置输出 我们的程序往往期望把信息输出到 log 中,现在设置输出到文件很方便,如下所示: beego.SetLogger("file", `{"filename":"logs/test.log"}`) 更多详细的日志配置请查看 beego 日志配置 这个默认情况就会同时输出到两个地方,一个 console,一个 file,如果只想输出到文件,就需要调用删除操作: beego.BeeLogger.DelLogger("console") 设置级别 日志的级别如上所示的代码这样分为八个级别: LevelEmergency LevelAlert LevelCritical LevelError LevelWarning LevelNotice LevelInformational LevelDebug 级别依次降低,默认全部打印,但是一般我们在部署环境,可以通过设置级别设置日志级别: beego.SetLevel(beego.LevelInformational) 输出文件名和行号 日志默认不输出调用的文件名和文件行号,如果你期望输出调用的文件名和文件行号,可以如下设置 beego.SetLogFuncCall(true) 开启传入参数 true, 关闭传入参数 false, 默认是关闭的. 完整示例 func internalCalculationFunc(x, y int) (result int, err error) { beego.Debug("calculating z. x:", x, " y:", y) z := y switch { case x == 3: beego.Debug("x == 3") panic("Failure.") case y == 1: beego....

September 16, 2020

golang interface

interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服。 什么是interface 简单的说,interface是一组method签名的组合,我们通过interface来定义对象的一组行为。 我们前面一章最后一个例子中Student和Employee都能SayHi,虽然他们的内部实现不一样,但是那不重要,重要的是他们都能say hi 让我们来继续做更多的扩展,Student和Employee实现另一个方法Sing,然后Student实现方法BorrowMoney而Employee实现SpendSalary。 这样Student实现了三个方法:SayHi、Sing、BorrowMoney;而Employee实现了SayHi、Sing、SpendSalary。 上面这些方法的组合称为interface(被对象Student和Employee实现)。例如Student和Employee都实现了interface:SayHi和Sing,也就是这两个对象是该interface类型。而Employee没有实现这个interface:SayHi、Sing和BorrowMoney,因为Employee没有实现BorrowMoney这个方法。 interface类型 interface类型定义了一组方法,如果某个对象实现了某个接口的所有方法,则此对象就实现了此接口。详细的语法参考下面这个例子 type Human struct { name string age int phone string } type Student struct { Human //匿名字段Human school string loan float32 } type Employee struct { Human //匿名字段Human company string money float32 } //Human对象实现Sayhi方法 func (h *Human) SayHi() { fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone) } // Human对象实现Sing方法 func (h *Human) Sing(lyrics string) { fmt.Println("La la, la la la, la la la la la...", lyrics) } //Human对象实现Guzzle方法 func (h *Human) Guzzle(beerStein string) { fmt.Println("Guzzle Guzzle Guzzle...", beerStein) } // Employee重载Human的Sayhi方法 func (e *Employee) SayHi() { fmt....

September 10, 2020

beego 的日志级别

看 beego 的源码,beego 的日志分为7个级别: const ( LevelEmergency = iota LevelAlert LevelCritical LevelError LevelWarning LevelNotice LevelInformational LevelDebug ) 转换为 LevelEmergency = 0 LevelAlert = 1 LevelCritical = 2 LevelError = 3 LevelWarning = 4 LevelNotice = 5 LevelInformational = 6 LevelDebug = 7

September 9, 2020