av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

創(chuàng)新互聯(lián)GoFrame教程:GoFrame日志組件-Handler

從?v2.0?版本開始,?glog?組件提供了超級(jí)強(qiáng)大的、可自定義日志處理的?Handler?特性。?Handler?采用了中間件設(shè)計(jì)方式,開發(fā)者可以為日志對(duì)象注冊(cè)多個(gè)處理?Handler?,也可以在?Handler?中覆蓋默認(rèn)的日志組件處理邏輯。

相關(guān)定義

Handler方法定義

// Handler is function handler for custom logging content outputs.
type Handler func(ctx context.Context, in *HandlerInput)

可以看到第二個(gè)參數(shù)為日志處理的日志信息,并且為指針類型,意味著在?Handler?中可以修改該參數(shù)的任意屬性信息,并且修改后的內(nèi)容將會(huì)傳遞給下一個(gè)?Handler?。

Handler參數(shù)定義

// HandlerInput is the input parameter struct for logging Handler.
type HandlerInput struct {
	Logger       *Logger         // Logger.
	Ctx          context.Context // Context.
	Buffer       *bytes.Buffer   // Buffer for logging content outputs.
	Time         time.Time       // Logging time, which is the time that logging triggers.
	TimeFormat   string          // Formatted time string, like "2016-01-09 12:00:00".
	Color        int             // Using color, like COLOR_RED, COLOR_BLUE, etc.
	Level        int             // Using level, like LEVEL_INFO, LEVEL_ERRO, etc.
	LevelFormat  string          // Formatted level string, like "DEBU", "ERRO", etc.
	CallerFunc   string          // The source function name that calls logging.
	CallerPath   string          // The source file path and its line number that calls logging.
	CtxStr       string          // The retrieved context value string from context.
	Prefix       string          // Custom prefix string for logging content.
	Content      string          // Content is the main logging content that passed by you.
	IsAsync      bool            // IsAsync marks it is in asynchronous logging.
	handlerIndex int             // Middleware handling index for internal usage.
}

開發(fā)者有兩種方式修改默認(rèn)的日志輸出內(nèi)容:一種是直接修改?HandlerInput?中的屬性信息,然后繼續(xù)執(zhí)行?in.Next()?;另一種自定義日志輸出內(nèi)容,將日志內(nèi)容寫入到?Buffer?中即可。

Handler注冊(cè)到Logger方法

// SetHandlers sets the logging handlers for current logger.
func (l *Logger) SetHandlers(handlers ...Handler)

使用示例

我們來看兩個(gè)示例便于更快速了解?Handler?的使用。

示例1. 將日志輸出轉(zhuǎn)換為Json格式輸出

在本示例中,我們采用了前置中間件的設(shè)計(jì),通過自定義?Handler?將日志內(nèi)容輸出格式修改為了?JSON?格式。

package main

import (
	"context"
	"encoding/json"
	"os"

	"github.com/GOgf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/glog"
	"github.com/gogf/gf/v2/text/gstr"
)

// JsonOutputsForLogger is for JSON marshaling in sequence.
type JsonOutputsForLogger struct {
	Time    string `json:"time"`
	Level   string `json:"level"`
	Content string `json:"content"`
}

// LoggingJsonHandler is a example handler for logging JSON format content.
var LoggingJsonHandler glog.Handler = func(ctx context.Context, in *glog.HandlerInput) {
	jsonForLogger := JsonOutputsForLogger{
		Time:    in.TimeFormat,
		Level:   gstr.Trim(in.LevelFormat, "[]"),
		Content: gstr.Trim(in.Content),
	}
	jsonBytes, err := json.Marshal(jsonForLogger)
	if err != nil {
		_, _ = os.Stderr.WriteString(err.Error())
		return
	}
	in.Buffer.Write(jsonBytes)
	in.Buffer.WriteString("\n")
	in.Next()
}

func main() {
	g.Log().SetHandlers(LoggingJsonHandler)
	ctx := context.TODO()
	g.Log().Debug(ctx, "Debugging...")
	g.Log().Warning(ctx, "It is warning info")
	g.Log().Error(ctx, "Error occurs, please have a check")
}

可以看到,我們可以在?Handler?中通過?Buffer?屬性操作來控制輸出的日志內(nèi)容。如果在所有的前置中間件?Handler?處理后?Buffer?內(nèi)容為空,那么繼續(xù)?Next?執(zhí)行后將會(huì)執(zhí)行日志中間件默認(rèn)的?Handler?邏輯。執(zhí)行本示例的代碼后,終端輸出:

{"time":"2021-12-31 11:03:25.438","level":"DEBU","content":"Debugging..."}
{"time":"2021-12-31 11:03:25.438","level":"WARN","content":"It is warning info"}
{"time":"2021-12-31 11:03:25.438","level":"ERRO","content":"Error occurs, please have a check \nStack:\n1.  main.main\n    C:/hailaz/test/main.go:42"}

示例2. 將內(nèi)容輸出到第三方日志搜集服務(wù)中

在本示例中,我們采用了后置中間件的設(shè)計(jì),通過自定義?Handler?將日志內(nèi)容輸出一份到第三方graylog日志搜集服務(wù)中,并且不影響原有的日志輸出處理。

?Graylog是與ELK可以相提并論的一款集中式日志管理方案,支持?jǐn)?shù)據(jù)收集、檢索、可視化Dashboard。在本示例中使用到了一個(gè)簡(jiǎn)單的第三方?graylog?客戶端組件。

package main

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/glog"
	gelf "github.com/robertkowalski/graylog-golang"
)

var grayLogClient = gelf.New(gelf.Config{
	GraylogPort:     80,
	GraylogHostname: "graylog-host.com",
	Connection:      "wan",
	MaxChunkSizeWan: 42,
	MaxChunkSizeLan: 1337,
})

// LoggingGrayLogHandler is an example handler for logging content to remote GrayLog service.
var LoggingGrayLogHandler glog.Handler = func(ctx context.Context, in *glog.HandlerInput) {
	in.Next()
	grayLogClient.Log(in.Buffer.String())
}

func main() {
	g.Log().SetHandlers(LoggingGrayLogHandler)
	ctx := context.TODO()
	g.Log().Debug(ctx, "Debugging...")
	g.Log().Warning(ctx, "It is warning info")
	g.Log().Error(ctx, "Error occurs, please have a check")
	glog.Print(ctx, "test log")
}

網(wǎng)站題目:創(chuàng)新互聯(lián)GoFrame教程:GoFrame日志組件-Handler
網(wǎng)頁(yè)路徑:http://uogjgqi.cn/article/cdephip.html
掃二維碼與項(xiàng)目經(jīng)理溝通

我們?cè)谖⑿派?4小時(shí)期待你的聲音

解答本文疑問/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流