新聞中心
?Writer?接口是最底層的?IO?寫入接口,如果業(yè)務(wù)需要自定義日志內(nèi)容打印,建議使用?Handler?特性。

成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、雁山網(wǎng)絡(luò)推廣、成都微信小程序、雁山網(wǎng)絡(luò)營(yíng)銷、雁山企業(yè)策劃、雁山品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供雁山建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
自定義Writer接口
?glog?模塊實(shí)現(xiàn)了標(biāo)準(zhǔn)輸出以及文件輸出的日志內(nèi)容打印。當(dāng)然,開發(fā)者也可以通過自定義?io.Writer?接口實(shí)現(xiàn)自定義的日志內(nèi)容輸出。?io.Writer?是標(biāo)準(zhǔn)庫(kù)提供的內(nèi)容輸出接口,其定義如下:
type Writer interface {
Write(p []byte) (n int, err error)
}我們可以通過?SetWriter?方法或者鏈?zhǔn)椒椒?To?來實(shí)現(xiàn)自定義?Writer?輸出,開發(fā)者可以在該?Writer?中實(shí)現(xiàn)定義的操作,也可以在其中整合其他的模塊功能。
此外,?glog.Logger?對(duì)象已經(jīng)實(shí)現(xiàn)了?io.Writer?接口,因此開發(fā)者可以非常方便地將?glog?整合使用到其他的模塊中。
示例1,實(shí)現(xiàn)日志HOOK
在該示例中,我們實(shí)現(xiàn)了一個(gè)自定義的?Writer?對(duì)象?MyWriter?,在該對(duì)象實(shí)現(xiàn)的?Writer?接口中我們對(duì)日志內(nèi)容進(jìn)行判斷,如果出現(xiàn)了?PANI?或者?FATA?錯(cuò)誤,那么表示是非常嚴(yán)重的錯(cuò)誤,該接口將會(huì)第一時(shí)間通過?HTTP?接口告知?Monitor?監(jiān)控服務(wù)。隨后再將日志內(nèi)容通過?glog?模塊按照配置寫入到文件和標(biāo)準(zhǔn)輸出。
package main
import (
"context"
"fmt"
"github.com/GOgf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/text/gregex"
)
type MyWriter struct {
logger *glog.Logger
}
func (w *MyWriter) Write(p []byte) (n int, err error) {
var (
s = string(p)
ctx = context.Background()
)
if gregex.IsMatchString(`PANI|FATA`, s) {
fmt.Println("SERIOUS ISSUE OCCURRED!! I'd better tell monitor in first time!")
g.Client().PostContent(ctx, "http://monitor.mydomain.com", s)
}
return w.logger.Write(p)
}
func main() {
var ctx = context.Background()
glog.SetWriter(&MyWriter{
logger: glog.New(),
})
glog.Fatal(ctx, "FATAL ERROR")
}執(zhí)行后,輸出結(jié)果為:
SERIOUS ISSUE OCCURRED!! I'd better tell monitor in first time!
2019-05-23 20:14:49.374 [FATA] FATAL ERROR
Stack:
1. /Users/john/Workspace/Go/GOPATH/src/github.com/gogf/gf/v2/geg/os/glog/glog_writer_hook.go:27示例2,整合graylog
假如我們需要輸出日志到文件及標(biāo)準(zhǔn)輸出,并且同時(shí)也需要輸出日志到?Graylog?,很明顯這個(gè)也是需要自定義?Writer?才能實(shí)現(xiàn)。當(dāng)然同理,我們也可以自定義輸出到其他的日志收集組件或者數(shù)據(jù)庫(kù)中。
?Graylog是與?ELK可以相提并論的一款集中式日志管理方案,支持?jǐn)?shù)據(jù)收集、檢索、可視化Dashboard?。
示例代碼:
package main
import (
"context"
"github.com/gogf/gf/v2/os/glog"
"github.com/robertkowalski/graylog-golang"
)
type MyGrayLogWriter struct {
gelf *gelf.Gelf
logger *glog.Logger
}
func (w *MyGrayLogWriter) Write(p []byte) (n int, err error) {
w.gelf.Send(p)
return w.logger.Write(p)
}
func main() {
var ctx = context.Background()
glog.SetWriter(&MyGrayLogWriter{
logger : glog.New(),
gelf : gelf.New(gelf.Config{
GraylogPort : 80,
GraylogHostname : "graylog-host.com",
Connection : "wan",
MaxChunkSizeWan : 42,
MaxChunkSizeLan : 1337,
}),
})
glog.Println(ctx, "test log")
} 新聞標(biāo)題:創(chuàng)新互聯(lián)GoFrame教程:GoFrame 日志組件-Writer接口
URL地址:http://www.5511xx.com/article/cosphsh.html


咨詢
建站咨詢
