99re热视频这里只精品,久久久天堂国产精品女人,国产av一区二区三区,久久久精品成人免费看片,99久久精品免费看国产一区二区三区

beego日志處理

2023-11-20 18:06 更新

beego 之前介紹的時候說過是基于幾個模塊搭建的,beego 的日志處理是基于 logs 模塊搭建的,內(nèi)置了一個變量 BeeLogger,默認已經(jīng)是 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 中,現(xiàn)在設置輸出到文件很方便,如下所示:

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

更多詳細的日志配置請查看日志配置

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

beego.BeeLogger.DelLogger("console")

設置級別

日志的級別如上所示的代碼這樣分為八個級別:

LevelEmergency
LevelAlert
LevelCritical
LevelError
LevelWarning
LevelNotice
LevelInformational
LevelDebug

級別依次降低,默認全部打印,但是一般我們在部署環(huán)境,可以通過設置級別設置日志級別:

beego.SetLevel(beego.LevelInformational)

輸出文件名和行號

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

beego.SetLogFuncCall(true)

開啟傳入?yún)?shù) true, 關(guān)閉傳入?yún)?shù) false, 默認是關(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)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號