liblog

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package liblog provides a date-organised, size-bounded log directory. Files are named <name>-<YYYY-MM-DD>.log, and a day that outgrows its size bound continues in <name>-<YYYY-MM-DD>.2.log, .3.log, and so on. A Writer is safe for concurrent use, and retention is applied on the write path rather than by a background goroutine.

Index

Constants

View Source
const DefaultMaxAge = 14 * 24 * time.Hour

DefaultMaxAge retires a log by date regardless of how few files exist.

View Source
const DefaultMaxBytes int64 = 10 << 20 // 10 MiB

DefaultMaxBytes is a single day-part's default size ceiling.

View Source
const DefaultMaxFiles = 14

DefaultMaxFiles is how many log files survive by default, counted across every date and part.

View Source
const Unlimited = -1

Unlimited disables a retention bound.

Variables

This section is empty.

Functions

func FormatSize

func FormatSize(n int64) string

FormatSize renders a byte count the way ParseSize would accept it back.

func ParseSize

func ParseSize(s string) (int64, error)

ParseSize reads a human-written size — "10MB", "512kb", "1 GiB", "2048" — into bytes. A bare number is bytes, and KB and KiB both mean 1024.

Types

type Config

type Config struct {
	// Dir is the directory log files live in. Created if absent.
	Dir string
	// Name is the base name: "serve" yields serve-2026-08-15.log.
	Name string
	// MaxBytes bounds one part. Non-positive means [DefaultMaxBytes].
	MaxBytes int64
	// MaxFiles bounds how many files are retained across all dates.
	// Non-positive means [DefaultMaxFiles]; use [Unlimited] for no bound.
	MaxFiles int
	// MaxAge retires files older than this by their date stamp. Zero means
	// [DefaultMaxAge]; negative means no age bound.
	MaxAge time.Duration
	// Now is the clock. Nil means [time.Now].
	Now func() time.Time
}

Config describes a log directory. The zero value is not usable.

type Writer

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

Writer is an io.WriteCloser over a date-organised log directory.

func Open

func Open(cfg Config) (*Writer, error)

Open prepares the log directory and opens today's current part for appending. A restart continues the latest existing part rather than starting a new one.

func (*Writer) Close

func (w *Writer) Close() error

Close closes the current file. It is safe to call more than once.

func (*Writer) Dir

func (w *Writer) Dir() string

Dir, MaxBytes, MaxFiles and MaxAge report the settings in force.

func (*Writer) MaxAge

func (w *Writer) MaxAge() time.Duration

func (*Writer) MaxBytes

func (w *Writer) MaxBytes() int64

func (*Writer) MaxFiles

func (w *Writer) MaxFiles() int

func (*Writer) Path

func (w *Writer) Path() string

Path reports the file currently being written.

func (*Writer) Reconfigure

func (w *Writer) Reconfigure(maxBytes int64, maxFiles int, maxAge time.Duration)

Reconfigure updates the retention bounds of a live log. The directory and name are fixed at Open.

func (*Writer) Size

func (w *Writer) Size() int64

Size reports the current part's size.

func (*Writer) Write

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

Write appends p, starting a new file first when the date has changed or when p would carry the current part past its ceiling. A single write larger than the ceiling is still written whole.

Jump to

Keyboard shortcuts

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