xlog

package
v0.3.34 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2024 License: BSD-3-Clause-Clear Imports: 7 Imported by: 0

Documentation

Overview

日志记录模块

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AE

func AE(msg string, err error) error

记录详情日志[AlertError的简称]

msg	消息备注
err	错误详情

func Alert

func Alert(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func AlertError

func AlertError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func CE

func CE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Crit

func Crit(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func CritError

func CritError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func DE

func DE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Debug

func Debug(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func DebugError

func DebugError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func EE

func EE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Error

func Error(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func ErrorError

func ErrorError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Ftime

func Ftime(t time.Time)

统计函数耗时,用法:defer xlog.Ftime(time.Now())

因内部使用了反射机制,所以无法使用匿名函数进行多层函数名获取 所以此处暂时截至到此处,使用 defer xlog.Ftime(time.Now()) 的固定结构来统计时长

t	当前时间

func IE

func IE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Info

func Info(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func InfoError

func InfoError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func NE

func NE(msg string) error

记录详情日志,返回error类型,以便其他方法调用

func NN

func NN(msg string) error

记录详情日志,返回nil的error类型,以便其他方法调用

func Notice

func Notice(msg string)

记录详情日志

msg	消息备注

func NoticeError

func NoticeError(msg string) error

记录详情日志,返回error类型,以便其他方法调用

func PE

func PE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Panic

func Panic(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func PanicError

func PanicError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Quest

func Quest(h *https.CURL)

记录HTTPS请求日志

注册HTTPS服务时将此函数注入进去

func QuestJson

func QuestJson(s []byte)

记录其他符合规则的HTTP请求,JSON格式化后结构需要为https.CURL结构

s	JSON字节码

func Recover

func Recover(f ...func())

程序Recover恢复及记录相关信息 此程序为放置在defer中,避免程序强退导致异常的消息记录,所以使用时请直接:defer xlog.Recover()

f	Recover时调用的函数列表,一般留空,最常用作用为panic时的特殊日志记录(除xlog.Panic记录外的其他记录)

func Replates added in v0.3.24

func Replates(pth string) string

func SaveAny

func SaveAny(inf byte, file string, line uint, msg string, err error)

记录日志信息[对外函数,用于记录任意类型/等级的日志信息]

inf		日志等级
file	错误文件
line	错误行号
msg		消息备注
err		错误详情

func SaveAnyError

func SaveAnyError(inf byte, file string, line uint, msg string, err error) error

记录日志信息[对外函数,用于记录任意类型/等级的日志信息]

inf		日志等级
file	错误文件
line	错误行号
msg		消息备注
err		错误详情

func SetInteface added in v0.3.24

func SetInteface(c LogSave)

设置日志存储的接口信息

c	日志存储实现类

func SetShow added in v0.3.24

func SetShow(c bool)

设置是否显示日志在控制台

c	是否在控制台显示

func User

func User(uid uint, typ, info string)

记录用户日志

uid		用户ID
typ		记录类型
info	记录的详细信息

func WE

func WE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Warning

func Warning(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func WarningError

func WarningError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

Types

type Config

type Config struct {
	SaveInterface LogSave // 不记录到DB数据库中,而是直接调用接口进行记录【一旦传入此接口,数据库中将不再进行插入】
	Console       bool    // 是否输出到控制台中
}

日志记录模块使用GO携程进行日志记录(此形式可能会存在退出的时候谢程未全部退出的问题,该问题会造成部分日志记录失败的情况) 错误等级:Info -> Notice -> Warning -> Error -> Crit -> Alert -> Panic 错误等级简写:I -> N -> W -> E -> C -> A -> P 备注:此等级中,Crit、Alert、Panic错误为影响到程序正常运行的错误,Error为需要注意,即将影响到正常运行的错误

Panic等级的错误也只是记录到数据库中,而不是直接将程序进行panic退出
记录函数的执行时常
调用方法为:defer xlog.FTime(time.Now())

配置项

type FuncLog

type FuncLog struct {
	Id        uint64 `gorm:"column:id;primaryKey;autoIncrement" json:"id" form:"id"`             // 条目ID
	Func      string `gorm:"column:func;size:200;comment:函数名称" json:"func" form:"func"`          //函数名称
	Line      uint   `gorm:"column:line;comment:调用行数" json:"line" form:"line"`                   //调用行数
	Runtime   uint64 `gorm:"column:runtime;comment:耗时:ns" json:"runtime" form:"runtime"`         // 耗时:ns
	CreatedAt string `gorm:"column:created_at;comment:调用时间" json:"created_at" form:"created_at"` //调用时间
}

函数耗时日志表

type LogSave

type LogSave interface {
	Log(c *LogStruct)     // 记录正常日志信息
	Quest(c *QuestStruct) // 记录Quest的Http请求日志
	Func(c *FuncLog)      // 记录Func的函数调用耗时日志
}

日志写入接口

type LogStruct

type LogStruct struct {
	Id        uint64 `gorm:"column:id;primaryKey;autoIncrement" json:"id" form:"id"`                   // 条目ID
	Type      string `gorm:"column:type;size:1;comment:错误类型;index:error_type" json:"type" form:"type"` //错误类型,如:I、D等
	File      string `gorm:"column:file;size:200;comment:错误文件" json:"file" form:"file"`                //错误文件
	Line      uint   `gorm:"column:line;comment:文件行数;index:error_type" json:"line" form:"line"`        //错误行数
	Msg       string `gorm:"column:msg;size:200;comment:消息概述" json:"msg" form:"msg"`                   //错误消息概述
	Content   string `gorm:"column:content;comment:错误消息内容" json:"content" form:"content"`              //错误信息内容
	CreatedAt string `gorm:"column:created_at;comment:创建时间" json:"created_at" form:"created_at"`       //错误发生时间
}

错误信息统计表

type QuestStruct

type QuestStruct struct {
	Id        uint   `gorm:"column:id;primaryKey;autoIncrement" form:"id" json:"id"`
	Uri       string `gorm:"column:uri;size:200;comment:HTTP请求网址" form:"uri" json:"uri"`                        //HTTP请求网址
	Param     string `gorm:"column:param;comment:请求参数[JSON格式存储]" form:"param" json:"param"`                     //请求参数[JSON格式存储]
	HttpCode  int    `gorm:"column:http_code;comment:HTTP请求返回的Code值" form:"http_code" json:"http_code"`         //HTTP请求返回的Code值
	Body      string `gorm:"column:body;comment:HTTP请求返回值" form:"body" json:"body"`                             //HTTP请求返回值
	Error     string `gorm:"column:error;size:200;comment:HTTP请求报错信息" form:"error" json:"error"`                //HTTP请求报错信息
	Header    string `gorm:"column:header;comment:HTTP请求时发送的Header请求头" form:"header" json:"header"`             //HTTP请求时发送的Header请求头
	StartTime string `gorm:"column:start_time;comment:HTTP请求开始时间" form:"start_time" json:"start_time"`          //HTTP请求开始时间
	EndTime   string `gorm:"column:end_time;comment:HTTP请求结束时间" form:"end_time" json:"end_time"`                //HTTP请求结束时间
	QuestSec  uint   `gorm:"column:quest_sec;comment:请求耗时 单位:毫秒" form:"quest_sec" json:"quest_sec"`             //请求耗时 单位:毫秒
	ClientIp  string `gorm:"column:client_ip;size:60;comment:客户端IP地址,兼容IPV6" form:"client_ip" json:"client_ip"` // 客户端IP地址,兼容IPV6
}

https请求日志记录

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL