rotate

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2021 License: GPL-3.0, GPL-3.0 Imports: 3 Imported by: 0

README

Build Status

Rotate Safe Writer

Makes an io.Writer that is resilient to inode changes (as caused by logrotate, file deletion, etc.)

This can be used anywhere that requires the io.Writer interface to be satisfied (including os.File). See the tests for usage examples

It works by storing the file's inode as it is opened, then checking whether or not the file at the original path still has the same inode value prior to calling File.Write(). If the inode has changed, the file at the original path will be re-opened, the new inode stored, then the write will continue as normal.

Documentation

Overview

Package `rotate` provides an io.Writer interface for files that will detect when the open file has been rotated away (due to log rotation, or manual move/deletion) and re-open it. This allows the standard log.Logger to continue working as expected after log rotation happens (without needing to specify the `copytruncate` or equivalient options).

This package is safe to use concurrently from multiple goroutines

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RotatableFileWriter

type RotatableFileWriter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

RotatableFileWriter implementation that knows when the file has been rotated and re-opens it

func NewRotatableFileWriter

func NewRotatableFileWriter(
	name string,
	retries int,
	create bool,
	mode os.FileMode) (*RotatableFileWriter, error)

NewRotatableFileWriter opens a file for appending and writing that can be safely rotated.

If retries is greater than 0, RotatableFileWriter will make that number of additional attempts to reopen a rotated file after a file open failure, following a 1ms delay. This option can mitigate race conditions that may occur when RotatableFileWriter reopens the file while an underlying file manager, such as logrotate, is recreating the file and setting its properties.

When create is true, RotatableFileWriter will attempt to create the file (using mode), if it does not exist, when reopening the file. Set create to false to avoid conflicts with an underlying file manager, such as logrotate. logrotate, unless configured with nocreate, creates files with O_EXCL and re-rotates/retries if another process has created the file.

func (*RotatableFileWriter) Close

func (f *RotatableFileWriter) Close() error

Close closes the underlying file

func (*RotatableFileWriter) Reopen

func (f *RotatableFileWriter) Reopen() error

Reopen provides the concurrency safe implementation of re-opening the file, and updating the struct's fileInfo

func (*RotatableFileWriter) Write

func (f *RotatableFileWriter) Write(p []byte) (int, error)

Write implements the standard io.Writer interface, but checks whether or not the file has changed prior to writing. If it has, it will reopen the file first, then write

Jump to

Keyboard shortcuts

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