beego日志處理

2023-11-20 18:06 更新

beego 之前介紹的時(shí)候說(shuō)過(guò)是基于幾個(gè)模塊搭建的,beego 的日志處理是基于 logs 模塊搭建的,內(nèi)置了一個(gè)變量 BeeLogger,默認(rèn)已經(jīng)是 logs.BeeLogger 類型,初始化了 console,也就是默認(rèn)輸出到 console。

使用入門(mén)

一般在程序中我們使用如下的方式進(jìn)行輸出:

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")

設(shè)置輸出

我們的程序往往期望把信息輸出到 log 中,現(xiàn)在設(shè)置輸出到文件很方便,如下所示:

beego.SetLogger("file", `{"filename":"logs/test.log"}`)

更多詳細(xì)的日志配置請(qǐng)查看日志配置

這個(gè)默認(rèn)情況就會(huì)同時(shí)輸出到兩個(gè)地方,一個(gè) console,一個(gè) file,如果只想輸出到文件,就需要調(diào)用刪除操作:

beego.BeeLogger.DelLogger("console")

設(shè)置級(jí)別

日志的級(jí)別如上所示的代碼這樣分為八個(gè)級(jí)別:

LevelEmergency
LevelAlert
LevelCritical
LevelError
LevelWarning
LevelNotice
LevelInformational
LevelDebug

級(jí)別依次降低,默認(rèn)全部打印,但是一般我們?cè)诓渴瓠h(huán)境,可以通過(guò)設(shè)置級(jí)別設(shè)置日志級(jí)別:

beego.SetLevel(beego.LevelInformational)

輸出文件名和行號(hào)

日志默認(rèn)不輸出調(diào)用的文件名和文件行號(hào),如果你期望輸出調(diào)用的文件名和文件行號(hào),可以如下設(shè)置

beego.SetLogFuncCall(true)

開(kāi)啟傳入?yún)?shù) true, 關(guān)閉傳入?yún)?shù) false, 默認(rèn)是關(guān)閉的.

完整示例

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.Debug("y == 1")
        return 0, errors.New("Error!")
    case y == 2:
        beego.Debug("y == 2")
        z = x
    default:
        beego.Debug("default")
        z += x
    }
    retVal := z - 3
    beego.Debug("Returning ", retVal)

    return retVal, nil
}

func processInput(input inputData) {
    defer func() {
        if r := recover(); r != nil {
            beego.Error("Unexpected error occurred: ", r)
            outputs <- outputData{result: 0, error: true}
        }
    }()
    beego.Informational("Received input signal. x:", input.x, " y:", input.y)

    res, err := internalCalculationFunc(input.x, input.y)
    if err != nil {
        beego.Warning("Error in calculation:", err.Error())
    }

    beego.Informational("Returning result: ", res, " error: ", err)
    outputs <- outputData{result: res, error: err != nil}
}

func main() {
    inputs = make(chan inputData)
    outputs = make(chan outputData)
    criticalChan = make(chan int)
    beego.Informational("App started.")

    go consumeResults(outputs)
    beego.Informational("Started receiving results.")

    go generateInputs(inputs)
    beego.Informational("Started sending signals.")

    for {
        select {
        case input := <-inputs:
            processInput(input)
        case <-criticalChan:
            beego.Critical("Caught value from criticalChan: Go shut down.")
            panic("Shut down due to critical fault.")
        }
    }
}
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)