iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 19
0
Modern Web

BeeGo系列 第 19

Logging

今天來介紹 Logging,使用 Logging ,我們可以印出訊息,藉著這些訊息來進行除錯。

使用

在 import "github.com/astaxie/beego" 以後,beego 有這些函式可以使用:

  • Debug()
  • Info()
  • Notice()
  • Warn()
  • Error()
  • Critical()
  • Alert()
  • Emergency()

基本上對應到 syslog 的 Severity level。

beego.Debug("this is debug")
beego.Info("this is info")
beego.Notice("this is notice")
beego.Warn("this is warn")
beego.Error("this is error")
beego.Critical("this is critical")
beego.Alert("this is alert")
beego.Emergency("this is emergency")

設定

基本上,這些 log 會輸出到 console 上,要輸出到檔案,可以在 main.go 裡用 SetLogger() 來指定。

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

如果不要輸出到 console,可以用 DelLogger("console")

beego.BeeLogger.DelLogger("console")

除了 console 與 file 之外,也可以將 log 以 SMTP/Slack 寄送。

// SMTP
import (
	"github.com/astaxie/beego/logs"
)

beego.SetLogger("smtp", `{"username":"user@example.com","password":"xxxxxxxx","host":"smtp.example.com:587","sendTos":["user@example.com"]}`)
beego.Critical("sendmail critical")  // 此時就會寄出!!

// Slack
import (
	"github.com/astaxie/beego/logs"
)

beego.SetLogger("slack", `{"webhookurl":"https://slack_webhook_url"}`)
beego.Debug("this is debug message")

其實看了原始碼以後,會發現還有 multifile, conn, es, jianliao, alils 這幾個可以使用:

  • multifile:可以寫入多個 log 檔案
  • conn:可以把 log 丟到指定 host 的 port。
  • es:可以把 log 丟到 ElasticSearch
  • alils:可以把 log 丟到阿里雲的 Log 服務
  • jianliao:這個我查了半天都沒查到,註解也沒有說明。

至於用法,我就沒有去深究了。

參考資料


上一篇
Multiple language
下一篇
Docker
系列文
BeeGo30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言