欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Go實(shí)現(xiàn)整合Logrus實(shí)現(xiàn)日志打印

 更新時(shí)間:2022年07月04日 10:04:57   作者:??BarryYan????  
這篇文章主要介紹了Go實(shí)現(xiàn)整合Logrus實(shí)現(xiàn)日志打印,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

Github:github.com/sirupsen/lo…

1 初步使用

package main
import (
 ? "context"
 ? "github.com/sirupsen/logrus"
)
?
func main() {
 ? method0()
}
func method0() {
 ? logger:= logrus.New()
 ? logger.Warning("This is a first log.")
 ? ctx := context.WithValue(context.Background(),"key","value")
 ? logger.Warning(ctx,"This is a second log.")
}

2 增加標(biāo)簽WithFields

package main?
import (
 ? "context"
 ? "github.com/sirupsen/logrus"
)
func main() {
 ? method1()
}
func method1() {
 ? log.WithFields(log.Fields{
 ? ? ?"fieldKey": "fieldValue",
 ? }).Warning("This is a first field log.")
?
 ? log.WithFields(log.Fields{
 ? ? ?"fieldKey": "fieldValue",
 ? ? ?"fieldKey2": "fieldValue2",
 ? }).Warning("This is a second field log.")
}

3 配置常見參數(shù)

package main
import (
 ? "context"
 ? "github.com/sirupsen/logrus"
 ? log "github.com/sirupsen/logrus"
 ? "os"
)
?func main() {
 ? method2()
}
func init() {
 ? // 日志作為JSON而不是默認(rèn)的ASCII格式器.
 ? log.SetFormatter(&log.JSONFormatter{})
?
 ? // 輸出到標(biāo)準(zhǔn)輸出,可以是任何io.Writer
 ? log.SetOutput(os.Stdout)
?
 ? // 只記錄xx級(jí)別或以上的日志
 ? log.SetLevel(log.TraceLevel)
}
func method2() {
 ? log.WithFields(log.Fields{
 ? ? ?"animal": "walrus",
 ? ? ?"size": ? 10,
 ? }).Info("A group of walrus emerges from the ocean")
?
 ? log.WithFields(log.Fields{
 ? ? ?"omg": ? ?true,
 ? ? ?"number": 122,
 ? }).Warn("The group's number increased tremendously!")
?
 ? log.WithFields(log.Fields{
 ? ? ?"omg": ? ?true,
 ? ? ?"number": 100,
 ? }).Fatal("The ice breaks!")
}

Formatter一般分為兩種:

  • &log.JSONFormatter{}
  • &log.TextFormatter{}

日志級(jí)別一共七種:

  • log.Trace()
  • log.Debug()
  • log.Info()
  • log.Warn()
  • log.Error()
  • log.Fatal()
  • log.Panic()

4 輸出日志到文件

package main
import (
    "context"
    "github.com/sirupsen/logrus"
    "os"
)
func main() {
    method4()
}
func method4() {
 ? var log = logrus.New()
 ? file ,err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666)
 ? if err == nil{
 ? ? ?log.Out = file
 ? }else{
 ? ? ?log.Info("Failed to log to file")
 ? }
?
 ? log.WithFields(logrus.Fields{
 ? ? ?"filename": "123.txt",
 ? }).Info("This is a file log")
}

logrus.log文件的內(nèi)容:

time="2022-01-06T13:04:25+08:00" level=info msg="This is a file log" filename=123.txt\

5 利用Hooks將日志輸出到其他地方

import (
 ?log "github.com/sirupsen/logrus"
 ?"gopkg.in/gemnasium/logrus-airbrake-hook.v2" // the package is named "airbrake"
 ?logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
 ?"log/syslog"
)
func init() {
 ?// 使用氣閘掛鉤來報(bào)告錯(cuò)誤嚴(yán)重程度或以上的錯(cuò)誤一個(gè)異常追蹤。您可以創(chuàng)建自定義鉤子,請參見鉤子部分。
 ?log.AddHook(airbrake.NewHook(123, "xyz", "production"))
?
 ?hook, err := logrus_syslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")
 ?if err != nil {
 ? ?log.Error("Unable to connect to local syslog daemon")
  } else {
 ? ?log.AddHook(hook)
  }
}

只需要在AddHook是添加相應(yīng)的Hook就可以了

到此這篇關(guān)于Go實(shí)現(xiàn)整合Logrus實(shí)現(xiàn)日志打印的文章就介紹到這了,更多相關(guān)Go Logrus日志打印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go+Kafka實(shí)現(xiàn)延遲消息的實(shí)現(xiàn)示例

    Go+Kafka實(shí)現(xiàn)延遲消息的實(shí)現(xiàn)示例

    本文主要介紹了Go+Kafka實(shí)現(xiàn)延遲消息的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Go有效獲取變量類型多種方法探索

    Go有效獲取變量類型多種方法探索

    這篇文章主要介紹了Go有效獲取變量類型的多種方法探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-02-02
  • Golang異??刂铺幚沓绦蝈e(cuò)誤流程

    Golang異常控制處理程序錯(cuò)誤流程

    這篇文章主要介紹了Golang異??刂铺幚沓绦蝈e(cuò)誤流程,Golang異常處理機(jī)制包括錯(cuò)誤處理、panic和defer,可控制程序錯(cuò)誤流程,保證程序穩(wěn)定性和安全性,是Golang編程的關(guān)鍵方式
    2023-04-04
  • go語言開發(fā)環(huán)境配置(sublime text3+gosublime)

    go語言開發(fā)環(huán)境配置(sublime text3+gosublime)

    網(wǎng)上google了下go的開發(fā)工具,大都推薦sublime text3+gosublime,本文就介紹了go語言開發(fā)環(huán)境配置(sublime text3+gosublime),具有一定的參考價(jià)值,感興趣的可以了解一下
    2022-01-01
  • GO語言中常見的排序算法使用示例

    GO語言中常見的排序算法使用示例

    這篇文章主要為大家介紹了GO語言中常見排序算法的使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • go日志系統(tǒng)logrus顯示文件和行號(hào)的操作

    go日志系統(tǒng)logrus顯示文件和行號(hào)的操作

    這篇文章主要介紹了go日志系統(tǒng)logrus顯示文件和行號(hào)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Golang語言學(xué)習(xí)拿捏Go反射示例教程

    Golang語言學(xué)習(xí)拿捏Go反射示例教程

    這篇文章主要為大家介紹了Golang語言中Go反射示例的教程,教你拿捏Go反射,再也不用被Go反射折磨,有需要的朋友可以共同學(xué)習(xí)參考下
    2021-11-11
  • go語言中HTTPRouter的算法演進(jìn)

    go語言中HTTPRouter的算法演進(jìn)

    這篇文章主要從開發(fā)中常見的應(yīng)用場景?“路由管理”?為例,為大家詳細(xì)介紹一下三種常用的實(shí)現(xiàn)方案背后的數(shù)據(jù)結(jié)構(gòu)和算法,感興趣的小伙伴可以了解一下
    2023-08-08
  • 詳解go語言 make(chan int, 1) 和 make (chan int) 的區(qū)別

    詳解go語言 make(chan int, 1) 和 make (chan int) 的區(qū)別

    這篇文章主要介紹了go語言 make(chan int, 1) 和 make (chan int) 的區(qū)別,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01
  • GO Cobra Termui庫開發(fā)終端命令行小工具輕松上手

    GO Cobra Termui庫開發(fā)終端命令行小工具輕松上手

    這篇文章主要為大家介紹了GO語言開發(fā)終端命令行小工具,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01

最新評(píng)論