Golang語言中的Prometheus的日志模塊使用案例代碼編寫
一.源代碼編寫
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è)置日志級別 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可視為美國、澳大利亞、古巴或中國的標(biāo)準(zhǔn)時間,CST可以為如下4個不同的時區(qū)的縮寫: // 美國中部時間:Central Standard Time (USA) UT-6:00 // 澳大利亞中部時間:Central Standard Time (Australia) UT+9:30 // 中國標(biāo)準(zhǔn)時間:China Standard Time UT+8:00 // 古巴標(biāo)準(zhǔn)時間:Cuba Standard Time UT-4:00 // // 重新設(shè)置一下時區(qū),否則是UTC時間,建議設(shè)置CST時區(qū),我們以北京的東八區(qū)時間為準(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) // 輸出日志事件時需要指定日志級別,此處我指定的日志級別為"info" level.Info(logger).Log( // 注意,寫入的數(shù)據(jù)成對出現(xiàn),比如下面的案例我就寫了5對測試數(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
三.測試
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ù) ./server --log.format=json -c /etc/nginx/nginx.conf
當(dāng)你的才華還撐不起你的野心的時候,你就應(yīng)該靜下心來學(xué)習(xí)。當(dāng)你的能力還駕馭不了你的目標(biāo)的時候,你就應(yīng)該沉下心來歷練。問問自己,想要怎樣的人生。 歡迎交流學(xué)習(xí)技術(shù)交流,個人微信: "JasonYin2020"(添加時請備注來源及意圖備注) 作者: 尹正杰, 博客: https://www.cnblogs.com/yinzhengjie/p/18351921
到此這篇關(guān)于Golang語言中的Prometheus的日志模塊使用案例代碼編寫的文章就介紹到這了,更多相關(guān)Golang Prometheus日志模塊使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang實現(xiàn)簡單的udp協(xié)議服務(wù)端與客戶端示例
這篇文章主要介紹了golang實現(xiàn)簡單的udp協(xié)議服務(wù)端與客戶端,結(jié)合實例形式分析了基于UDP協(xié)議的數(shù)據(jù)傳輸相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2016-07-07golang的時區(qū)和神奇的time.Parse的使用方法
這篇文章主要介紹了golang的時區(qū)和神奇的time.Parse的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Golang中interface轉(zhuǎn)string輸出打印方法
這篇文章主要給大家介紹了關(guān)于Golang中interface轉(zhuǎn)string輸出打印的相關(guān)資料,在go語言中interface轉(zhuǎn)string可以直接使用fmt提供的fmt函數(shù),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-02-02使用systemd部署和守護(hù)golang應(yīng)用程序的操作方法
systemd是一個流行的守護(hù)進(jìn)程管理器,可以輕松管理服務(wù)的啟動、停止、重啟等操作,讓我們的應(yīng)用程序始終保持在線,本文介紹了如何使用systemd部署和守護(hù)golang應(yīng)用程序,感興趣的朋友一起看看吧2023-10-10