Golang語(yǔ)言中的Prometheus的日志模塊使用案例代碼編寫(xiě)
一.源代碼編寫(xiě)
package main import ( "fmt" "os" "path/filepath" "time" "github.com/alecthomas/kingpin/v2" "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/prometheus/common/promlog" promlogflag "github.com/prometheus/common/promlog/flag" "github.com/prometheus/common/version" ) var ( videos = "https://space.bilibili.com/600805398/channel/series" docs = "https://www.cnblogs.com/yinzhengjie" // 命令行解析 app = kingpin.New(filepath.Base(os.Args[0]), fmt.Sprintf("yinzhengjie-devops'server Program, docs: %s, videos: %s", docs, videos)) // 指定配置文件 configFile = app.Flag("config.file", "configuration file path").Short('c').Default("yinzhengjie-devops-server.yaml").String() ) // Logger用于設(shè)置prometheus的Logger, func Logger(config *promlog.Config) log.Logger { var ( l log.Logger le level.Option ) // 設(shè)置日志的輸出格式 if config.Format.String() == "logfmt" { l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)) } else { l = log.NewJSONLogger(log.NewSyncWriter(os.Stderr)) } // 設(shè)置日志級(jí)別 switch config.Level.String() { case "debug": le = level.AllowDebug() case "info": le = level.AllowInfo() case "warn": le = level.AllowWarn() case "error": le = level.AllowError() } l = level.NewFilter(l, le) // CST可視為美國(guó)、澳大利亞、古巴或中國(guó)的標(biāo)準(zhǔn)時(shí)間,CST可以為如下4個(gè)不同的時(shí)區(qū)的縮寫(xiě): // 美國(guó)中部時(shí)間:Central Standard Time (USA) UT-6:00 // 澳大利亞中部時(shí)間:Central Standard Time (Australia) UT+9:30 // 中國(guó)標(biāo)準(zhǔn)時(shí)間:China Standard Time UT+8:00 // 古巴標(biāo)準(zhǔn)時(shí)間:Cuba Standard Time UT-4:00 // // 重新設(shè)置一下時(shí)區(qū),否則是UTC時(shí)間,建議設(shè)置CST時(shí)區(qū),我們以北京的東八區(qū)時(shí)間為準(zhǔn)。 l = log.With(l, "cts", log.TimestampFormat( func() time.Time { return time.Now().Local() }, "2006-01-02T15:04:05.000Z08:00", ), "caller", log.DefaultCaller) return l } func main() { // 版本信息 // app.Version("v1.0") app.Version(version.Print("yinzhengjie-devops-server")) // 幫助信息 app.HelpFlag.Short('h') promlogConfig := promlog.Config{} promlogflag.AddFlags(app, &promlogConfig) // 強(qiáng)制解析 kingpin.MustParse(app.Parse(os.Args[1:])) fmt.Printf("configFile: %s\n", *configFile) // 設(shè)置prometheus的logger var logger log.Logger = Logger(&promlogConfig) // 輸出日志事件時(shí)需要指定日志級(jí)別,此處我指定的日志級(jí)別為"info" level.Info(logger).Log( // 注意,寫(xiě)入的數(shù)據(jù)成對(duì)出現(xiàn),比如下面的案例我就寫(xiě)了5對(duì)測(cè)試數(shù)據(jù)。 "Name", "尹正杰", "Hobby", "Golang K8S Docker", "blog", "https://www.cnblogs.com/yinzhengjie", "cfg", *configFile, "age", 18, ) }
二.編譯
go build -o server -ldflags "-X 'github.com/prometheus/common/version.BuildUser=y1053419035@qq.com' -X 'github.com/prometheus/common/version.BuildDate=`date`' -X 'github.com/prometheus/common/version.Version=v0.2'" src/models/server/server.go
三.測(cè)試
1.查看服務(wù)的版本信息 ./server --version 2.指定程序的配置文件 ./server -c /etc/nginx/conf.d/games.conf 3.查看程序的幫助信息 ./server -h 4.不指定任何參數(shù) ./server 5.指定日志輸出格式 ./server --log.format=json 6.同時(shí)指定多個(gè)參數(shù) ./server --log.format=json -c /etc/nginx/nginx.conf
當(dāng)你的才華還撐不起你的野心的時(shí)候,你就應(yīng)該靜下心來(lái)學(xué)習(xí)。當(dāng)你的能力還駕馭不了你的目標(biāo)的時(shí)候,你就應(yīng)該沉下心來(lái)歷練。問(wèn)問(wèn)自己,想要怎樣的人生。 歡迎交流學(xué)習(xí)技術(shù)交流,個(gè)人微信: "JasonYin2020"(添加時(shí)請(qǐng)備注來(lái)源及意圖備注) 作者: 尹正杰, 博客: https://www.cnblogs.com/yinzhengjie/p/18351921
到此這篇關(guān)于Golang語(yǔ)言中的Prometheus的日志模塊使用案例代碼編寫(xiě)的文章就介紹到這了,更多相關(guān)Golang Prometheus日志模塊使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang實(shí)現(xiàn)簡(jiǎn)單的udp協(xié)議服務(wù)端與客戶端示例
這篇文章主要介紹了golang實(shí)現(xiàn)簡(jiǎn)單的udp協(xié)議服務(wù)端與客戶端,結(jié)合實(shí)例形式分析了基于UDP協(xié)議的數(shù)據(jù)傳輸相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-07-07Golang極簡(jiǎn)入門(mén)教程(二):方法和接口
這篇文章主要介紹了Golang極簡(jiǎn)入門(mén)教程(二):方法和接口,本文同時(shí)講解了錯(cuò)誤、匿名域等內(nèi)容,需要的朋友可以參考下2014-10-10golang的時(shí)區(qū)和神奇的time.Parse的使用方法
這篇文章主要介紹了golang的時(shí)區(qū)和神奇的time.Parse的使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Golang中interface轉(zhuǎn)string輸出打印方法
這篇文章主要給大家介紹了關(guān)于Golang中interface轉(zhuǎn)string輸出打印的相關(guān)資料,在go語(yǔ)言中interface轉(zhuǎn)string可以直接使用fmt提供的fmt函數(shù),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02使用systemd部署和守護(hù)golang應(yīng)用程序的操作方法
systemd是一個(gè)流行的守護(hù)進(jìn)程管理器,可以輕松管理服務(wù)的啟動(dòng)、停止、重啟等操作,讓我們的應(yīng)用程序始終保持在線,本文介紹了如何使用systemd部署和守護(hù)golang應(yīng)用程序,感興趣的朋友一起看看吧2023-10-10