logrotate

package
v0.12.67 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: Apache-2.0, Apache-2.0 Imports: 9 Imported by: 1

Documentation

Overview

Package logrotate integrates lumberjack file rotation with xlog and provides a ChannelWriter for optional background byte writes.

Initialize replaces xlog's global formatter with a PrettyFormatter. For example, within an application function returning error:

closer, err := logrotate.Initialize("./logs", "app", 7, 100, false, os.Stderr)
if err != nil {
	return err
}
logger := xlog.NewPackageLogger("example.com/app", "main")
logger.KV(xlog.INFO, "event", "started")
// Stop application producers before closing the logging destination.
return closer.Close()

The buffered flag queues already formatted bytes; it does not move xlog formatting to a worker. A full queue blocks. Without an extra sink, a file buffer is used even with buffered=false. PackageLogger.FlushError and Close flush it. Close removes the formatter override, drains, flushes, and closes the owned file; concurrent/repeated calls return the same result. Extra sinks are flushed when supported but remain caller-owned. Neither Flush nor Close guarantees filesystem durability, and stalled destinations can block shutdown.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Initialize

func Initialize(logFolder, baseFilename string, maxAge, maxSize int, buffered bool, extraSink io.Writer) (io.Closer, error)

Initialize creates a lumberjack log rotator and redirects log output to it. maxAge is retention in days; maxSize is maximum file size in lumberjack MB. The output is logFolder/baseFilename.log. File opening is lazy; later failures are reported by the returned closer. buffered enables a 256-item byte queue with a one-second flush interval. Without extraSink, an 8 KiB file buffer is used even when buffered is false; explicit logger flushing makes it visible. An extra destination is flushed when supported, but never closed by this package. It is a second io.Writer, unrelated to xlog.Sink. Close removes this formatter override, waits for admitted logging calls, drains, flushes, and closes the owned file. Overrides may close in any order. Concurrent and repeated Close calls wait and return the same result.

Types

type ChannelWriter

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

ChannelWriter copies writes into a bounded queue consumed by one goroutine. A full queue blocks producers. Err, Flush, and Close report destination errors. Stop rejects new writes and waits for accepted writes and the final flush. A ChannelWriter must not be copied after first use.

func NewChannelWriter

func NewChannelWriter(dest io.Writer, bufferDepth int, flushInterval time.Duration) *ChannelWriter

NewChannelWriter starts a worker that writes to dest. bufferDepth is the number of queued byte slices, not a byte or memory limit; zero is unbuffered and a negative depth panics. The worker calls Flush() error on dest at positive flushInterval ticks and on Stop, when supported. Zero disables periodic flush. Queued writes may be lost on a crash. The destination is never closed.

func (*ChannelWriter) Close added in v0.12.67

func (cw *ChannelWriter) Close() error

Close stops the worker and returns the first destination error. It is safe to call concurrently or repeatedly. It does not close the caller-owned destination.

func (*ChannelWriter) Err added in v0.12.67

func (cw *ChannelWriter) Err() error

Err returns the first destination write, short-write, or flush error observed so far. It is safe during logging; enqueue success does not imply Err is nil.

func (*ChannelWriter) Flush added in v0.12.67

func (cw *ChannelWriter) Flush() error

Flush waits for previously enqueued writes and flushes the destination when supported. During or after Stop it waits for shutdown and returns its error. It is a delivery barrier, not an fsync, and can block on a stalled destination.

func (*ChannelWriter) IsStopped

func (cw *ChannelWriter) IsStopped() bool

IsStopped reports whether stopping has begun, not whether draining has finished.

func (*ChannelWriter) Stop

func (cw *ChannelWriter) Stop()

Stop rejects new writes, drains accepted writes, and flushes the destination if supported. Every caller waits for completion, including concurrent callers. Stop can block indefinitely on a stalled destination.

func (*ChannelWriter) Write

func (cw *ChannelWriter) Write(d []byte) (int, error)

Write copies d into the queue and reports enqueue success, not delivery. Writes after shutdown begins return zero and an error wrapping io.ErrClosedPipe. Writes overlapping shutdown may enqueue successfully or return that error; every successful write is drained before Stop returns.

Jump to

Keyboard shortcuts

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