当前位置:网站首页>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 ")边栏推荐
- Note d'étude du DC: zéro dans le chapitre officiel - - Aperçu et introduction du processus de base
- Koa koa combine routes sub route management
- 面对产业互联网的时候,甚至还用消费互联网的方式和方法去落地和实践产业互联网
- System settings large page
- dc_labs--lab1的学习与总结
- What will Web3 bring in the future?
- 使用 C# 创造 ASCII 艺术
- zabbix如何配置告警短信?(预警短信通知设置流程)
- Poor students can also play raspberry pie
- 物业怎么发短信通知给业主?
猜你喜欢

Unknown database连接数据库错误

Why not two or four TCP handshakes

个人博客搭建与美化

Kongyiji's first question: how much do you know about service communication?

DC学习笔记正式篇之零——综述与基本流程介绍

Dls-20 double position relay 220VDC

ESP8266 RC522

neo4j安装、运行以及项目的构建和功能实现

迪赛智慧数——其他图表(平行坐标图):2021年应届专业就业情况

Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
随机推荐
DC學習筆記正式篇之零——綜述與基本流程介紹
gin_gorm
StrictMode分析Activity泄漏-StrictMode原理(3)
flutter报错 -- The argument type ‘Function‘ can‘t be assigned to the parameter type ‘void Function()?‘
Using recyclerreview to show banner is very simple
编译安装oh-my-zsh
使用 C# 创造 ASCII 艺术
New opportunities for vr/ar brought by metauniverse
Mustache syntax
Installing mongodb database in Windows Environment
(learning power + thinking power) x action power, summary of flywheel effect on the growth of technicians
C语言一点点(未来可会增加)
About vctk datasets
[deepin] common sets
Matlab farthest point sampling (FPS improved version)
Relationship between ASCII, Unicode, GBK, UTF-8
System settings large page
OCR的一些项目
视频教程 | 长安链推出系列视频教程合集(入门)
Basic knowledge of software and hardware -- diary (1)