rotatewriter

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

rotatewriter

rotatewriter is a Go package that provides an io.Writer with simple file rotation support.

Features

  • Drop-in io.Writer implementation
  • Automatic log rotation when the file reaches a maximum size
  • Configurable maximum number of backup files
  • Optional automatic directory creation
  • Safe handling of symlinks

Installation

go get github.com/monitoring-forge/rotatewriter

Usage

package main

import (
    "log"

    "github.com/monitoring-forge/rotatewriter"
)

func main() {
    w, err := rotatewriter.New(
        rotatewriter.Filename("/var/log/myapp/app.log"),
        rotatewriter.MaxSize(100),      // Rotate when the file reaches 100 MiB
        rotatewriter.MaxBackups(7),     // Keep up to 7 backup files
        rotatewriter.AutoDirCreate(true),
    )
    if err != nil {
        log.Fatal(err)
    }
    defer w.Close()

    logger := log.New(w, "", log.LstdFlags)
    logger.Println("application started")
}

Options

Option Default Description
Filename required Path to the log file.
MaxSize 100 Maximum size of the active log file in MiB.
MaxBackups 7 Maximum number of rotated backup files to retain.
AutoDirCreate false Automatically create the log directory if it does not exist.

Rotation behavior

  • When a write would cause the active log file to exceed MaxSize, the current file is closed and renamed to {basename}-{unixnano}{ext}.
  • Old backup files are removed to keep at most MaxBackups files.
  • A new active log file is created at the original Filename.

License

See LICENSE.

Documentation

Index

Constants

View Source
const (
	DefaultMaxSize       = 100
	DefaultMaxBackups    = 7
	DefaultAutoDirCreate = false
)

Variables

This section is empty.

Functions

This section is empty.

Types

type RotateWriter

type RotateWriter struct {
	Filename      string
	MaxSize       int
	MaxBackups    int
	AutoDirCreate bool
	// contains filtered or unexported fields
}

func New

func New(ops ...WriterOption) (*RotateWriter, error)

func (*RotateWriter) Close

func (w *RotateWriter) Close() error

func (*RotateWriter) Write

func (w *RotateWriter) Write(p []byte) (int, error)

type WriterOption

type WriterOption func(*RotateWriter)

func AutoDirCreate

func AutoDirCreate(auto bool) WriterOption

func Filename

func Filename(name string) WriterOption

func MaxBackups

func MaxBackups(num int) WriterOption

func MaxSize

func MaxSize(size int) WriterOption

Jump to

Keyboard shortcuts

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