当前位置:网站首页>日志 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级别的日志")边栏推荐
- 二季度最后一天
- Basic knowledge of software and hardware -- diary (1)
- 个人博客搭建与美化
- StrictMode带来的思考-StrictMode原理(5)
- 软硬件基础知识学习--小日记(1)
- 双位置继电器DLS-5/2 DC220V
- Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
- [leetcode] sum of two numbers [1]
- [LeetCode] 两数之和【1】
- Digital IC design process summary
猜你喜欢
随机推荐
Impact relay zc-23/dc220v
Using asyncio for concurrency
Parity linked list [two general directions of linked list operation]
Inspire students' diversified thinking with steam Education
Visual studio 2019 Download
DLS-20型双位置继电器 220VDC
冲击继电器ZC-23/DC220V
dc_labs--lab1的学习与总结
[learning notes] double + two points
Double position relay dls-5/2 dc220v
双位置继电器ST2-2L/AC220V
二季度最后一天
Digital IC design process summary
QT5-布局在创作中的理解应用
fluttertoast
pull_ to_ refresh
User defined annotation implementation verification
基础知识之一——STA基础概述
C语言一点点(未来可会增加)
Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images









