Documentation
¶
Overview ¶
Package logrotate provides append-only log writers with file rotation.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = fmt.Errorf("logrotate: async writer is closed: %w", fs.ErrClosed)
ErrClosed reports that the async writer no longer accepts writes; errors.Is can also match fs.ErrClosed.
var ErrDropped = errors.New("logrotate: async queue is full; record dropped")
ErrDropped reports that the async queue is full under FullDropNewest and the current record was dropped.
Functions ¶
This section is empty.
Types ¶
type AsyncConfig ¶
type AsyncConfig struct {
// QueueSize is the buffer depth in records (one per Write call), not bytes.
// Values <= 0 use defaultQueueSize.
QueueSize int
// FullPolicy selects the policy applied when the queue is full; it defaults
// to FullBlock.
FullPolicy FullPolicy
}
AsyncConfig configures the async writer.
type AsyncWriter ¶
type AsyncWriter struct {
// contains filtered or unexported fields
}
AsyncWriter serializes writes to the underlying sink on a single flusher goroutine; caller Write calls only enqueue. Close rejects new writes, wakes callers waiting to enqueue, drains the queue, and closes the sink without calling Sync implicitly. To shut down, stop producers first and then call Close so delivery completes before exit. A successful Write only means the record was enqueued; call and check Sync explicitly when persistence must be confirmed.
func OpenAsync ¶
func OpenAsync(sink FileSink, cfg AsyncConfig) (*AsyncWriter, error)
OpenAsync starts background flushing. sink must not be nil; on success the AsyncWriter owns and closes it. Write copies p, so the caller may reuse the buffer.
func (*AsyncWriter) Close ¶
func (aw *AsyncWriter) Close() error
Close stops accepting writes, wakes callers waiting to enqueue, drains the queue, and closes the sink. It does not call Sync implicitly and sets no timeout. It returns the first background write error joined with the sink close error, if any. Repeated calls return the same result.
func (*AsyncWriter) Dropped ¶
func (aw *AsyncWriter) Dropped() uint64
Dropped returns the number of records dropped by FullDropNewest.
func (*AsyncWriter) LastError ¶
func (aw *AsyncWriter) LastError() error
LastError reports the current background flush state: the most recent failure, or nil once a later operation succeeds. A nil return from Write only means the record was enqueued, not that it reached disk; poll this method for health checks.
func (*AsyncWriter) Sync ¶
func (aw *AsyncWriter) Sync() error
Sync places a FIFO barrier in the queue: it syncs the sink after all previously enqueued records are written. It returns the first background write error joined with the sync error, if any. It returns ErrClosed after Close. It is safe for concurrent use.
type Config ¶
type Config struct {
Filename string
Daily bool
// MaxFileMiB is the size-based rollover threshold in MiB (1 << 20 bytes).
MaxFileMiB int64
HistoryDays int
// TotalArchiveMiB limits the combined size of finalized archives in MiB.
TotalArchiveMiB int64
Compress bool
}
Config controls the daily/size format in Filename + ".archives". Zero limits are disabled. Calendar dates use the process local time zone.
type FileSink ¶
FileSink is the underlying destination owned exclusively by AsyncWriter; the async layer calls its methods serially. Write follows the io.Writer contract; a short write without an error is reported as io.ErrShortWrite. Close is called after all accepted data is delivered and never implies Sync.
type FullPolicy ¶
type FullPolicy int
FullPolicy selects the behavior when the async queue is full.
const ( // FullBlock blocks until the queue has room (the default). It never drops // data, but can inflate caller latency. FullBlock FullPolicy = iota // FullDropNewest drops the current record immediately; Write returns // (0, ErrDropped). FullDropNewest )
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer writes to an active file and rotates it according to Config. Use Open to construct a Writer; the zero value is not ready for use. Only MaintenanceError is safe to call concurrently with other methods.
func Open ¶
Open opens the active path for append. The caller must exclusively own the active file and its archive directory. The active file must be regular, not a symlink, and Filename must not contain parent (..) components. New active files start private; replacements inherit permission bits subject to umask. Startup archive maintenance runs in the background. Its errors are available via MaintenanceError and do not prevent writes.
func (*Writer) Close ¶
Close closes the active file without syncing it, then waits for scheduled archive maintenance and reports unresolved maintenance errors. Subsequent calls return nil.
func (*Writer) MaintenanceError ¶
MaintenanceError returns the latest archive maintenance result. It can be called concurrently with Write, Sync, or Close.