Documentation
¶
Index ¶
- Constants
- type Format
- type Option
- func WithBufferSize(bufferSize int) Option
- func WithCompress(compress bool) Option
- func WithFlushInterval(flushInterval time.Duration) Option
- func WithFormat(format Format) Option
- func WithMaxAge(maxAge time.Duration) Option
- func WithMaxSize(maxSize int64) Option
- func WithPath(path string) Option
- func WithRotate(rotate Rotate) Option
- type Rotate
- type Syncer
Examples ¶
Constants ¶
const ( FormatText = internal.FormatText // Text format FormatJson = internal.FormatJson // JSON format )
const Name = "file"
Name is the syncer name.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option func(o *options)
Option configures the syncer.
func WithBufferSize ¶ added in v2.6.0
WithBufferSize sets the buffer size.
func WithCompress ¶
WithCompress sets whether to compress rotated log files.
func WithFlushInterval ¶ added in v2.6.0
WithFlushInterval sets the flush policy: <=0 flushes every log record immediately, >0 batches writes and flushes periodically.
func WithMaxAge ¶
WithMaxAge sets the maximum retention time of a file.
func WithMaxSize ¶
WithMaxSize sets the maximum size of a single file.
type Syncer ¶
type Syncer struct {
// contains filtered or unexported fields
}
func NewSyncer ¶
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)
}
Output:
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)
}
Output: