file

package
v2.6.6 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	FormatText = internal.FormatText // Text format
	FormatJson = internal.FormatJson // JSON format
)
View Source
const Name = "file"

Name is the syncer name.

Variables

This section is empty.

Functions

This section is empty.

Types

type Format

type Format = internal.Format

Format is the log output format.

type Option

type Option func(o *options)

Option configures the syncer.

func WithBufferSize added in v2.6.0

func WithBufferSize(bufferSize int) Option

WithBufferSize sets the buffer size.

func WithCompress

func WithCompress(compress bool) Option

WithCompress sets whether to compress rotated log files.

func WithFlushInterval added in v2.6.0

func WithFlushInterval(flushInterval time.Duration) Option

WithFlushInterval sets the flush policy: <=0 flushes every log record immediately, >0 batches writes and flushes periodically.

func WithFormat

func WithFormat(format Format) Option

WithFormat sets the output format.

func WithMaxAge

func WithMaxAge(maxAge time.Duration) Option

WithMaxAge sets the maximum retention time of a file.

func WithMaxSize

func WithMaxSize(maxSize int64) Option

WithMaxSize sets the maximum size of a single file.

func WithPath

func WithPath(path string) Option

WithPath sets the file path.

func WithRotate

func WithRotate(rotate Rotate) Option

WithRotate sets the file rotation rule.

type Rotate

type Rotate string

Rotate is a log rotation rule.

const (
	RotateNone  Rotate = "none"  // No rotation
	RotateYear  Rotate = "year"  // Rotate by year
	RotateMonth Rotate = "month" // Rotate by month
	RotateWeek  Rotate = "week"  // Rotate by week
	RotateDay   Rotate = "day"   // Rotate by day
	RotateHour  Rotate = "hour"  // Rotate by hour
)

type Syncer

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

func NewSyncer

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

ExampleNewSyncer uses the high-performance mode: batched writes with periodic flushing (the default semantics). It offers the highest throughput; a process crash loses at most the log records written within the last 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: batched writes flushed every second
		file.WithBufferSize(32<<10),         // 32KB buffer
		file.WithMaxSize(500<<20),           // 500MB per file
		file.WithMaxAge(7*24*time.Hour),     // retain for 7 days
		file.WithRotate(file.RotateDay),     // rotate daily
		file.WithCompress(true),             // compress rotated files
	)
	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 uses the high-reliability mode: every log record is flushed immediately. A process crash loses at most the record being written, at the cost of lower throughput.

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: flush every log record immediately
	)
	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 closes the syncer, flushing and closing the underlying file.

func (*Syncer) Name

func (s *Syncer) Name() string

Name returns the syncer name.

func (*Syncer) Write

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

Write writes the given entity. It reports errors.ErrSyncerClosed when the syncer has been closed.

Jump to

Keyboard shortcuts

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