当前位置:网站首页>Log logrus third party library usage
Log logrus third party library usage
2022-07-01 01:31:00 【weixin_ thirty-eight million one hundred and seven thousand fou】
One 、 install
github.com/sirupsen/logruslogrus Use profile

{
"log_path":"logs",
"log_name":"gin_project.log",
"log_level":"info"
}Two 、 Load the configuration

package logs_source
import (
"os"
"io/ioutil"
"fmt"
"encoding/json"
)
type LogConfig struct {
LogPath string `json:"log_path"`
LogName string `json:"log_name"`
LogLevel string `json:"log_level"`
}
func LoadLogConfig() *LogConfig{
log_conf := LogConfig{}
file,err := os.Open("conf/log_conf.json")
if err != nil {
panic(err)
}
defer file.Close()
byte_datas,err2 := ioutil.ReadAll(file)
if err2 != nil {
panic(err2)
}
err3 := json.Unmarshal(byte_datas,&log_conf)
if err3 != nil {
fmt.Println(err3)
}
fmt.Println(log_conf)
return &log_conf
}
3、 ... and 、 initialization
package logs_source
import (
"github.com/sirupsen/logrus"
"os"
"fmt"
)
// Create an instance
var Log = logrus.New()
func init() {
log_conf := LoadLogConfig()
// Set log output file
log_dir := log_conf.LogPath + "/" +log_conf.LogName
fmt.Println(log_dir)
file,err := os.OpenFile(log_dir,os.O_APPEND|os.O_WRONLY,os.ModeAppend)
if err != nil {
panic(err)
}
Log.Out = file
// Set the log level
level_mapping := map[string]logrus.Level{
"trace":logrus.TraceLevel,
"debug":logrus.DebugLevel,
"info":logrus.InfoLevel,
"warn":logrus.WarnLevel,
"error":logrus.ErrorLevel,
"fatal":logrus.FatalLevel,
"panic":logrus.PanicLevel,
}
Log.SetLevel(level_mapping[log_conf.LogLevel])
// Log formatting
Log.SetFormatter(&logrus.TextFormatter{})
}
//func main() {
// Log.Info("xxxxx")
//}logs.Log.Warn(" This is a warnning Level of logging ")
logs.Log.WithFields(logrus.Fields{
"msg": " Test error ",
}).Warn(" This is a warnning Level of logging ")边栏推荐
- 1175. Prime Arrangements
- 元宇宙为 VR/AR 带来的新机会
- New opportunities for vr/ar brought by metauniverse
- neo4j安装、运行以及项目的构建和功能实现
- [learning notes] double + two points
- 【Proteus仿真】Arduino UNO +74C922键盘解码驱动4X4矩阵键盘
- Solve idea:class' xxx 'not found in module' xxx‘
- 【队列】933. Number of Recent Calls
- TypeError: Argument ‘angle‘ can not be treated as a double
- 45岁程序员告诉你:程序员为什么要跳槽,太真实...
猜你喜欢
随机推荐
未来的 Web3会带来什么?
为什么要搭建个人博客
面对产业互联网的时候,甚至还用消费互联网的方式和方法去落地和实践产业互联网
JS to convert numbers into Chinese characters for output
C语言一点点(未来可会增加)
uniapp官方组件点击item无效,解决方案
Open3D 点云颜色渲染
1175. Prime Arrangements
[problem handled] -nvidia SMI command cannot obtain the GPU process number of its own container and the external GPU process number
Docker deployment MySQL 8
【多源bfs】934. Shortest Bridge
"Open math input panel" in MathType editing in win11 is gray and cannot be edited
Uniapp official component clicking item is invalid, solution
Relationship between ASCII, Unicode, GBK, UTF-8
Mustache syntax
45岁程序员告诉你:程序员为什么要跳槽,太真实...
[leetcode] sum of two numbers [1]
StrictMode分析Registion-StrictMode原理(4)
Strictmode analysis registration strictmode principle (4)
微生物健康,食品微生物检测为什么很重要





![Parity linked list [two general directions of linked list operation]](/img/4e/ce860bc172bb75f456427ba26a7842.png)



