当前位置:网站首页>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 ")边栏推荐
- Unknown database连接数据库错误
- 基础知识之二——STA相关的基本定义
- [learning notes] simple DP
- 生意和投资的思考
- 微生物安全與健康,什麼是生物處理?
- Microbial safety and health, what is biological treatment?
- Using recyclerreview to show banner is very simple
- Looksrare team's "cash out" caused disturbance
- Dx-11q signal relay
- [dynamic planning] path dp:931 Minimum Falling Path Sum
猜你喜欢

微生物健康,食品微生物检测为什么很重要

日志 logrus第三方库的使用

未来的 Web3会带来什么?

友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)

Windows环境下安装MongoDB数据库

qt5-MVC:数据可视化的层次揭秘
![Split the linked list [take next first and then cut the linked list to prevent chain breakage]](/img/eb/708ab20c13df75f4dbd2d6461d3602.png)
Split the linked list [take next first and then cut the linked list to prevent chain breakage]
![Parity linked list [two general directions of linked list operation]](/img/4e/ce860bc172bb75f456427ba26a7842.png)
Parity linked list [two general directions of linked list operation]

农产品换房?“变相”购房补贴!
![[network packet loss and network delay? This artifact can help you deal with everything!]](/img/c4/f733b23327458b9266b9cbcccb6f14.png)
[network packet loss and network delay? This artifact can help you deal with everything!]
随机推荐
flutter报错 -- The argument type ‘Function‘ can‘t be assigned to the parameter type ‘void Function()?‘
用 Flutter 的 Canvas 画点有趣的图形
visual studio 2019 快捷键备忘
What are the functions of soil microorganisms in microbial detection?
视频教程 | 长安链推出系列视频教程合集(入门)
未来的 Web3会带来什么?
Two position relay st2-2l/ac220v
Dx-11q signal relay
Open3D 点云颜色渲染
Strictmode jamming and leakage detection -strictmode principle (2)
Parity linked list [two general directions of linked list operation]
K210 access control complete
Pre training / transfer learning of models
Double position relay dls-5/2 dc220v
Basic knowledge II - Basic definitions related to sta
Poor students can also play raspberry pie
Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images
1175. Prime Arrangements
ESP8266 RC522
Typora的使用