log

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2023 License: MIT Imports: 14 Imported by: 0

README

go-log

介绍

golang简单异步日志库

软件架构

软件架构说明

安装教程
  1. xxxx
  2. xxxx
  3. xxxx
使用说明
  1. xxxx
  2. xxxx
  3. xxxx
参与贡献
  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
特技
  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/

Documentation

Index

Constants

View Source
const (
	CUTDUR_M   time.Duration = time.Minute
	CUTDUR_M5                = time.Minute * 5
	CUTDUR_M10               = time.Minute * 10
	CUTDUR_M15               = time.Minute * 15
	CUTDUR_M20               = time.Minute * 20
	CUTDUR_M30               = time.Minute * 30
	CUTDUR_H                 = time.Hour
	CUTDUR_H2                = time.Hour * 2
	CUTDUR_H3                = time.Hour * 3
	CUTDUR_H4                = time.Hour * 4
	CUTDUR_H6                = time.Hour * 6
	CUTDUR_H8                = time.Hour * 8
	CUTDUR_H12               = time.Hour * 12
	//以下遇每月1号分割
	CUTDUR_D  = time.Hour * 24 //每1天分割,取值范围[1D,2D)
	CUTDUR_D2 = CUTDUR_D * 2   //每2天分割,取值范围[2D,3D)
	CUTDUR_D3 = CUTDUR_D * 3   //每3天分割,取值范围[3D,4D)
	CUTDUR_D4 = CUTDUR_D * 4   //每4天分割,取值范围[4D,5D)
	CUTDUR_D5 = CUTDUR_D * 5   //每5天分割,取值范围[5D,6D)
	//
	CUTDUR_D6  = 1              //按周分割,从日志开始记录按周分割,取值范围[6D,7D)
	CUTDUR_D7  = CUTDUR_D * 7   //按周分割,每周一进行分割,取值范围[7D,8D)
	CUTDUR_D8  = 2              //按周分割,每周六进行分割,取值范围[8D,9D)
	CUTDUR_D9  = 3              //按周分割,每周天进行分割,取值范围[9D,10D)
	CUTDUR_D10 = CUTDUR_D * 10  //按旬分割,每月1号11号21号进行分割,取值范围[10D,15D)
	CUTDUR_D15 = CUTDUR_D * 15  //按半月分割,每月1号16号进行分割,取值范围[16D,28D)
	CUTDUR_D30 = CUTDUR_D * 30  //按月分割,取值范围[28D,90D)
	CUTDUR_Q   = CUTDUR_D30 * 3 //按季分割,取值范围[90D,180D)
	CUTDUR_Q2  = CUTDUR_Q * 2   //按半年分割,取整范围[180D,360D)
	CUTDUR_Y   = CUTDUR_Q * 4   //按年分割,取整范围[360D,+∞)
)

按时间分割日志

View Source
const (
	Lv_Fatal Levels = FATAL
	Lv_Error        = Lv_Fatal | ERROR
	Lv_Warn         = Lv_Error | WARN
	Lv_Info         = Lv_Warn | INFO
	Lv_Debug        = Lv_Info | DEBUG
)
View Source
const DefaultDatetimeLayout = "2006-01-02T15:04:05.000"
View Source
const DefaultMaxSize = 100 * 1024 * 1024 //100M
View Source
const FilenameLayout = "20060102150405"

Variables

View Source
var (
	ErrNoSpace = errors.New("no space")
)

Functions

func Debug

func Debug(args ...any) error

func Debugf

func Debugf(format string, args ...any) error

func Error

func Error(args ...any) error

func Errorf

func Errorf(format string, args ...any) error

func Fatal

func Fatal(args ...any) error

func Fatalf

func Fatalf(format string, args ...any) error

func Info

func Info(args ...any) error

func Infof

func Infof(format string, args ...any) error

func LevelString

func LevelString(lv Level) string

func Log

func Log(lv Level, args ...any) error

日志输出全局接口

func LogDepth

func LogDepth(calldepth int, lv Level, args ...any) error

calldepth从0开始计算

func LogDepthf

func LogDepthf(calldepth int, lv Level, format string, args ...any) error

func LogDepthr added in v0.0.3

func LogDepthr(calldepth int, lv Level, r io.Reader) error

func Logf

func Logf(lv Level, format string, args ...any) error

func Logr added in v0.0.3

func Logr(lv Level, r io.Reader) error

func SetLevelName

func SetLevelName(f Level2Str)

func SetLevelParse

func SetLevelParse(f Str2Level)

func SetLogger

func SetLogger(logger Logger)

设置标准日志接口

func Warn

func Warn(args ...any) error

func Warnf

func Warnf(format string, args ...any) error

Types

type ByteBuffer

type ByteBuffer []byte

func (ByteBuffer) ReadBytes

func (b ByteBuffer) ReadBytes(n int, cb func([]byte)) (int, error)

type BytesReader

type BytesReader interface {
	ReadBytes(n int, cb func([]byte)) (int, error)
}

type FileCut

type FileCut func(string)

文件切换通知函数,参数是已经完成的文件名

type Filer

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

func NewFiler

func NewFiler(opts ...FilerOption) (*Filer, error)

func (*Filer) Close

func (f *Filer) Close() error

func (*Filer) Write

func (f *Filer) Write(content []byte) (int, error)

type FilerOption

type FilerOption func(*Filer)

func FWFileCut

func FWFileCut(fc FileCut) FilerOption

文件切换通知

func FileDelete

func FileDelete(h, m, s int) FilerOption

循环删除时间节点,参数未时分秒

func FileDuration

func FileDuration(d time.Duration) FilerOption

按时间分割文件

func FileExt

func FileExt(ext string) FilerOption

func FileMaxSize

func FileMaxSize(m int) FilerOption

日志文件最大字节数

func FileRoot

func FileRoot(p string) FilerOption

func FileSaveDays

func FileSaveDays(m int) FilerOption

日志最多保存时长

type Formatter

type Formatter interface {
	//calldepth 调用层数,需要展示的最顶层第0层
	//w 写入接口
	//lv 日志类型
	//后面包含日志参数
	//返回日志填充的缓存
	Sprint(calldepth int, w io.Writer, lv Level, args ...any) (int, error)
	Sprintf(calldepth int, w io.Writer, lv Level, format string, args ...any) (int, error)
	Sprintr(calldepth int, w io.Writer, lv Level, r io.Reader) (int, error)
}

type FormatterOption

type FormatterOption func(*formatterOptions)

func FDatetimeLayout

func FDatetimeLayout(layout string) FormatterOption

func FLineFeed

func FLineFeed() FormatterOption

func FMsgprefix

func FMsgprefix(a ...any) FormatterOption

func FProperties

func FProperties(p Properties) FormatterOption

type Level

type Level uint
const (
	FATAL Level = 1 << iota
	ERROR
	WARN
	INFO
	DEBUG
)
const NOLOG Level = 0

func ParseLevel

func ParseLevel(lv string) Level

func (Level) String

func (x Level) String() string

type Level2Str

type Level2Str func(Level) string

type Levels

type Levels = Level

func NewLevel

func NewLevel(ct string) Levels

func (Levels) Enable

func (x Levels) Enable(lv Level) bool

type LogOption

type LogOption func(*logger)

func LogCache

func LogCache(size int) LogOption

func LogFormatter

func LogFormatter(formatter Formatter) LogOption

func LogMustLevels

func LogMustLevels(lvs Levels) LogOption

func LogSetWriter

func LogSetWriter(writer LogWriter) LogOption

type LogWriter

type LogWriter interface {
	io.Writer
	//获取可写日志类型
	GetLevels() Levels
	//判断是否写入
	IsWrite(lv Level) bool
	//写日志
	WriteLog(lv Level, content []byte)
	//关闭
	io.Closer
}

func NewConsoleWriter

func NewConsoleWriter(opts ...LogWriterOption) LogWriter

控制台输出

func NewLogWriter

func NewLogWriter(opts ...LogWriterOption) LogWriter

type LogWriterOption

type LogWriterOption func(*logWriterOptions)

func LWErrHandler

func LWErrHandler(eh WriterErrHandler) LogWriterOption

func LWLevel

func LWLevel(lv Levels) LogWriterOption

func LWStrLevel

func LWStrLevel(lv string) LogWriterOption

func LWithWriter

func LWithWriter(writer io.Writer) LogWriterOption

type Logger

type Logger interface {
	//标准接口
	Log(lv Level, args ...any) error
	Logf(lv Level, format string, args ...any) error
	Logr(lv Level, r io.Reader) error //写大块日志
	//自定义calldepth,与log库一样
	LogDepth(calldepth int, lv Level, args ...any) error
	LogDepthf(calldepth int, lv Level, format string, args ...any) error
	LogDepthr(calldepth int, lv Level, r io.Reader) error //写大块日志
	//关闭日志
	io.Closer
}

日志接口定义

func GetLogger

func GetLogger() Logger

func NewLogger

func NewLogger(opts ...LogOption) Logger

type MultiWriter

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

多个输出终端

func NewMultiWriter

func NewMultiWriter(ws ...LogWriter) *MultiWriter

func (*MultiWriter) Close

func (w *MultiWriter) Close() error

func (*MultiWriter) GetLevels

func (w *MultiWriter) GetLevels() Levels

func (*MultiWriter) IsWrite

func (w *MultiWriter) IsWrite(lv Level) bool

func (*MultiWriter) Write

func (w *MultiWriter) Write(content []byte) (int, error)

func (*MultiWriter) WriteLog

func (w *MultiWriter) WriteLog(lv Level, content []byte)

type PipeWriter

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

异步输出终端

func NewPipeWriter

func NewPipeWriter(opts ...LogWriterOption) *PipeWriter

func (*PipeWriter) Close

func (w *PipeWriter) Close() error

func (*PipeWriter) GetLevels

func (w *PipeWriter) GetLevels() Levels

func (*PipeWriter) IsWrite

func (w *PipeWriter) IsWrite(lv Level) bool

func (*PipeWriter) Write

func (w *PipeWriter) Write(content []byte) (int, error)

func (*PipeWriter) WriteLog

func (w *PipeWriter) WriteLog(lv Level, content []byte)

type Properties

type Properties uint

properties

const (
	Ldatetime Properties = 1 << iota // the date in the local time zone: 2006-01-02
	LUTC                             // if Ldate or Ltime is set, use UTC rather than the local time zone
	Llevel                           // the level of the log
	Lfunc                            // full file name and line number: gitee.com/a/b.c:23
	LineFeed                         // End with newline

	LstdFlags = Ldatetime | Llevel | Lfunc | LineFeed // initial values for the standard logger
)

func (Properties) Enable

func (x Properties) Enable(y Properties) bool

func (*Properties) Remove

func (x *Properties) Remove(y Properties)

type RingBuffer

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

缓存器

func NewRingBuffer

func NewRingBuffer(size int) *RingBuffer

func (*RingBuffer) BackWrite

func (b *RingBuffer) BackWrite(size int)

回退写

func (*RingBuffer) Close

func (b *RingBuffer) Close() error

func (*RingBuffer) GetPos

func (b *RingBuffer) GetPos() int

func (*RingBuffer) Len

func (b *RingBuffer) Len() int

当前数据长度

func (*RingBuffer) Read

func (b *RingBuffer) Read(buf []byte) (int, error)

读数据

func (*RingBuffer) ReadBytes

func (b *RingBuffer) ReadBytes(n int, cb func([]byte)) (int, error)

func (*RingBuffer) TryReadBytes

func (b *RingBuffer) TryReadBytes(n int, cb func([]byte)) (int, error)

func (*RingBuffer) TryWrite

func (b *RingBuffer) TryWrite(p []byte) (int, error)

func (*RingBuffer) Write

func (b *RingBuffer) Write(p []byte) (int, error)

写数据,要么全部写成功,要么不写

type Str2Level

type Str2Level func(string) Level

type TextFormatter

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

func NewTextFormatter

func NewTextFormatter(opts ...FormatterOption) *TextFormatter

func (*TextFormatter) Sprint

func (f *TextFormatter) Sprint(calldepth int, w io.Writer, lv Level, args ...any) (int, error)

func (*TextFormatter) Sprintf

func (f *TextFormatter) Sprintf(calldepth int, w io.Writer, lv Level, format string, args ...any) (int, error)

func (*TextFormatter) Sprintr added in v0.0.3

func (f *TextFormatter) Sprintr(calldepth int, w io.Writer, lv Level, r io.Reader) (int, error)

type WriterErrHandler

type WriterErrHandler func(w LogWriter, e error, log string)

Directories

Path Synopsis
log

Jump to

Keyboard shortcuts

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