file

package
v2.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	FormatText = internal.FormatText // 文本格式
	FormatJson = internal.FormatJson // JSON格式
)
View Source
const Name = "file"

Variables

This section is empty.

Functions

This section is empty.

Types

type Format

type Format = internal.Format

Format 日志输出格式

type Option

type Option func(o *options)

func WithBufferSize added in v2.6.0

func WithBufferSize(bufferSize int) Option

WithBufferSize 设置缓冲区大小

func WithCompress

func WithCompress(compress bool) Option

WithCompress 设置是否对轮换日志文件进行压缩

func WithFlushInterval added in v2.6.0

func WithFlushInterval(flushInterval time.Duration) Option

WithFlushInterval 设置刷盘策略:<=0 表示每条日志立即刷盘,>0 表示批量刷写并定时刷盘

func WithFormat

func WithFormat(format Format) Option

WithFormat 设置输出格式

func WithMaxAge

func WithMaxAge(maxAge time.Duration) Option

WithMaxAge 设置文件最大留存时间

func WithMaxSize

func WithMaxSize(maxSize int64) Option

WithMaxSize 设置单个文件最大尺寸

func WithPath

func WithPath(path string) Option

WithPath 设置文件路径

func WithRotate

func WithRotate(rotate Rotate) Option

WithRotate 设置文件反转规则

type Rotate

type Rotate string

日志翻转规则

const (
	RotateNone  Rotate = "none"  // 不翻转
	RotateYear  Rotate = "year"  // 按年翻转
	RotateMonth Rotate = "month" // 按月翻转
	RotateWeek  Rotate = "week"  // 按周翻转
	RotateDay   Rotate = "day"   // 按天翻转
	RotateHour  Rotate = "hour"  // 按时翻转
)

type Syncer

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

func NewSyncer

func NewSyncer(opts ...Option) *Syncer
Example

ExampleNewSyncer 高性能模式:批量刷写 + 定时刷盘(默认语义)。 吞吐最高,进程崩溃最多丢失最近一个 flushInterval 内的日志。

package main

import (
	"os"
	"path/filepath"
	"time"

	"github.com/dobyte/due/v2/log"
	"github.com/dobyte/due/v2/log/file"
)

func main() {
	dir, _ := os.MkdirTemp("", "due-log-example")
	defer os.RemoveAll(dir)

	syncer := file.NewSyncer(
		file.WithPath(filepath.Join(dir, "due.log")),
		file.WithFormat(file.FormatJson),
		file.WithFlushInterval(time.Second), // >0:批量写 + 每 1s 定时刷盘
		file.WithBufferSize(32<<10),         // 32KB 缓冲
		file.WithMaxSize(500<<20),           // 单个文件 500MB
		file.WithMaxAge(7*24*time.Hour),     // 保留 7 天
		file.WithRotate(file.RotateDay),     // 按天轮转
		file.WithCompress(true),             // 压缩轮转文件
	)
	defer syncer.Close()

	entity := &log.Entity{
		Now:     time.Now(),
		Time:    time.Now().Format("2006-01-02 15:04:05"),
		Level:   log.LevelInfo,
		Message: "hello due-framework",
	}

	_ = syncer.Write(entity)
}
Example (ImmediateFlush)

ExampleNewSyncer_immediateFlush 高可靠模式:每条日志立即刷盘。 进程崩溃最多丢失正在写入的那一条,代价是吞吐下降。

package main

import (
	"os"
	"path/filepath"
	"time"

	"github.com/dobyte/due/v2/log"
	"github.com/dobyte/due/v2/log/file"
)

func main() {
	dir, _ := os.MkdirTemp("", "due-log-example")
	defer os.RemoveAll(dir)

	syncer := file.NewSyncer(
		file.WithPath(filepath.Join(dir, "due.log")),
		file.WithFlushInterval(0), // <=0:每条日志立即刷盘
	)
	defer syncer.Close()

	entity := &log.Entity{
		Now:     time.Now(),
		Time:    time.Now().Format("2006-01-02 15:04:05"),
		Level:   log.LevelError,
		Message: "something went wrong",
	}

	_ = syncer.Write(entity)
}

func (*Syncer) Close

func (s *Syncer) Close() error

Close 关闭同步器

func (*Syncer) Name

func (s *Syncer) Name() string

Name 同步器名称

func (*Syncer) Write

func (s *Syncer) Write(entity *internal.Entity) error

Write 写入日志

Jump to

Keyboard shortcuts

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