xlog

package
v0.2.30 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: BSD-3-Clause-Clear Imports: 9 Imported by: 0

Documentation

Overview

日志记录模块

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AE added in v0.1.29

func AE(msg string, err error) error

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

msg	消息备注
err	错误详情

func Alert added in v0.0.16

func Alert(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func AlertError added in v0.1.1

func AlertError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func CE added in v0.1.29

func CE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Crit added in v0.0.16

func Crit(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func CritError added in v0.1.1

func CritError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func DE added in v0.1.29

func DE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Debug added in v0.0.16

func Debug(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func DebugError added in v0.1.1

func DebugError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func EE added in v0.1.29

func EE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Error added in v0.0.16

func Error(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func ErrorError added in v0.1.1

func ErrorError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Ftime added in v0.1.12

func Ftime(t time.Time)

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

因内部使用了反射机制,所以无法使用匿名函数进行多层函数名获取 所以此处暂时截至到此处,使用 defer xlog.Ftime(time.Now()) 的固定结构来统计时长 另,因内部使用了 go 队列的形式来进行入库,所以建议此函数使用在正常运行的结构中 可随时退出的命令行记录的话建议使用 defer xlog.RTime(time.Now()) 来记录日志

t	当前时间

func GetLogTypeBigs added in v0.1.16

func GetLogTypeBigs(typ string, big bool) []string

获取分类以上的等级列表

typ	基础分类
big	是否查询比此条目高的条目

func IE added in v0.1.29

func IE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Info added in v0.0.16

func Info(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func InfoError added in v0.1.1

func InfoError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func LogMigration added in v0.1.26

func LogMigration()

日志迁移表信息

func NE added in v0.1.29

func NE(msg string) error

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

func NN added in v0.1.29

func NN(msg string) error

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

func Notice added in v0.0.16

func Notice(msg string)

记录详情日志

msg	消息备注

func NoticeError added in v0.1.1

func NoticeError(msg string) error

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

func PE added in v0.1.29

func PE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Panic added in v0.0.16

func Panic(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func PanicError added in v0.1.1

func PanicError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Quest added in v0.1.27

func Quest(h *https.CURL)

记录HTTPS请求日志

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

func QuestJson added in v0.1.27

func QuestJson(s []byte)

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

s	JSON字节码

func Recover added in v0.2.21

func Recover(f ...func())

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

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

func Regedit

func Regedit(c *Config)

注入配置项

func Rtime added in v0.1.12

func Rtime(t time.Time)

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

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

t	当前时间

func SaveAny added in v0.0.17

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

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

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

func SaveAnyError added in v0.1.1

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

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

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

func User added in v0.0.16

func User(uid uint, typ, info string)

记录用户日志

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

func WE added in v0.1.29

func WE(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

func Warning added in v0.0.16

func Warning(msg string, err error)

记录详情日志

msg	消息备注
err	错误详情

func WarningError added in v0.1.1

func WarningError(msg string, err error) error

记录详情日志

msg	消息备注
err	错误详情

Types

type Config

type Config struct {
	DB                *gorm.DB                                               // 数据库连接
	Console           bool                                                   // 是否输出到控制台中
	TableName         string                                                 // 日志记录的数据表
	QuestName         string                                                 // http请求记录
	RunDir            string                                                 // 程序所在目录
	FuncLogName       string                                                 // 函数耗时存储表
	MaxTimes          uint                                                   // 日志中存储日志的最大天数,默认30天
	OldTableName      string                                                 // 默认旧的日志表名称,默认为[log_old]
	OldFuncLogName    string                                                 // 默认旧的函数耗时存储表名称,默认为[log_func_runtime_old]
	OldQuestName      string                                                 // http请求记录历史记录表,默认为[log_quest_name]
	MaxMigrationLimit uint                                                   // 单次迁移最大条数,默认为10000(每次均需要迁移log和log_func,所以最多为MaxMigrationLimit*2条记录)
	InsertAfter       func(log *LogStruct, fun *FuncLog, quest *QuestStruct) // 日志回调【为了避免循环调用,此函数中禁止使用xlog.xxxx进行日志记录】

	LkInsertAfterNum int // 每秒钟回调日志处理函数次数,超过该次数将会丢弃【避免xlog循环记录,默认值为10】
	// contains filtered or unexported fields
}

日志记录模块使用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 added in v0.1.12

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

函数耗时日志表

func (*FuncLog) TableName added in v0.1.12

func (c *FuncLog) TableName() string

获取表名

type LogStruct added in v0.0.16

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

错误信息统计表

func GetLog added in v0.1.16

func GetLog(typ string, big bool, offset, limit int) (int64, []LogStruct, error)

获取日志列表

typ		基础分类
big		是否查询比此条目高的日志条目
offset	跳过条数
limit	查询条数

func (*LogStruct) TableName added in v0.0.16

func (c *LogStruct) TableName() string

获取表名

type OldQuestStruct added in v0.1.27

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

https请求日志记录

func (*OldQuestStruct) TableName added in v0.1.27

func (c *OldQuestStruct) TableName() string

返回所属表名信息

type QuestStruct added in v0.1.27

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

https请求日志记录

func (*QuestStruct) TableName added in v0.1.27

func (c *QuestStruct) TableName() string

返回所属表名信息

type TypeInfo added in v0.1.16

type TypeInfo struct {
	Key    string `json:"key"`
	Name   string `json:"name"`
	NameCN string `json:"name_cn"`
}

func GetLogType added in v0.1.16

func GetLogType() []TypeInfo

获取错误分类列表 此列表会按照错误等级来进行获取

type UserLogStruct added in v0.1.29

type UserLogStruct struct {
	Id        uint64 `gorm:"column:id" json:"id" form:"id"`                         // 条目ID
	Uid       uint   `gorm:"column:line" json:"uid" form:"uid"`                     //错误行数
	LogType   string `gorm:"column:msg" json:"log_type" form:"log_type"`            //错误消息概述
	Content   string `gorm:"column:content" json:"content" form:"content"`          //错误信息内容
	CreatedAt string `gorm:"column:created_at" json:"created_at" form:"created_at"` //错误发生时间
}

用户错误信息返回结构

func GetLogUser added in v0.1.16

func GetLogUser(uid uint, offset, limit int) (int64, []UserLogStruct, error)

获取用户日志记录

uid		用户ID
offset	跳过条数
limit	查询条数

Jump to

Keyboard shortcuts

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