pslog

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 18 Imported by: 0

README

ps-log 日志采集

项目背景
  1. 测试/开发环境, 出现了 error log等不能被开发感知, 当反馈到开发时时间间隔较长, 如何解决?

    解决: 定时(如:10m)去解析 log 中每行包含 error 的内容, 再进行对应的处理(如: 发邮件, 发es等)

  2. 定时去分析 log 吗, 需要实时感知 error log 怎么办呢?

    解决: 通过文件事件通知来感知文件变化呀, 有变化的时候就去查看文件了内容

  3. 实时感知的文件需要起多个监听任务吗? 需要多次打卡相同的文件怎么处理呢?

    解决: 不需要,只需要1个监听者,1个处理者; 可以通过池化文件句柄

介绍
go get -u gitee.com/xuesongtao/ps-log
  1. 支持定时/实时去解析多个 log 文件, 采集完后会根据配置进行采集位置的持久化保存(即: 文件偏移量保存), 便于停机后重启防止出现重复采集现象
  2. 支持 log 行内容 多个匹配规则, 匹配的内容支持不同的处理方式(支持同步/异步处理)
  3. 采用文件池将频繁使用的句柄进行缓存, 采用 tire 树缓存匹配规则提高匹配效率
使用
实时监听
func main() {
 ps, err := pslog.NewPsLog(pslog.WithAsync2Tos(), pslog.WithPreCleanOffset())
 if err != nil {
  panic(err)
 }
 defer ps.Close()

 if err := ps.TailLogs(); err != nil {
  panic(err)
 }

 handler := &pslog.Handler{
  Change:   -1,
  Tail:     true,
  ExpireAt: pslog.NoExpire,
  Targets: []*pslog.Target{
   {
    Content:  " ",
    Excludes: []string{},
    To:       []io.Writer{os.Stdout},
   },
  },
 }

 if err := ps.Register(handler); err != nil {
  panic(err)
 }

 closeCh := make(chan int)
 go func() {
  fh := xfile.NewFileHandle("log/test.log")
  if err := fh.Initf(os.O_RDWR | os.O_TRUNC); err != nil {
   xlog.Error(err)
   return
  }
  f := fh.GetFile()
  for i := 0; i < 30; i++ {
   time.Sleep(time.Second)
   _, err := f.WriteString(time.Now().Format(base.DatetimeFmt+".000") + " " + fmt.Sprint(i) + "\n")
   if err != nil {
    xlog.Error("write err:", err)
   }
  }
  close(closeCh)
 }()

 if err := ps.AddPaths("log/test.log"); err != nil {
  panic(err)
 }

 for range closeCh {}
}
其他
  • 欢迎大佬们指正, 希望大佬给❤️,to gitee, github

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NoExpire = base.Datetime2TimeObj("9999-12-31 23:59:59") // 不过期
)

Functions

func SetLogger

func SetLogger(l PsLogger)

SetLogger 设置 pl

Types

type FileInfo

type FileInfo struct {
	Handler *Handler // 这里优先 PsLog.handler
	Dir     string   // 文件目录
	Name    string   // 文件名
	// contains filtered or unexported fields
}

func (*FileInfo) CleanNameFmt

func (f *FileInfo) CleanNameFmt() string

CleanNameFmt 清理 Name 中的格式, 如: test.log => test

func (*FileInfo) FileName

func (f *FileInfo) FileName() string

FileName 获取文件的全路径名

func (*FileInfo) HandlerIsNil

func (f *FileInfo) HandlerIsNil() bool

HandlerIsNil

func (*FileInfo) IsExpire

func (f *FileInfo) IsExpire(t ...time.Time) bool

IsExpire 文件是否过期

func (*FileInfo) Parse

func (f *FileInfo) Parse(path string)

Parse 解析 path

type Handler

type Handler struct {
	Tail     bool      // 是否实时处理, 说明: true 为实时; false 需要外部定时调用
	Change   int32     // 文件 offset 变化次数, 为持久化文件偏移量数阈值, 当, 说明: -1 为实时保存; 0 达到默认值 defaultHandleChange 时保存; 其他 大于后会保存
	ExpireAt time.Time // 文件句柄过期时间, 如: 2022-12-03 11:11:10

	Targets []*Target // 目标 msg
	// contains filtered or unexported fields
}

Handler 处理的部分

func (*Handler) Valid

func (h *Handler) Valid() error

type Opt

type Opt func(*PsLog)

Opt

func WithAsync2Tos

func WithAsync2Tos() Opt

WithAsync2Tos 异步处理 tos

func WithCleanUpTime

func WithCleanUpTime(dur time.Duration) Opt

WithCleanUpTime 设置清理 logMap 的周期

func WithPreCleanOffset

func WithPreCleanOffset() Opt

WithPreCleanOffset 是否预先清理文件偏移量

func WithTaskPoolSize

func WithTaskPoolSize(size int) Opt

WithTaskPoolSize 设置协程池大小

type PsLog

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

PsLog 解析 log

func NewPsLog

func NewPsLog(opts ...Opt) (*PsLog, error)

NewPsLog 是根据提供的 log path 进行逐行解析 注: 结束时需要调用 Close

func (*PsLog) AddPath2HandlerMap

func (p *PsLog) AddPath2HandlerMap(path2HandlerMap map[string]*Handler) error

AddPath2HandlerMap 添加文件对应的处理方法 只会根据文件对应的 Handler 进行处理

func (*PsLog) AddPaths

func (p *PsLog) AddPaths(paths ...string) error

AddPaths 添加 path, path 必须为文件全路径 根据 PsLog.Handler 进行处理

func (*PsLog) Close

func (p *PsLog) Close()

Close 释放资源

func (*PsLog) CronLogs

func (p *PsLog) CronLogs()

cronLog 定时解析 log

func (*PsLog) Register

func (p *PsLog) Register(handler *Handler) error

Register 注册处理器

func (*PsLog) TailLogs

func (p *PsLog) TailLogs(watchChSize ...int) error

TailLogs 实时解析 log watchSize 为监听到文件变化处理数据的 chan 的长度, 建议为监听文件的个数

type PsLogger

type PsLogger interface {
	Info(v ...interface{})
	Infof(format string, v ...interface{})
	Error(v ...interface{})
	Errorf(format string, v ...interface{})
	Warning(v ...interface{})
	Warningf(format string, v ...interface{})
}

type Target

type Target struct {
	Content  string   // 目标内容
	Excludes []string // 排除 msg

	To []io.Writer // 处理
	// contains filtered or unexported fields
}

Target 目标内容

type Watch

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

Watch 监听的文件

func NewWatch

func NewWatch() (*Watch, error)

NewWatch 监听

func (*Watch) Add

func (w *Watch) Add(paths ...string) error

Add 添加待 watch 的路径 说明:

  1. 自行去重
  2. paths 中可以为目录和文件
  3. 建议使用绝对路径

func (*Watch) Close

func (w *Watch) Close()

Close

func (*Watch) Remove

func (w *Watch) Remove(paths ...string) error

Remove 移除待 watch 的路径

func (*Watch) Watch

func (w *Watch) Watch(busCh chan *WatchFileInfo)

Watch 文件异步监听

func (*Watch) WatchList

func (w *Watch) WatchList() string

WatchList 查询监听的所有 path

type WatchFileInfo

type WatchFileInfo struct {
	IsDir         bool   // 是否为目录
	Path          string // 原始添加的文件路径, 这里可能是文件路径或目录路径
	WatchFilePath string // 监听到的变化的文件全路径
}

WatchFileInfo

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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