当前位置:网站首页>日志 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级别的日志")边栏推荐
猜你喜欢

蒹葭苍苍,白露为霜。

Why not two or four TCP handshakes

機器人編程的培訓學科類原理

Docker 部署 MySQL 8

Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support

技术人进阶画业务大图,手把手教学来了

Note d'étude du DC: zéro dans le chapitre officiel - - Aperçu et introduction du processus de base

Two position relay st2-2l/ac220v

TCP三次握手为什么不是两次或四次

基础知识之二——STA相关的基本定义
随机推荐
Training discipline principle of robot programming
06.论Redis持久化的几种方式
用recyclerReview展示Banner,很简单
解读创客教育所蕴含的科技素养
解决IDEA:Class ‘XXX‘ not found in module ‘XXX‘
DLS-20型双位置继电器 220VDC
K210门禁毕设
生意和投资的思考
Analyzing the wisdom principle in maker education practice
染色法判断二分图
小程序自定义宫格
06. on several ways of redis persistence
友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
5. TPM module initialization
The liquor and tourism sector recovers, and Yaduo continues to dream of listing. How far is it from "the first share of the new accommodation economy"?
mysql数据库基础:流程控制
Pre training / transfer learning of models
Koa koa combine routes sub route management
For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
图的连通性基础