亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我應該為 GAE 中的 Go 使用哪個日志記錄庫?

我應該為 GAE 中的 Go 使用哪個日志記錄庫?

Go
四季花海 2023-06-01 09:56:14
我為GAE找到了兩種go庫:“google.golang.org/appengine/log”“cloud.google.com/go/logging”我應該使用哪一個?順便說一下,我在我的應用程序中同時使用了兩個日志記錄庫。在本地開發模式下,我可以看到這樣的日志。2019/01/08 06:57:34 INFO: Search keyword="test" idOnly=bool 2019/01/08 06:57:34 INFO: Search:"test"但是當我部署到生產 GAE 時,我看不到任何日志。無論通過resource.type="gae_app" resource.labels.module_id="default"或者 gcloud 命令 gcloud app logs tail -s default
查看完整描述

2 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

如果您希望日志出現在 Stackdriver Logging 中,正確的方法是使用“google.golang.org/appengine/log”包。

但是,根據Go1.11 運行時的文檔,建議不要使用 App Engine 特定的 API 并使用 Google Cloud 客戶端庫。

關于日志記錄,這意味著不使用“google.golang.org/appengine/log”,推薦的方法是使用“log”包。一個例子:

應用程序.yaml

runtime:?go111

你好去

package main


import (

? ? ? ? "fmt"

? ? ? ? "log"

? ? ? ? "net/http"

? ? ? ? "os"

)


func main() {

? ? ? ? http.HandleFunc("/", indexHandler)


? ? ? ? port := os.Getenv("PORT")

? ? ? ? if port == "" {

? ? ? ? ? ? ? ? port = "8080"

? ? ? ? }


? ? ? ? log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))

}


func indexHandler(w http.ResponseWriter, r *http.Request) {

? ? ? ? //Create the log and write it

? ? ? ? log.Printf("Hello world!")

? ? ? ? fmt.Fprint(w, "Log written in Stackdriver!")

}

此日志將顯示在 Stackdriver Logging 中的以下位置:


resource.type="gae_app"

resource.labels.module_id="default"

logName="projects/<YOUR_PROJECT_NAME>/logs/stderr"

stderr或者通過在日志過濾器下拉列表中選擇。


但是,如果您愿意,您仍然可以使用“google.golang.org/appengine/log”包,但您還必須添加“google.golang.org/appengine”包,并將入口點添加appengine.Main()到main()功能。


查看完整回答
反對 回復 2023-06-01
?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

您也可以采用結構化日志記錄路線,您不依賴于上述客戶端庫。

// Entry defines a log entry.

type Entry struct {

? ? Message? string `json:"message"`

? ? Severity string `json:"severity,omitempty"`

? ? Trace? ? string `json:"logging.googleapis.com/trace,omitempty"`


? ? // Cloud Log Viewer allows filtering and display of this as `jsonPayload.component`.

? ? Component string `json:"component,omitempty"`

}


// String renders an entry structure to the JSON format expected by Cloud Logging.

func (e Entry) String() string {

? ? if e.Severity == "" {

? ? ? ? e.Severity = "INFO"

? ? }

? ? out, err := json.Marshal(e)

? ? if err != nil {

? ? ? ? log.Printf("json.Marshal: %v", err)

? ? }

? ? return string(out)

}

然后使用內置log包登錄:


log.Println(Entry{

? ? ? ? Severity:? "NOTICE",

? ? ? ? Message:? ?"This is the default display field.",

? ? ? ? Component: "arbitrary-property",

? ? ? ? Trace:? ? ?trace,

? ? })

查看完整回答
反對 回復 2023-06-01
  • 2 回答
  • 0 關注
  • 152 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號