当前位置:网站首页>Go integrates logrus to realize log printing
Go integrates logrus to realize log printing
2022-07-01 13:56:00 【Barry Yan】
Github:https://github.com/sirupsen/logrus
1 Preliminary use
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 labeling 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 Configure common parameters
package main
import (
"context"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"os"
)
func main() {
method2()
}
func init() {
// Log as JSON Not the default ASCII Formatter .
log.SetFormatter(&log.JSONFormatter{
})
// Output to standard output , It could be anything io.Writer
log.SetOutput(os.Stdout)
// Record only xx Level or above
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 There are generally two kinds :
- &log.JSONFormatter{}
- &log.TextFormatter{}
There are seven log levels :
- log.Trace()
- log.Debug()
- log.Info()
- log.Warn()
- log.Error()
- log.Fatal()
- log.Panic()
4 Output log to file
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 The content of the document :
time="2022-01-06T13:04:25+08:00" level=info msg="This is a file log" filename=123.txt\
5 utilize Hooks Output logs to other places
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() {
// Use the airlock hook to report errors of severity or above an anomaly trace . You can create custom hooks , See the hook section .
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)
}
}
Only need AddHook Yes, add the corresponding Hook That's all right.
边栏推荐
猜你喜欢

Basic operation of queue (implemented in C language)

After being laid off for three months, the interview ran into a wall everywhere, and the mentality has begun to collapse

The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise

So programmers make so much money doing private work? It's really delicious

一文读懂TDengine的窗口查询功能

App自动化测试开元平台Appium-runner
![[anwangbei 2021] Rev WP](/img/98/ea5c241e2b8f3ae4c76e1c75c9e0d1.png)
[anwangbei 2021] Rev WP

Admire, Ali female program undercover more than 500 black production groups

刘对(火线安全)-多云环境的风险发现

04 redis source code data structure dictionary
随机推荐
队列的基本操作(C语言实现)
How much money do novices prepare to play futures? Is agricultural products OK?
The integration of computing and Internet enables the transformation of the industry, and the mobile cloud lights up a new roadmap for the future of digital intelligence
What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
Etcd summary mechanism and usage scenarios
Explain IO multiplexing, select, poll, epoll in detail
QT学习管理系统
Six years of technology iteration, challenges and exploration of Alibaba's globalization and compliance
“国防七子”经费暴增,清华足足362亿元,甩第二名101亿 |全国高校2022预算大公开...
Spark source code reading outline
QT community management system
二传感器尺寸「建议收藏」
[NLP] pre training model - gpt1
Leetcode question 1: sum of two numbers (3 languages)
龙蜥社区开源 coolbpf,BPF 程序开发效率提升百倍
SAP 智能机器人流程自动化(iRPA)解决方案分享
运行游戏时出现0xc000007b错误的解决方法[通俗易懂]
ArrayList capacity expansion mechanism and thread safety
Fiori 应用通过 Adaptation Project 的增强方式分享
玩转MongoDB—搭建MongoDB集群