当前位置:网站首页>日志 logrus第三方库的使用
日志 logrus第三方库的使用
2022-07-01 00:41:00 【weixin_38107457】
一、安装
github.com/sirupsen/logruslogrus使用配置文件

{
"log_path":"logs",
"log_name":"gin_project.log",
"log_level":"info"
}二、加载配置

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
}
三、初始化
package logs_source
import (
"github.com/sirupsen/logrus"
"os"
"fmt"
)
// 创建一个实例
var Log = logrus.New()
func init() {
log_conf := LoadLogConfig()
// 设置日志输出文件
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
// 设置日志級別
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.SetFormatter(&logrus.TextFormatter{})
}
//func main() {
// Log.Info("xxxxx")
//}logs.Log.Warn("这是一个warnning级别的日志")
logs.Log.WithFields(logrus.Fields{
"msg": "测试的错误",
}).Warn("这是一个warnning级别的日志")边栏推荐
猜你喜欢
随机推荐
MFC TCP通信服务端客户端Demo备忘vs2019
Web interface testing of software testing
Packing and unpacking of C #
自定义注解实现校验
数字IC设计流程总结
Interpreting the scientific and technological literacy contained in maker Education
Q弹松软的大号吐司,带来更舒服的睡眠
基础知识之二——STA相关的基本定义
奇偶链表[链表操作的两种大方向]
K210 site helmet
解析融合学科本质的创客教育路径
For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
用recyclerReview展示Banner,很简单
StrictMode分析Activity泄漏-StrictMode原理(3)
Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images
小程序自定义宫格
使用StrictMode-StrictMode原理(1)
软硬件基础知识学习--小日记(1)
K210工地安全帽








