AyaLog

package module
v2.1.5 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2023 License: MPL-2.0 Imports: 8 Imported by: 4

README

AyaLog

适用于Web后端的Golang Log库

使用简单,适配Gin,Gorm,实现了基础的Log功能,例如日志级别,按时间分割日志与中间件支持,适用于轻量的Log记录需求

🎬 如何使用

安装

go get -u github.com/nyancatda/AyaLog/v2

基础功能

例子:
package main

import (
	"errors"

	"github.com/nyancatda/AyaLog/v2"
)

func main() {
	// 创建一个默认日志实例
	Log := AyaLog.NewLog()
	// 配置日志实例
	Log.Level = AyaLog.DEBUG // 设置日志等级

	// 打印DeBug日志
	Log.DeBug("System", "This is a debug message") // 2022-05-22 23:57:38 DEBUG [System] This is a debug message
	// 打印Info日志
	Log.Info("System", "This is a info message") // 2022-05-22 23:57:38 INFO [System] This is a info message
	// 打印Warning日志
	Log.Warning("System", "This is a warning message") // 2022-05-22 23:57:38 WARNING [System] This is a warning message
	// 打印Error日志
	Log.Error("System", errors.New("This is a error message")) // 2022-05-22 23:57:38 ERROR [System] This is a error message

	// 为打印的文本设置颜色
	Log.Info("System", "This is "+AyaLog.Green("Green"))
	// 为打印的文本设置背景颜色
	Log.Info("System", "This is "+AyaLog.GreenBackground("GreenBackground"))
}

为Gin日志启用

安装Gin日志模块
go get -u github.com/nyancatda/AyaLog/Module/GinLog
注册模块提供的日志中间件
r.Use((GinLog.GinLog(*Log)))
例子
package main

import (
	"os"

	"github.com/gin-gonic/gin"
	"github.com/nyancatda/AyaLog/Module/GinLog"
	"github.com/nyancatda/AyaLog/v2"
)

func main() {
	// 创建一个默认日志实例
	Log := AyaLog.NewLog()

	// 关闭Gin默认的日志输出
	gin.DefaultWriter = os.Stdin
	// 初始化GIN
	r := gin.Default()
	// 注册日志中间件
	r.Use((GinLog.GinLog(*Log)))

	// 运行
	if err := r.Run(":8000"); err != nil {
		Log.Error("GIN", err)
	}
}

为Gorm日志启用

安装Gorm日志模块
go get -u github.com/nyancatda/AyaLog/Module/GormLog
将Logger设置为模块提供的接口
ConnectDB, err := gorm.Open(mysql.Open(ConnectInfo), &gorm.Config{
	Logger: GormLog.GormLog{Log: *Log},
})
例子
package main

import (
	"github.com/nyancatda/AyaLog/Module/GormLog"
	"github.com/nyancatda/AyaLog/v2"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func main() {
	// 创建一个默认日志实例
	Log := AyaLog.NewLog()

	// 配置数据库信息
	MySQLUser := ""
	MySQLPassword := ""
	MySQLIp := ""
	MySQLDatabase := ""

	ConnectInfo := MySQLUser + ":" + MySQLPassword + "@tcp(" + MySQLIp + ")/" + MySQLDatabase + "?charset=utf8mb4&parseTime=True&loc=Local"

	//创建MySQL连接
	ConnectDB, err := gorm.Open(mysql.Open(ConnectInfo), &gorm.Config{
		Logger: GormLog.GormLog{Log: *Log}, // Logger设置为AyaLog的GormLog模块
	})
	if err != nil {
		Log.Error("Gorm", err)
	}

	// 关闭连接
	SQLDB, err := ConnectDB.DB()
	if err != nil {
		Log.Error("Gorm", err)
	}
	defer SQLDB.Close()
}

启用自动压缩与清理日志文件

安装定时任务模块
go get -u github.com/nyancatda/AyaLog/Module/TimedTask
直接启用定时任务

直接启动默认的定时任务,每天压缩日志文件,每天清理7天前的日志文件

package main

import (
	"github.com/nyancatda/AyaLog/Module/TimedTask"
	"github.com/nyancatda/AyaLog/v2"
)

func main() {
	// 创建一个默认日志实例
	Log := AyaLog.NewLog()

	// 启动定时任务
	go TimedTask.Start(*Log)

	Log.Info("System", "定时任务启动")
}
自定义定时任务

使用模块提供的函数,自定义定时任务,推荐使用jasonlvhit/gocron

package main

import (
	"github.com/jasonlvhit/gocron"
	"github.com/nyancatda/AyaLog/Module/TimedTask"
	"github.com/nyancatda/AyaLog/v2"
)

func main() {
	// 创建一个默认日志实例
	Log := AyaLog.NewLog()

	// 新建一个线程来执行定时任务
	go func() {
		// 初始化定时任务
		Task := gocron.NewScheduler()

		Task.Every(1).Day().Do(TimedTask.CompressLogs, *Log) // 每天执行一次日志压缩任务
		Task.Every(1).Day().Do(TimedTask.CleanFile, *Log, 7) // 每天执行一次日志清理任务,清理7天前的日志文件

		// 开始执行定时任务
		<-Task.Start()
	}()

	Log.Info("System", "定时任务启动")
}

使用中间件

AyaLog提供了中间件的支持,可以方便的在日志打印前后进行调整和上报等操作

打印前中间件

打印前中间件会在Print函数的最开始执行,即使使用OFF日志等级也会执行,且无视日志等级设定

Log.UseBefore(func(Level *int, Source *string, Text ...*any) {
	fmt.Println("I will print before")

	// 可以使用指针修改日志内容
	*Source = "Middleware"
})
打印后中间件

打印后中间件会在Print函数末尾执行,内容会受到打印前中间件的影响,且无法修改日志内容

Log.UseAfter(func(Level int, Source string, Text ...any) {
	fmt.Println("I will print after")
})
完整示例
package main

import (
	"fmt"

	"github.com/nyancatda/AyaLog/v2"
)

func main() {
	// 创建一个默认日志实例
	Log := AyaLog.NewLog()

	// 添加打印前中间件
	Log.UseBefore(func(Level *int, Source *string, Text ...*any) {
		fmt.Println("I will print before")

		*Source = "Middleware"
	})

	// 添加打印后中间件
	Log.UseAfter(func(Level int, Source string, Text ...any) {
		fmt.Println("I will print after")
	})

	// 打印Info日志
	Log.Info("System", "This is a info message")
}

此示例会输出

I will print before
2022-05-22 23:57:38 INFO [Middleware] This is a info message
I will print after

📖 许可证

项目采用Mozilla Public License Version 2.0协议开源

二次修改源代码需要开源修改后的代码,对源代码修改之处需要提供说明文档

Documentation

Overview

* @Author: NyanCatda * @Date: 2022-05-22 00:02:30 * @LastEditTime: 2022-05-22 22:41:22 * @LastEditors: NyanCatda * @Description: 终端输出增加颜色 * @FilePath: \AyaLog\Color.go

* @Author: NyanCatda * @Date: 2022-11-26 16:45:50 * @LastEditTime: 2023-01-07 17:22:41 * @LastEditors: NyanCatda * @Description: 文件操作 * @FilePath: \AyaLog\File.go

* @Author: NyanCatda * @Date: 2022-05-22 00:03:28 * @LastEditTime: 2023-01-07 22:02:49 * @LastEditors: NyanCatda * @Description: 日志模块 * @FilePath: \AyaLog\Log.go

* @Author: NyanCatda * @Date: 2023-01-07 21:56:56 * @LastEditTime: 2023-01-07 22:03:16 * @LastEditors: NyanCatda * @Description: 中间件实现 * @FilePath: \AyaLog\Middleware.go

* @Author: NyanCatda * @Date: 2022-11-26 16:50:36 * @LastEditTime: 2023-05-20 20:58:51 * @LastEditors: NyanCatda * @Description: 打印日志 * @FilePath: \AyaLog\Print.go

Index

Constants

View Source
const (
	TextBlack = iota + 30
	TextRed
	TextGreen
	TextYellow
	TextBlue
	TextMagenta
	TextCyan
	TextGrey
)
View Source
const (
	BackgroundBlack = iota + 40
	BackgroundRed
	BackgroundGreen
	BackgroundYellow
	BackgroundBlue
	BackgroundMagenta
	BackgroundCyan
	BackgroundWhite
)

定义背景颜色

View Source
const (
	DEBUG = iota + 0
	INFO
	WARNING
	ERROR
	OFF // 关闭日志
)

定义日志等级

View Source
const (
	TextWhite = 1
)

定义文字颜色

Variables

This section is empty.

Functions

func Black

func Black(msg string) string

*

  • @description: 设置文字颜色为黑色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func BlackBackground

func BlackBackground(msg string) string

*

  • @description: 设置背景颜色为黑色,文字颜色为白色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func Blue

func Blue(msg string) string

*

  • @description: 设置文字颜色为蓝色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func BlueBackground

func BlueBackground(msg string) string

*

  • @description: 设置背景颜色为蓝色,文字颜色为白色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func Cyan

func Cyan(msg string) string

*

  • @description: 设置文字颜色为青蓝色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func CyanBackground

func CyanBackground(msg string) string

*

  • @description: 设置背景颜色为青蓝色,文字颜色为白色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func DelColor

func DelColor(msg string) string

*

  • @description: 去除文字颜色
  • @param {string} msg 需要去除颜色的文字
  • @return {string} 去除颜色后的文字

func Green

func Green(msg string) string

*

  • @description: 设置文字颜色为绿色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func GreenBackground

func GreenBackground(msg string) string

*

  • @description: 设置背景颜色为绿色,文字颜色为白色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func Magenta

func Magenta(msg string) string

*

  • @description: 设置文字颜色为紫红色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func MagentaBackground

func MagentaBackground(msg string) string

*

  • @description: 设置背景颜色为紫红色,文字颜色为白色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func Red

func Red(msg string) string

*

  • @description: 设置文字颜色为红色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func RedBackground

func RedBackground(msg string) string

*

  • @description: 设置背景颜色为红色,文字颜色为白色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func SetColor

func SetColor(msg string, conf, bg, text int) string

*

  • @description: 设置文字颜色与背景颜色
  • @param {string} msg 需要设置颜色的文字
  • @param {int} conf 颜色配置
  • @param {int} bg 设置背景颜色
  • @param {int} text 设置文字颜色
  • @return {string} 设置颜色后的文字

func White

func White(msg string) string

*

  • @description: 设置文字颜色为白色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func WhiteBackground

func WhiteBackground(msg string) string

*

  • @description: 设置背景颜色为白色,文字颜色为黑色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func Yellow

func Yellow(msg string) string

*

  • @description: 设置文字颜色为黄色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

func YellowBackground

func YellowBackground(msg string) string

*

  • @description: 设置背景颜色为黄色,文字颜色为白色
  • @param {string} msg 需要设置颜色的文字
  • @return {string} 设置颜色后的文字

Types

type Log

type Log struct {
	Path            string // 日志文件保存路径
	Segmentation    string // 日志文件分割标识(使用go默认时间格式)
	WriteFile       bool   // 是否写入文件
	ColorPrint      bool   // 是否打印颜色
	Level           int    // 日志等级
	Prefix          string // 日志前缀
	PrefixWriteFile bool   // 日志前缀是否写入文件
	Suffix          string // 日志后缀
	SuffixWriteFile bool   // 日志后缀是否写入文件
	PrintErrorStack bool   // 是否打印错误堆栈
	// contains filtered or unexported fields
}

func NewLog

func NewLog() *Log

*

  • @description: 创建一个默认日志实例
  • @param {*}
  • @return {Log} 日志实例

func (*Log) DeBug

func (Log *Log) DeBug(Source string, Text ...any)

*

  • @description: 打印DeBug错误
  • @param {string} Source 日志来源
  • @param {...any} Text 日志内容
  • @return {*}

func (*Log) Error

func (Log *Log) Error(Source string, Error error, Text ...any)

*

  • @description: 打印错误
  • @param {string} Source 日志来源
  • @param {error} Error 错误信息
  • @param {...any} Text 日志内容
  • @return {*}

func (*Log) Info

func (Log *Log) Info(Source string, Text ...any)

*

  • @description: 打印信息
  • @param {string} Source 日志来源
  • @param {...any} Text 日志内容
  • @return {*}

func (*Log) Print

func (Log *Log) Print(Source string, Level int, Text ...any) error

*

  • @description: 标准日志打印
  • @param {string} Source 日志来源
  • @param {int} Level 日志等级 DEBUG/INFO/WARNING/ERROR/OFF
  • @param {...any} Text 日志内容
  • @return {error} error

func (*Log) UseAfter added in v2.1.3

func (Log *Log) UseAfter(Func func(Level int, Source string, Text ...any))

*

  • @description: 添加日志打印后中间件
  • @param {int} Func Level 日志等级 DEBUG(0)/INFO(1)/WARNING(2)/ERROR(3)/OFF(4)
  • @param {string} Func Source 日志来源
  • @param {...any} Func Text 日志内容
  • @return {*}

func (*Log) UseBefore added in v2.1.3

func (Log *Log) UseBefore(Func func(Level *int, Source *string, Text ...*any))

*

  • @description: 添加日志打印前中间件
  • @param {*int} Func Level 日志等级 DEBUG(0)/INFO(1)/WARNING(2)/ERROR(3)/OFF(4)
  • @param {*string} Func Source 日志来源
  • @param {...*any} Func Text 日志内容
  • @return {*}

func (*Log) Warning

func (Log *Log) Warning(Source string, Text ...any)

*

  • @description: 打印警告
  • @param {string} Source 日志来源
  • @param {...any} Text 日志内容
  • @return {*}

type LogPrint

type LogPrint interface {
	Print(Source string, Level int, Text ...any) error // 打印日志
	Error(Source string, Error error, Text ...any)     // 打印错误
	Warning(Source string, Text ...any)                // 打印警告
	Info(Source string, Text ...any)                   // 打印信息
	DeBug(Source string, Text ...any)                  // 打印DeBug信息
}

Jump to

Keyboard shortcuts

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