slog

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2022 License: MulanPSL-2.0 Imports: 9 Imported by: 0

README

slog

介绍

slog是一个简单的日志组件库通过简单的配置实现快速的日志记录

本日志库可向控制台日志文件、及任意IO对象写入日志

日志库由纯GO及官方包实现,不依赖任何第三方包

兼容官方log包部分方法

安装教程
go get gitee.com/mowens/slog
使用说明

//配置无顺序要求

1.配置日志输出格式,支持两种输出格式:单行简单结构化

slog.DefWriteModel = WriteSingleLine

WriteSingleLine 单行输出
WriteStructured 进行简单结构化 默认使用简单结构化

2.配置输出方向,当前支持向控制台本地日志文件自定义IO对象

slog.DefPrint = slog.Console

slog.Console 代表控制台 默认控制台
slog.File 代表本地文件
    仅使用日志文件时需要配置第 3、4、5 项    
slog.Other 代表任意IO对象 --满足 io.Writer 接口即可
    使用此模式需要重新实例化写入对象
    slog.NewOutWrite(w) // w 为满足io.Writer接口的实体

3.配置日志文件输出路径 该路径以项目根目录为起点 置空则是项目根目录

slog.LogFilePath = "example/log"

4.配置日志文件名浅醉 无需添加文件名前的目录分割符,程序会自行补全

slog.LogFilePrefix = "slog"

5.配置日志分割模式,按天按小时

slog.DefSplit = slog.Day

slog.Day 按天 默认按天
slog.Hour 按小时

调用日志文件初始化函数

slog.InitLogFile()

所有配置完成,可以写入日志了。 配置设置结束

示例:

slog.DefWriteModel = slog.WriteSingleLine

slog.DefPrint = slog.File

slog.DefSplit = slog.Day

//上述参数均可使用默认值 //下面两项最好还是配置一下

slog.LogFilePath = "example/log"

slog.LogFilePrefix = "slog_test"

slog.InitLogFile()

//输出info级别的日志 --每个日志级别都有其对应的方法

slog.Info("%s", "slog日志组件")

//Printf 方法始终向控制台输出,缺点就是需要添加 日志级别

//Println方法Printf 完全

slog.Printf(slog.DEBUG,"%s", "slog日志组件")

//Panic 方法会向控制台与目标IO对象一起输出Panic信息,但当输出对象是控制台时只会输出一次

slog.Panic("%s", "slog日志组件模拟Panic")

Documentation

Index

Constants

View Source
const (
	ALL logLevel = iota
	DEBUG
	INFO
	WARN
	ERROR
	FATAL
	PANIC
	OFF
)
View Source
const (
	// Console 输出至控制台
	Console printOut = iota
	// File 输出至日志文件
	File
	// Other 输出向其他IO接口
	Other
)
View Source
const (
	Day cut = iota
	Hour
)
View Source
const (
	Kb size = 1024
	Mb      = 1024 * Kb
	Gb      = 1024 * Mb
	Tb      = 1024 * Gb
)

Variables

View Source
var DefLogLevel = INFO

DefLogLevel 默认的日志级别

View Source
var DefPrint printOut = Console

DefPrint 输出方向 设置一个全局的输出方向, 但函数Printf 不受此参数控制,Printf()始终将日志输出到控制台

View Source
var DefSplit cut = Day

DefSplit 日志分割模式

View Source
var LogFilePath = ""

LogFilePath 日志文件输出路径 参考路径为项目根目录 无需添加结尾的 "/" 程序会补全 “log” 日志将会输出到 项目根目录 log/20220801.log

View Source
var LogFilePrefix = ""

LogFilePrefix 日志前缀--可携带路径参数,如: “slog” 日志将会输出到 slog20220801.log

View Source
var WritePool = sync.Pool{New: func() any { return os.NewFile(uintptr(syscall.Stdout), "/dev/stdout") }}

Functions

func All

func All(format string, v ...interface{})

func Debug

func Debug(format string, v ...interface{})

func Debugf added in v0.0.10

func Debugf(format string, v ...interface{})

func Error

func Error(format string, v ...interface{})

func Errorf added in v0.0.10

func Errorf(format string, v ...interface{})

func Fatal

func Fatal(format string, v ...interface{})

func Fatalf added in v0.0.10

func Fatalf(format string, v ...interface{})

func FileNameSuffix

func FileNameSuffix(c cut, unix ...int64) string

FileNameSuffix 根据时间戳计算对应格式的字符串

func GetFileSiz added in v0.0.8

func GetFileSiz(unit size) int64

GetFileSiz 返回当前日志文件大小 注意此函数仅在日志存储模式为日志文件时生效

func GetLogLevel added in v0.0.8

func GetLogLevel() string

func GetPrintTo added in v0.0.8

func GetPrintTo() string

func GetWriteModel added in v0.0.8

func GetWriteModel() string

func Info

func Info(format string, v ...interface{})

func Infof added in v0.0.10

func Infof(format string, v ...interface{})

func InitLogFile added in v0.1.0

func InitLogFile()

func Off

func Off()

func PW added in v0.1.2

func PW(b []byte) (n int, err error)

func Panic added in v0.1.0

func Panic(format string, v ...interface{})

func Panicf added in v0.1.0

func Panicf(format string, v ...interface{})

func Printf

func Printf(lv logLevel, format string, v ...interface{})

Printf 向控制台输出日志 lv 日志登录

func Println added in v0.0.10

func Println(lv logLevel, format string, v ...interface{})

func RunPath

func RunPath(skip int) (filepath, line, funcName string)

RunPath 获取运行函数所在文件路径、行号、函数名 runtime.Caller(2) 获取前2层调用者信息

func TimeNanoToString

func TimeNanoToString(unix ...int64) string

TimeNanoToString 获取当前时间转为人类可读模式--纳秒

func TimeToString

func TimeToString(unix ...int64) string

TimeToString 获取当天日期

func Warn

func Warn(format string, v ...interface{})

func Warnf added in v0.0.10

func Warnf(format string, v ...interface{})

func WriteSlog

func WriteSlog(lv logLevel, body string) *slog

Types

type InputWriter added in v0.1.2

type InputWriter struct{}

func (*InputWriter) Write added in v0.1.2

func (iw *InputWriter) Write(b []byte) (n int, err error)

type LogOutWrite added in v0.1.0

type LogOutWrite struct {
	// contains filtered or unexported fields
}

LogOutWrite 向日志文件输出

func NewLogFile added in v0.1.0

func NewLogFile() (log *LogOutWrite, err error)

func (*LogOutWrite) FileName added in v0.1.0

func (l *LogOutWrite) FileName(timestamp int64) string

func (LogOutWrite) Name added in v0.1.0

func (l LogOutWrite) Name() string

func (*LogOutWrite) Size added in v0.1.0

func (l *LogOutWrite) Size(unit size) int64

func (*LogOutWrite) Sync added in v0.1.0

func (l *LogOutWrite) Sync() error

func (*LogOutWrite) Write added in v0.1.0

func (l *LogOutWrite) Write(p []byte) (n int, err error)

type OutWriter added in v0.1.0

type OutWriter interface {
	io.Writer
	Sync() error
	Name() string
	Size(unit size) int64
}
var LogWrite OutWriter

func NewOutWrite added in v0.1.0

func NewOutWrite(w io.Writer) OutWriter

NewOutWrite 向自定义IO对象输出

type WriteModel

type WriteModel int

WriteModel 日志写入模式 当前提供结构化与单行写入

const (
	// WriteSingleLine 单行模式 所有信息都保存到一行
	WriteSingleLine WriteModel = iota
	// WriteStructured 结构化模式
	WriteStructured
)
var DefWriteModel WriteModel = WriteStructured

DefWriteModel 默认的日志写入模式

type XSlog added in v0.1.0

type XSlog interface {
	ToString() string
	ToBytes() []byte
	LogLv() logLevel
	// contains filtered or unexported methods
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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