当前位置:网站首页>日志 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级别的日志")边栏推荐
猜你喜欢
随机推荐
TypeError: Argument ‘angle‘ can not be treated as a double
機器人編程的培訓學科類原理
Pytorch programming knowledge (2)
【Qt5-基础篇_1】从0开始,德天老师和你一起学习——窗口简介
Inspire students' diversified thinking with steam Education
用recyclerReview展示Banner,很简单
ESP8266 RC522
qt5-MVC:数据可视化的层次揭秘
How to do the performance pressure test of "Health Code"
DLS-20型双位置继电器 220VDC
Green, green the reed. dew and frost gleam.
文件服务设计
基础知识之三——标准单元库
visual studio 2019 下载
Open3D 点云包围盒
[问题已处理]-nvidia-smi命令获取不到自身容器的GPU进程和外部的GPU进程号
Locking relay ydb-100, 100V
MFC TCP通信服务端客户端Demo备忘vs2019
ASCII、Unicode、GBK、UTF-8之间的关系
JS方法大全的一个小文档









