rlog

package module
v1.1.9 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 12 Imported by: 1

README

打印彩色日志

默认

1

全彩

2

支持的打印与设置

type RLogger interface {
    Print(a ...any)
    Println(a ...any)
    Printf(format string, a ...any)
    
    Debug(a ...any)
    DebugF(format string, a ...any)
    Info(a ...any)
    InfoF(format string, a ...any)
    Success(a ...any)
    SuccessF(format string, a ...any)
    Warn(a ...any)
    WarnF(format string, a ...any)
    Error(a ...any)
    ErrorF(format string, a ...any)
    Panic(a ...any)
    PanicF(format string, a ...any)
    ExitError(a ...any)          //退出程序并且打印错误,并且立即退出关闭程序,os.Exit(1)。
    ExitErrorAndSleep3(a ...any) //退出程序并且打印错误,延时3秒后退出关闭程序,os.Exit(1)。
    Recover()                    //协程崩溃恢复,并且打印崩溃日志。
    recover(content string)
    
    PrintlnConsoleMust(a ...any)     //强制打印到控制台日志
    PrintlnFileMust(a ...any)        //强制打印到文件
    PrintlnConsoleMustOnly(a ...any) //强制只打印到控制台日志
    PrintlnFileMustOnly(a ...any)    //强制只打印到文件
    
    SetLogFilePath(filepath string, oldLogToZip bool) //设置日志文件路径
    SetConsoleLevel(level Level)                      //设置控制台日志打印等级
    SetFileLevel(level Level)                         //设置文件日志打印等级
    SetFullTextStaining(full bool)                    //设置是否全文着色,默认false:tag 着色。
    SetAutoDeleteOldLogFile(day uint)                 //设置自动删除多少天前的日志
    SetLogFileMaxSize(mb uint)                        //设置日志文件最大大小,MB
    
    SetRuntimeCaller(skip int)
}

优雅的初始化

package main

import (
    "time"
    
    "gitee.com/ruige_fun/rlog"
)

func init() {
    rlog.Init()                    //这个必须在项目的main.go里面初始化,会去计算项目文件所在的文件夹。
    rlog.SetFullTextStaining(true) //设置为全彩打印日志
}

func main() {
    rlog.Print("你好呀", time.Now().Year(), "哈哈哈")
    rlog.Printf("你好呀%v哈哈哈", time.Now().Year())
    rlog.Println("你好呀", time.Now().Year(), "哈哈哈")
    rlog.Debug("你好呀")
    rlog.Info("你好呀")
    rlog.Success("你好呀")
    rlog.Warn("你好呀")
    rlog.Error("你好呀")
    rlog.Panic("你好呀")
}

推荐的初始化方式

package main

import (
    "os"
    "os/signal"
    "syscall"
    "time"
    
    "gitee.com/ruige_fun/rlog"
)

func init() {
    rlog.Init() //这个必须在项目的main.go里面初始化,会去计算项目文件所在的文件夹。
    rlog.SetFullTextStaining(true) //设置为全彩打印日志
}

func main() {
    var ch = make(chan os.Signal)  //信号通道
    
    go PrintInfo() //开个协程去执行服务代码
    
    signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
    <-ch
    rlog.WaitFinish() //安全结束日志打印
}

func PrintInfo() {
    for {
        rlog.Print("你好呀", time.Now().Year(), "哈哈哈")
        rlog.Printf("你好呀%v哈哈哈", time.Now().Year())
        rlog.Println("你好呀", time.Now().Year(), "哈哈哈")
        rlog.Debug("你好呀")
        rlog.Info("你好呀")
        rlog.Success("你好呀")
        rlog.Warn("你好呀")
        rlog.Error("你好呀")
        rlog.Panic("你好呀")
        time.Sleep(time.Second)
    }
}

特点

  • 支持彩色打印日志
  • 支持打印到控制台和打印到日志文件
  • 支持日志文件切割,按日志文件大小切割
  • 支持切割的旧日志文件自动按日期重命名,并且压缩为zip文件。
  • 支持日志打印级别:Debug,Info,Success,Warn,Error,Panic。

Documentation

Index

Constants

View Source
const (
	LevelDebug = iota
	LevelInfo
	LevelSuccess
	LevelWarn
	LevelError
	LevelPanic
	LevelDisable = 100
)
View Source
const (
	LogFileModel = 0660

	KB = 1 << 10 //千字节
	MB = 1 << 20 //兆字节
	GB = 1 << 30 //千兆字节
	TB = 1 << 40 //太字节
	PB = 1 << 50 //拍字节
	EB = 1 << 60 //艾字节

	DefaultLogFileMaxSize = MB * 10
)

Variables

This section is empty.

Functions

func Debug

func Debug(a ...any)

func DebugF added in v1.1.4

func DebugF(format string, a ...any)

func Error

func Error(a ...any)

func ErrorF added in v1.1.4

func ErrorF(format string, a ...any)

func ExitError added in v1.1.2

func ExitError(a ...any)

func ExitErrorAndSleep3 added in v1.1.2

func ExitErrorAndSleep3(a ...any)

func Info

func Info(a ...any)

func InfoF added in v1.1.4

func InfoF(format string, a ...any)

func Init added in v1.1.0

func Init()

Init 初始化,需要在 main函数或者main.init里调用。 用于计算出项目文件夹地址。

func InitCustomLayer added in v1.1.3

func InitCustomLayer(CustomLayer uint)

InitCustomLayer 初始化,自定义层数。 如果在项目文件夹根目录,则填入1;如果在二级目录,填入2;三级目录填入3。 用于计算出项目文件夹地址。

func Panic

func Panic(a ...any)

func PanicF added in v1.1.4

func PanicF(format string, a ...any)

func Print added in v1.1.0

func Print(a ...any)

func Printf added in v1.1.0

func Printf(format string, a ...any)

func Println added in v1.1.0

func Println(a ...any)

func PrintlnConsoleMust added in v1.1.0

func PrintlnConsoleMust(a ...any)

PrintlnConsoleMust 强制打印到控制台日志

func PrintlnConsoleMustOnly added in v1.1.0

func PrintlnConsoleMustOnly(a ...any)

PrintlnConsoleMustOnly 强制只打印到控制台日志

func PrintlnFileMust added in v1.1.0

func PrintlnFileMust(a ...any)

PrintlnFileMust 强制打印到文件

func PrintlnFileMustOnly added in v1.1.0

func PrintlnFileMustOnly(a ...any)

PrintlnFileMustOnly 强制只打印到文件

func ProjectDir added in v1.1.0

func ProjectDir() string

ProjectDir 返回项目main函数的文件夹地址

func Recover added in v1.1.6

func Recover()

Recover 崩溃恢复,并且打印崩溃日志。

func SetAutoDeleteOldLogFile added in v1.1.0

func SetAutoDeleteOldLogFile(day uint)

SetAutoDeleteOldLogFile 设置自动删除多少天前的日志

func SetConsoleLevel added in v1.1.0

func SetConsoleLevel(level Level)

SetConsoleLevel 设置控制台日志打印等级

func SetFileLevel added in v1.1.0

func SetFileLevel(level Level)

SetFileLevel 设置文件日志打印等级

func SetFullTextStaining added in v1.1.0

func SetFullTextStaining(full bool)

SetFullTextStaining 设置是否全文着色,默认false:tag 着色。

func SetLogFileMaxSize added in v1.1.0

func SetLogFileMaxSize(mb uint)

SetLogFileMaxSize 设置日志文件最大大小,MB

func SetLogFilePath

func SetLogFilePath(filepath string, oldLogToZip bool)

SetLogFilePath 设置日志文件路径

func SetProjectDir added in v1.1.3

func SetProjectDir(dir string)

SetProjectDir 手动设置项目文件夹地址,如果main.go在项目的根目录,请使用 Init 函数,该函数可以自动计算项目所在文件夹位置。

func Success added in v1.1.0

func Success(a ...any)

func SuccessF added in v1.1.4

func SuccessF(format string, a ...any)

func WaitFinish added in v1.1.0

func WaitFinish()

WaitFinish 安全结束日志打印

func Warn

func Warn(a ...any)

func WarnF added in v1.1.4

func WarnF(format string, a ...any)

Types

type Level added in v1.1.0

type Level byte

type RLogger added in v1.1.0

type RLogger interface {
	Print(a ...any)
	Println(a ...any)
	Printf(format string, a ...any)

	Debug(a ...any)
	DebugF(format string, a ...any)
	Info(a ...any)
	InfoF(format string, a ...any)
	Success(a ...any)
	SuccessF(format string, a ...any)
	Warn(a ...any)
	WarnF(format string, a ...any)
	Error(a ...any)
	ErrorF(format string, a ...any)
	Panic(a ...any)
	PanicF(format string, a ...any)
	ExitError(a ...any)          //退出程序并且打印错误,并且立即退出关闭程序,os.Exit(1)。
	ExitErrorAndSleep3(a ...any) //退出程序并且打印错误,延时3秒后退出关闭程序,os.Exit(1)。
	Recover()                    //协程崩溃恢复,并且打印崩溃日志。

	PrintlnConsoleMust(a ...any)     //强制打印到控制台日志
	PrintlnFileMust(a ...any)        //强制打印到文件
	PrintlnConsoleMustOnly(a ...any) //强制只打印到控制台日志
	PrintlnFileMustOnly(a ...any)    //强制只打印到文件

	SetLogFilePath(filepath string, oldLogToZip bool) //设置日志文件路径
	SetConsoleLevel(level Level)                      //设置控制台日志打印等级
	SetFileLevel(level Level)                         //设置文件日志打印等级
	SetFullTextStaining(full bool)                    //设置是否全文着色,默认false:tag 着色。
	SetAutoDeleteOldLogFile(day uint)                 //设置自动删除多少天前的日志
	SetLogFileMaxSize(mb uint)                        //设置日志文件最大大小,MB

	SetRuntimeCaller(skip int)
	// contains filtered or unexported methods
}

func NewLogger added in v1.1.0

func NewLogger(stdout io.Writer) RLogger

NewLogger 新建一个日志管理器 windows彩色日志:import "github.com/mattn/go-colorable" 参数传 colorable.NewColorableStdout()

Jump to

Keyboard shortcuts

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