<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 一、golang日志庫 ## ? ? ? ?1.1 golang日志庫簡介 ? ? ? ? ?golang標準庫的日志框架非常簡單,僅僅提供了print,panic和fatal三個函數。對于更精細的日志級別、日志文件分割,以及日志分發等方面,并沒有提供支持。所以,催生了很多第三方的日志庫。但是,在golang的世界里,沒有一個日志庫像slf4j那樣在Java中具有絕對統治地位。在golang的世界,流行的日志框架包括logrus、zap、zerolog、seelog等。? ? ? ? ?logrus是目前Github上star數量最多的日志庫,目前(2018.08,下同)star數量為8119,fork數為1031。logrus功能強大,性能高效,而且具有高度靈活性,提供了自定義插件的功能。很多開源項目,如docker,prometheus等,都是用了logrus來記錄其日志。 ? ? ? ? zap是Uber推出的一個快速、結構化的分級日志庫。具有強大的ad-hoc分析功能,并且具有靈活的儀表盤。zap目前在GitHub上的star數量約為4.3k。? ? ? ? ? seelog提供了靈活的異步調度、格式化和過濾功能。目前在GitHub上的star數量也有約1.1k。? ## ? ? ? ? 1.2 golang logrus的GitHub地址 ? ? ? ? ? ?logrus的GitHub地址? ??[https://github.com/sirupsen/logrus](https://github.com/sirupsen/logrus) ? ? ? ? ? ?lfshook的GitHub地址? ??[https://github.com/rifflock/lfshook](https://github.com/rifflock/lfshook) ? ? ? ? ? ?file-rotatelogs的GitHub地址? ?[https://github.com/lestrrat-go/file-rotatelogs](https://github.com/lestrrat-go/file-rotatelogs) ? ? ? ? ? ?pkg/errors的GitHub地址? ??[https://github.com/pkg/errors](https://github.com/pkg/errors)? ? # 二、logrus特性 logrus具有以下特性: 1. 完全兼容golang標準庫日志模塊。logrus擁有六種日志級別:debug、info、warn、error、fatal和panic,這是golang標準庫日志模塊的API的超集。如果你的項目使用標準庫日志模塊,完全可以用最低的代價遷移到logrus上。 2. 可擴展的Hook機制。允許使用者通過hook方式,將日志分發到任意地方,如本地文件系統、標準輸出、logstash、elasticsearch或者mq等,或者通過hook定義日志內容和格式等。 3. 可選的日志輸出格式。**logrus內置了兩種日志格式,JSONFormatter和TextFormatter。**如果這兩個格式不滿足需求,可以自己動手實現接口Formatter,來定義自己的日志格式。 4. Field機制。logrus鼓勵通過Field機制進行精細化、結構化的日志記錄,而不是通過冗長的消息來記錄日志。 5. logrus是一個可插拔的、結構化的日志框架。 # 三、logrus的使用 ## 3.1 第一個示例 最簡單的使用logrus的示例如下: ~~~ package main import ( log "github.com/sirupsen/logrus" ) func main() { log.WithFields(log.Fields{ "animal": "walrus", }).Info("A walrus appears") } ~~~ 上面代碼執行后,標準輸出上輸出如下: ~~~ time="2018-08-11T15:42:22+08:00" level=info msg="A walrus appears" animal=walrus ~~~ logrus與golang標準庫日志模塊完全兼容,因此,你可以使用`log “github.com/sirupsen/logrus”`替換所有日志導入。? ## 3.2 第二個示例 logrus可以通過簡單的配置,來定義輸出、格式或者日志級別等。示例如下: ~~~ package main import ( "os" log "github.com/sirupsen/logrus" ) func initLog() { // 設置日志格式為json格式 log.SetFormatter(&log.JSONFormatter{}) // 設置將日志輸出到標準輸出(默認的輸出為stderr,標準錯誤) // 日志消息輸出可以是任意的io.writer類型 log.SetOutput(os.Stdout) // 設置日志級別為warn以上 log.SetLevel(log.WarnLevel) } func main() { initLog() log.WithFields(log.Fields{ "animal": "walrus", "size": 10, "country": "china", }).Info("A group of walrus emerges from the ocean") log.WithFields(log.Fields{ "omg": true, "number": 122, "country": "china", }).Warn("The group's number increased tremendously!") log.WithFields(log.Fields{ "omg": true, "number": 100, "country": "america", }).Fatal("The ice breaks!") } ~~~ 上面代碼執行后,標準輸出上輸出如下: ~~~ {"country":"china","level":"warning","msg":"The group's number increased tremendously!","number":122,"omg":true,"time":"2018-12-12T18:22:20+08:00"} {"country":"america","level":"fatal","msg":"The ice breaks!","number":100,"omg":true,"time":"2018-12-12T18:22:20+08:00"} exit status 1 ~~~ ## 3.3 Logger logger是一種相對高級的用法。對于一個大型項目,往往需要一個全局的logrus實例,即`logger`對象,來記錄項目所有的日志。示例如下: ~~~ package main import ( "github.com/sirupsen/logrus" "os" ) // logrus提供了New()函數來創建一個logrus的實例。 // 項目中,可以創建任意數量的logrus實例。 var log = logrus.New() func main() { // 為當前logrus實例設置消息的輸出,同樣地, // 可以設置logrus實例的輸出到任意io.writer log.Out = os.Stdout // 為當前logrus實例設置消息輸出格式為json格式。 // 同樣地,也可以單獨為某個logrus實例設置日志級別和hook,這里不詳細敘述。 log.Formatter = &logrus.JSONFormatter{} log.WithFields(logrus.Fields{ "animal": "walrus", "size": 10, }).Info("A group of walrus emerges from the ocean") } ~~~ 執行結果如下所示: ~~~ {"animal":"walrus","level":"info","msg":"A group of walrus emerges from the ocean","size":10,"time":"2018-12-12T18:33:38+08:00"} ~~~ ## 3.4 Fields 前一章提到過,logrus不推薦使用冗長的消息來記錄運行信息,它推薦使用`Fields`來進行精細化的、結構化的信息記錄。? 例如下面記錄日志的方式: ~~~ log.Fatalf("Failed to send event %s to topic %s with key %d", event, topic, key) ```` 在logrus中不太提倡,logrus鼓勵使用以下方式替代之: <div class="se-preview-section-delimiter"></div> ```go log.WithFields(log.Fields{ "event": event, "topic": topic, "key": key, }).Fatal("Failed to send event") ~~~ 前面的`WithFields`?API可以規范使用者按照其提倡的方式記錄日志。但是,`WithFields`依然是可選的,因為某些場景下,使用者確實只需要記錄一條簡單的消息。 通常,在一個應用中,或者應用的一部分中,都有一些固定的`Field`。比如,在處理用戶http請求時,在上下文中,所有的日志都會有`request_id`和`user_ip`。為了避免每次記錄日志都要使用`log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip})`,我們可以創建一個`logrus.Entry`實例,為這個實例設置默認`Fields`,在上下文中使用這個`logrus.Entry`實例記錄日志即可。 ~~~ requestLogger := log.WithFields(log.Fields{"request_id": request_id, "user_ip": user_ip}) requestLogger.Info("something happened on that request") # will log request_id and user_ip requestLogger.Warn("something not great happened") ~~~ # 四、Hook logrus最令人心動的功能,就是其可擴展的HOOK機制了。通過在初始化時為logrus添加hook,logrus可以實現各種擴展功能。 ## 4.1 Hook接口 logrus的hook原理是:在每次寫入日志時攔截,修改logrus.Entry。logrus的hook接口定義如下。 ~~~ // logrus在記錄Levels()返回的日志級別的消息時,會觸發HOOK。 // 然后,按照Fire方法定義的內容,修改logrus.Entry。 type Hook interface { Levels() []Level Fire(*Entry) error } ~~~ 一個簡單自定義的hook如下所示。`DefaultFieldHook類型的對象`會在所有級別的日志消息中加入默認字段`appName="myAppName"`。 ~~~ type DefaultFieldHook struct { } func (hook *DefaultFieldHook) Fire(entry *log.Entry) error { entry.Data["appName"] = "MyAppName" return nil } func (hook *DefaultFieldHook) Levels() []log.Level { return log.AllLevels } ~~~ hook的使用也很簡單,在初始化前調用`log.AddHook(hook)`添加相應的`hook`即可。 logrus官方僅僅內置了syslog的[hook](https://github.com/sirupsen/logrus/tree/master/hooks/syslog)。?但Github上有很多第三方的hook可供使用,文末將提供一些第三方HOOK的鏈接。 # 五、問題與解決方案 盡管logrus有諸多優點,但是為了靈活性和可擴展性,官方也削減了很多實用的功能,例如: * 沒有提供行號和文件名的支持 * 輸出到本地文件系統時,沒有提供日志分割功能 * 官方沒有提供輸出到ELK等日志處理中心的功能 但是,這些功能都可以通過自定義hook來實現。 ## 5.1 記錄文件名和行號 logrus一個很致命的問題,就是沒有提供文件名和行號,這在大型項目中通過日志定位問題時有諸多不便。Github上的logrus的issue#63:[Log filename and line number](https://github.com/sirupsen/logrus/issues/63)創建于2014年,四年過去了仍是open狀態。 網上給出的解決方案分位兩類,一就是自己實現一個hook;二就是通過裝飾器包裝`logrus.Entry`。兩種方案網上都有很多代碼,但是大多無法正常工作。但總體來說,解決問題的思路都是對的:通過標準庫的`runtime`模塊獲取運行時信息,并從中提取文件名、行號和調用函數名。 標準庫`runtime`模塊的`Caller(skip int)`函數可以返回當前goroutine調用棧中的文件名、行號、函數信息等,參數skip表示返回的棧幀的層次,0表示`runtime.Caller`的調用者。返回值包括響應棧幀層次的pc(程序計數器)、文件名和行號信息。為了提高效率,我們通過跟蹤調用棧發現,從`runtime.Caller()`的調用者開始,到記錄日志的生成代碼之間,大概有8到11層左右,所以我們在hook中循環第8到11層調用棧,應該可以找到日志記錄的生產代碼。? ![這里寫圖片描述](https://img-blog.csdn.net/20180814170801498?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dzbHlrNjA2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)? 此外,`runtime.FuncForPC(pc uintptr) *Func`可以返回指定`pc`的函數信息。? 所以,我們要實現的hook也是基于以上原理,使用`runtime.Caller()`依次循環調用棧的第7~11層,過濾掉`sirupsen`包內容,那么第一個非`siupsenr`包就認為是我們的生產代碼了,并返回`pc`以便通過`runtime.FuncForPC()`獲取函數名稱。然后將文件名、行號和函數名組裝為`source`字段塞到`logrus.Entry`中即可。 ~~~ time="2018-08-11T19:10:15+08:00" level=warning msg="postgres_exporter is ready for scraping on 0.0.0.0:9295..." source="postgres_exporter/main.go:60:main()" time="2018-08-11T19:10:17+08:00" level=error msg="!!!msb info not found" source="postgres/postgres_query.go:63:QueryPostgresInfo()" time="2018-08-11T19:10:17+08:00" level=error msg="get postgres instances info failed, scrape metrics failed, error:msb env not found" source="collector/exporter.go:71:Scrape()" ~~~ ## 5.2 日志本地文件分割 logrus本身不帶日志本地文件分割功能,但是我們可以通過`file-rotatelogs`進行日志本地文件分割。 每次在我們寫入日志的時候,logrus都會調用`file-rotatelogs`來判斷日志是否要進行切分。關于本地日志文件分割的例子,網上很多,這里不再詳細介紹,奉上代碼: ~~~ import ( "github.com/lestrrat-go/file-rotatelogs" "github.com/rifflock/lfshook" log "github.com/sirupsen/logrus" "time" ) func newLfsHook(logLevel *string, maxRemainCnt uint) log.Hook { writer, err := rotatelogs.New( logName+".%Y%m%d%H", // WithLinkName 為最新的日志建立軟連接,以方便隨時找到當前日志文件 rotatelogs.WithLinkName(logName), // WithRotationTime 設置日志分割的時間,這里設置為一小時分割一次 rotatelogs.WithRotationTime(time.Hour), // WithMaxAge和WithRotationCount 二者只能設置一個, // WithMaxAge 設置文件清理前的最長保存時間, // WithRotationCount 設置文件清理前最多保存的個數。 //rotatelogs.WithMaxAge(time.Hour*24), rotatelogs.WithRotationCount(maxRemainCnt), ) if err != nil { log.Errorf("config local file system for logger error: %v", err) } level, ok := logLevels[*logLevel] if ok { log.SetLevel(level) } else { log.SetLevel(log.WarnLevel) } lfsHook := lfshook.NewHook(lfshook.WriterMap{ log.DebugLevel: writer, log.InfoLevel: writer, log.WarnLevel: writer, log.ErrorLevel: writer, log.FatalLevel: writer, log.PanicLevel: writer, }, &log.TextFormatter{DisableColors: true}) return lfsHook } ~~~ 使用上述本地日志文件切割的效果如下:? ![這里寫圖片描述](https://img-blog.csdn.net/20180814170847468?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dzbHlrNjA2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70) ## 5.3 將日志發送到elasticsearch 將日志發送到elasticsearch,是很多日志監控系統的選擇。將logrus日志發送到elasticsearch的原理是:在hook的每次fire調用時,使用golang的es客戶端將日志信息寫到elasticsearch。elasticsearch官方沒有提供golang客戶端,但是有很多第三方的go語言客戶端可供使用,我們選擇[elastic](https://github.com/olivere/elastic)。elastic提供了豐富的[文檔](https://godoc.org/gopkg.in/olivere/elastic.v5),以及Java中的流式接口,使用起來非常方便。 ~~~ client, err := elastic.NewClient(elastic.SetURL("http://localhost:9200")) if err != nil { log.Panic(err) } // Index a tweet (using JSON serialization) tweet1 := Tweet{User: "olivere", Message: "Take Five", Retweets: 0} put1, err := client.Index(). Index("twitter"). Type("tweet"). Id("1"). BodyJson(tweet1). Do(context.Background()) ~~~ 考慮到logrus的Fields機制,可以實現如下數據格式: ~~~ msg := struct { Host string Timestamp string `json:"@timestamp"` Message string Data logrus.Fields Level string } ~~~ 其中,`Host`記錄產生日志主機信息,在創建hook時指定。對于其他數據,需要從`logrus.Entry`中取得。測試中,我們選擇按照此原理實現的第三方HOOK:[elogrus](https://github.com/sohlich/elogrus)。其使用如下: ~~~ import ( "github.com/olivere/elastic" "gopkg.in/sohlich/elogrus" ) func initLog() { client, err := elastic.NewClient(elastic.SetURL("http://localhost:9200")) if err != nil { log.Panic(err) } hook, err := elogrus.NewElasticHook(client, "localhost", log.DebugLevel, "mylog") if err != nil { log.Panic(err) } log.AddHook(hook) } ~~~ 從Elasticsearch查詢得到日志存儲,效果如下: ~~~ GET http://localhost:9200/mylog/_search HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 transfer-encoding: chunked { "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2474, "max_score": 1.0, "hits": [ { "_index": "mylog", "_type": "log", "_id": "AWUw13jWnMZReb-jHQup", "_score": 1.0, "_source": { "Host": "localhost", "@timestamp": "2018-08-13T01:12:32.212818666Z", "Message": "!!!msb info not found", "Data": {}, "Level": "ERROR" } }, { "_index": "mylog", "_type": "log", "_id": "AWUw13jgnMZReb-jHQuq", "_score": 1.0, "_source": { "Host": "localhost", "@timestamp": "2018-08-13T01:12:32.223103348Z", "Message": "get postgres instances info failed, scrape metrics failed, error:msb env not found", "Data": { "source": "collector/exporter.go:71:Scrape()" }, "Level": "ERROR" } }, //... { "_index": "mylog", "_type": "log", "_id": "AWUw2f1enMZReb-jHQu_", "_score": 1.0, "_source": { "Host": "localhost", "@timestamp": "2018-08-13T01:15:17.212546892Z", "Message": "!!!msb info not found", "Data": { "source": "collector/exporter.go:71:Scrape()" }, "Level": "ERROR" } }, { "_index": "mylog", "_type": "log", "_id": "AWUw2NhmnMZReb-jHQu1", "_score": 1.0, "_source": { "Host": "localhost", "@timestamp": "2018-08-13T01:14:02.21276903Z", "Message": "!!!msb info not found", "Data": {}, "Level": "ERROR" } } ] } } Response code: 200 (OK); Time: 16ms; Content length: 3039 bytes ~~~ ## 5.4 將日志發送到其他位置 將日志發送到日志中心,也是logrus所提倡的。雖然沒有提供官方支持,但是目前Github上有很多第三方hook可供使用: * [logrus\_amqp](https://github.com/vladoatanasov/logrus_amqp):Logrus hook for Activemq * [logrus-logstash-hook](https://github.com/bshuster-repo/logrus-logstash-hook):Logstash hook for logrus * [mgorus](https://github.com/weekface/mgorus):Mongodb Hooks for Logrus * [logrus\_influxdb](https://github.com/abramovic/logrus_influxdb):InfluxDB Hook for Logrus * [logrus-redis-hook](https://github.com/rogierlommers/logrus-redis-hook):Hook for Logrus which enables logging to RELK stack (Redis, Elasticsearch, Logstash and Kibana) 等等。對于上述第三方hook,我這里沒有具體驗證,大家可以根據需要自行嘗試。 ## 5.5 其他注意事項 ### 5.5.1 Fatal處理 和很多日志框架一樣,logrus的`Fatal`系列函數會執行`os.Exit(1)`。但是,logrus提供“可以注冊一個或多個`fatal handler`函數`”`的接口`logrus.RegisterExitHandler(handler func(){})`,讓logrus在執行`os.Exit(1)`之前進行相應的處理。`fatal handler`可以在系統異常時調用一些資源釋放api等,讓應用正確地關閉。 ### 5.5.2 線程安全 **默認情況下,logrus的api都是線程安全的,其內部通過互斥鎖來保護并發寫**。互斥鎖工作于調用hooks或者寫日志的時候。如果不需要鎖,可以調用`logger.SetNoLock()`來關閉之。可以關閉logrus互斥鎖的情形包括: * 沒有設置hook,或者所有的hook都是線程安全的實現。 * 寫日志到logger.Out已經是線程安全的了。例如,logger.Out已經被鎖保護,或者寫文件時,文件是以O\_APPEND方式打開的,并且每次寫操作都小于4k。 尊重別人的勞動成果,原文網址: [https://blog.csdn.net/wslyk606/article/details/81670713](https://blog.csdn.net/wslyk606/article/details/81670713)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看