第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 Cloud Run 上使用 Stackdriver 日志記錄

如何在 Cloud Run 上使用 Stackdriver 日志記錄

Go
喵喔喔 2023-08-07 10:56:34
我正在嘗試讓 stackdriver 日志記錄在 Google Cloud Run(完全托管)中運(yùn)行的簡(jiǎn)單 Go 應(yīng)用程序正常工作,但在 CloudRun 日志中看不到 stackdriver 條目。我根據(jù)“官方”stackdriver golang 示例創(chuàng)建了最簡(jiǎn)單的演示應(yīng)用程序Cloud Run 文檔指出不應(yīng)執(zhí)行任何其他操作來(lái)寫(xiě)入 stackdriver 日志我的服務(wù)使用默認(rèn)服務(wù)帳戶我正在使用 Go 1.13 編譯代碼(Dockerfile 是從Cloud Run 示例“按原樣”復(fù)制的)我嘗試過(guò)將其部署到不同的地區(qū),但沒(méi)有成功使用服務(wù)帳戶憑據(jù)在本地運(yùn)行容器時(shí),stackdriver 日志消息不會(huì)出現(xiàn)在本地終端或 stackdriver 控制臺(tái)中無(wú)論如何,在應(yīng)用程序啟動(dòng)時(shí),我只看到"Before stackdriver logging"后面的內(nèi)容,"After stackdriver logging"中間沒(méi)有其他消息\錯(cuò)誤以下是日志記錄代碼的一部分(使用上面的鏈接獲取完整源代碼、Dockerfile 以及構(gòu)建和運(yùn)行應(yīng)用程序的說(shuō)明):import (? ? "context"? ? "log"? ? "os"? ? ...? ? "cloud.google.com/go/compute/metadata"? ? "cloud.google.com/go/logging")func main() {? ? loggingClient, err := stackdriverClient()? ? ...? ? log.Println("Before stackdriver logging")? ? logger.StandardLogger(logging.Info).Println("Stackdriver log")? ? if err = logger.Flush(); err != nil {? ? ? ? log.Fatalf("Failed to flush client: %v", err)? ? }? ? if err = loggingClient.Close(); err != nil {? ? ? ? log.Fatalf("Failed to close client: %v", err)? ? }? ? log.Println("After stackdriver logging")? ? ...}func stackdriverClient() (client *logging.Client, err error) {? ? var projectID string? ? if projectID, err = metadata.ProjectID(); err == nil {? ? ? ? client, err = logging.NewClient(context.Background(), projectID)? ? }? ? return}
查看完整描述

2 回答

?
蝴蝶不菲

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

事實(shí)證明,日志條目已成功寫(xiě)入
,但logviewer Web UI中的默認(rèn) Cloud Run 過(guò)濾器不包含它們
下面的過(guò)濾器表達(dá)式可幫助我獲取所有日志:

resource.type?=?"project"?OR?resource.type?=?"cloud_run_revision"

(服務(wù)名稱(chēng)、地點(diǎn)、嚴(yán)重程度省略)

https://img3.sycdn.imooc.com/64d05db2000199cf06100065.jpg

“stdout\stderr”日志條目匹配resource.type="cloud_run_revision",而 stackdriver 日志條目匹配resource.type="project"

查看完整回答
反對(duì) 回復(fù) 2023-08-07
?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個(gè)贊

由于 Cloud Run 已與 Cloud Logging 集成,因此無(wú)需使用 Go 客戶端庫(kù)。我們?cè)?Cloud Run 上運(yùn)行所有 gRPC 服務(wù),并使用以下內(nèi)容來(lái)確保 Cloud Logging 中的日志格式正確:


package main


import (

? ? "github.com/rs/zerolog"

? ? "github.com/rs/zerolog/log"

? ? "os"

)


// initializeLogging sets up the logging configuration for the service.

// Invoke this method in your Init() method.

func initializeLogging() {

? ? // Set logging options for production development

? ? if os.Getenv("ENV") != "DEV" {

? ? ? ? // change the level field name to ensure these are parsed correctly in Stackdriver

? ? ? ? zerolog.LevelFieldName = "severity"

? ? ? ? // UNIX Time is faster and smaller than most timestamps

? ? ? ? zerolog.TimeFieldFormat = zerolog.TimeFormatUnix

? ? } else {

? ? ? ? // Set logging options for local development

? ? ? ? log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})

? ? ? ? zerolog.SetGlobalLevel(zerolog.DebugLevel)

? ? }

? ??

? ? // Example log

? ? log.Info().Msg("This is how you log at Info level")

}


然后日志會(huì)很好地顯示以供本地開(kāi)發(fā)。


如果您不想使用任何第三方日志記錄庫(kù),一個(gè)簡(jiǎn)單的方法是構(gòu)造您自己的 Entry 對(duì)象。


package main


import (

? ? "context"

? ? "encoding/json"

? ? "fmt"

? ? "google.golang.org/grpc"

? ? "google.golang.org/grpc/metadata"

? ? "log"

? ? "os"

? ? "strings"

)


// LogSeverity is used to map the logging levels consistent with Google Cloud Logging.

type LogSeverity string


const (

? ? // LogSeverity_DEBUG Debug or trace information.

? ? LogSeverity_DEBUG LogSeverity = "DEBUG"

? ? // LogSeverity_INFO Routine information, such as ongoing status or performance.

? ? LogSeverity_INFO LogSeverity = "INFO"

? ? // LogSeverity_NOTICE Normal but significant events, such as start up, shut down, or

? ? // a configuration change.

? ? LogSeverity_NOTICE LogSeverity = "NOTICE"

? ? // LogSeverity_WARNING Warning events might cause problems.

? ? LogSeverity_WARNING LogSeverity = "WARNING"

? ? // LogSeverity_ERROR Error events are likely to cause problems.

? ? LogSeverity_ERROR LogSeverity = "ERROR"

? ? // LogSeverity_CRITICAL Critical events cause more severe problems or outages.

? ? LogSeverity_CRITICAL LogSeverity = "CRITICAL"

? ? // LogSeverity_ALERT A person must take an action immediately.

? ? LogSeverity_ALERT LogSeverity = "ALERT"

? ? // LogSeverity_EMERGENCY One or more systems are unusable.

? ? LogSeverity_EMERGENCY LogSeverity = "EMERGENCY"

)


// Entry defines a log entry.

// If logs are provided in this format, Google Cloud Logging automatically

// parses the attributes into their LogEntry format as per

// https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry which then automatically

// makes the logs available in Google Cloud Logging and Tracing.

type Entry struct {

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

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

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

? ? // To extend details sent to the logs, you may add the attributes here.

? ? //MyAttr1 string `json:"component,omitempty"`

}


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

func (e Entry) String() string {


? ? // Defaults to INFO level.

? ? if e.Severity == "" {

? ? ? ? e.Severity = LogSeverity_INFO

? ? }


? ? // if Development is local then print out all logs

? ? if os.Getenv("ENV") == "LOCAL" {

? ? ? ? var prefix string

? ? ? ? switch e.Severity {

? ? ? ? case LogSeverity_DEBUG:

? ? ? ? ? ? prefix = colorize("DBG:? ? ? ", 90)

? ? ? ? case LogSeverity_INFO:

? ? ? ? ? ? prefix = colorize("INFO:? ? ?", 32)

? ? ? ? case LogSeverity_NOTICE:

? ? ? ? ? ? prefix = colorize("NOTICE:? ?", 34)

? ? ? ? case LogSeverity_WARNING:

? ? ? ? ? ? prefix = colorize("WARNING:? ", 33)

? ? ? ? case LogSeverity_ERROR:

? ? ? ? ? ? prefix = colorize("ERROR:? ? ", 31)

? ? ? ? case LogSeverity_ALERT:

? ? ? ? ? ? prefix = colorize("ALERT:? ? ", 91)

? ? ? ? case LogSeverity_CRITICAL:

? ? ? ? ? ? prefix = colorize("CRITICAL: ", 41)

? ? ? ? case LogSeverity_EMERGENCY:

? ? ? ? ? ? prefix = colorize("EMERGENCY:", 101)

? ? ? ? }

? ? ? ? return prefix + " " + e.Message

? ? } else {

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

? ? ? ? if err != nil {

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

? ? ? ? }

? ? ? ? return string(out)

? ? }

}


// colorize returns the string s wrapped in ANSI code c

// Codes available at https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

func colorize(s interface{}, c int) string {

? ? return fmt.Sprintf("\x1b[%dm%v\x1b[0m", c, s)

}


使用 Google Cloud 的特殊字段可以與其 Cloud Logging 產(chǎn)品更緊密地集成。



查看完整回答
反對(duì) 回復(fù) 2023-08-07
  • 2 回答
  • 0 關(guān)注
  • 158 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)