xfile

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2024 License: MIT Imports: 12 Imported by: 1

README

xfile

import "github.com/fufuok/utils/xfile"

Index

Constants

const (
    DefaultFlushSizeLimit = 1 << 20
    DefaultFlushInterval  = 1 * time.Second
    MinFlushSizeLimit     = 4096
    MinFlushInterval      = 100 * time.Millisecond
)

Variables

var (
    ErrFilename = errors.New("wrong file name")
)

func IsDir

func IsDir(s string) bool

IsDir 目录是否存在

func IsExist

func IsExist(s string) bool

IsExist 文件或目录是否存在

func IsFile

func IsFile(s string) bool

IsFile 文件是否存在

type DefaultFilename

type DefaultFilename struct{}
func (*DefaultFilename) MakeFilename
func (d *DefaultFilename) MakeFilename(name string) string

type FilenameMaker

type FilenameMaker interface {
    MakeFilename(name string) string
}

type Logger

type Logger interface {
    Errorf(format string, v ...interface{})
}

type Options

type Options struct {
    // 文件名生成器, 每秒检查
    FilenameMaker FilenameMaker

    // 日志处理器
    Logger Logger

    // 每次(实例启动时除外)滚动的文件是否删除后重建
    Rebuild bool

    // 刷新到磁盘的大小和时间间隔, 默认 1MiB, 1秒
    // 注意: 由于写文件有缓冲, 如果按秒切割文件, 数据将有可能写入上一秒的文件名中
    FlushSizeLimit int
    FlushInterval  time.Duration
}

type Roller

type Roller struct {
    // contains filtered or unexported fields
}
func NewRoller
func NewRoller(filename string, opt *Options) (*Roller, error)
func (*Roller) Close
func (r *Roller) Close()
func (*Roller) Write
func (r *Roller) Write(p []byte) (int, error)
func (*Roller) WriteString
func (r *Roller) WriteString(s string) (int, error)

type TimeBasedFilename

type TimeBasedFilename struct {
    // 文件目录
    FilePath string

    // 文件名模板: run-%s.log
    FilenameTpl string

    // 日期时间模板: 060102
    TimeTpl string

    // 时间标识相同则不生成新文件名: 221130
    TimeTag string
}
func (*TimeBasedFilename) MakeFilename
func (t *TimeBasedFilename) MakeFilename(name string) string

Generated by gomarkdoc

Documentation

Index

Constants

View Source
const (
	DefaultFlushSizeLimit = 1 << 20
	DefaultFlushInterval  = 1 * time.Second
	MinFlushSizeLimit     = 4096
	MinFlushInterval      = 100 * time.Millisecond
)

Variables

View Source
var (
	ErrFilename = errors.New("wrong file name")
)

Functions

func IsDir

func IsDir(s string) bool

IsDir 目录是否存在

func IsExist

func IsExist(s string) bool

IsExist 文件或目录是否存在

func IsFile

func IsFile(s string) bool

IsFile 文件是否存在

func ModTime added in v1.0.0

func ModTime(filename string) time.Time

ModTime 文件最后修改时间

func ReadFile added in v0.10.3

func ReadFile(filename string) (string, error)

ReadFile reads contents from a file

func ReadLines added in v0.10.3

func ReadLines(filename string) ([]string, error)

ReadLines reads contents from a file and splits them by new lines. A convenience wrapper to ReadLinesOffsetN(filename, 0, -1).

func ReadLinesOffsetN added in v0.10.3

func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error)

ReadLinesOffsetN reads contents from file and splits them by new line. The offset tells at which line number to start. The count determines the number of lines to read (starting from offset): n >= 0: at most n lines n < 0: whole file Ref: gopsutil

Types

type DefaultFilename

type DefaultFilename struct{}

func (*DefaultFilename) MakeFilename

func (d *DefaultFilename) MakeFilename(name string) string

type FilenameMaker

type FilenameMaker interface {
	MakeFilename(name string) string
}

type Logger

type Logger interface {
	Errorf(format string, v ...interface{})
}

type Options

type Options struct {
	// 文件名生成器, 每秒检查
	FilenameMaker FilenameMaker

	// 日志处理器
	Logger Logger

	// 每次(实例启动时除外)滚动的文件是否删除后重建
	Rebuild bool

	// 刷新到磁盘的大小和时间间隔, 默认 1MiB, 1秒
	// 注意: 由于写文件有缓冲, 如果按秒切割文件, 数据将有可能写入上一秒的文件名中
	FlushSizeLimit int
	FlushInterval  time.Duration
}

type Roller

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

func NewRoller

func NewRoller(filename string, opt *Options) (*Roller, error)

func (*Roller) Close

func (r *Roller) Close()

func (*Roller) Write

func (r *Roller) Write(p []byte) (int, error)

func (*Roller) WriteString

func (r *Roller) WriteString(s string) (int, error)

type TimeBasedFilename

type TimeBasedFilename struct {
	// 文件目录
	FilePath string

	// 文件名模板: run-%s.log
	FilenameTpl string

	// 日期时间模板: 060102
	TimeTpl string

	// 时间标识相同则不生成新文件名: 221130
	TimeTag string
}

func (*TimeBasedFilename) MakeFilename

func (t *TimeBasedFilename) MakeFilename(name string) string

Directories

Path Synopsis
examples
roller command
time_based command
weekly_log command

Jump to

Keyboard shortcuts

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