workerlog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2026 License: MIT Imports: 6 Imported by: 0

README

workerlog

Go Reference Go Report Card

Simple file-based logging for background workers with automatic rotation.

Features

  • 📅 Date-based file naming: worker-YYYY-MM-DD.log
  • 🔄 Automatic file rotation (oldest files deleted when limit exceeded)
  • 🔒 Thread-safe operations
  • 📖 Simple API for reading log files
  • 📦 Zero external dependencies

Installation

go get github.com/alexeevp/workerlog

Usage

package main

import (
    "log"
    "github.com/alexeevp/workerlog"
)

func main() {
    // Create writer that keeps 7 days of logs
    writer := workerlog.NewWriter("var/logs/workers", 7)
    
    // Write log lines
    err := writer.Write("my-worker", []string{
        "Task started",
        "Processing item 1",
        "Task completed successfully",
    })
    if err != nil {
        log.Fatal(err)
    }
    
    // Get recent log files
    files := writer.GetLogFiles("my-worker", 3)
    for _, f := range files {
        log.Printf("Log file: %s (date: %s)", f.Filename, f.Date)
    }
    
    // Read specific log file
    content, err := writer.ReadLogFile("my-worker", "2026-02-19")
    if err != nil {
        log.Fatal(err)
    }
    log.Println(content)
}

API

Creating a Writer
writer := workerlog.NewWriter(baseDir string, maxFiles int)
  • baseDir - Directory for log storage (e.g., "var/logs/workers")
  • maxFiles - Maximum number of files per worker (oldest are deleted)
Writing Logs
err := writer.Write(workerName string, lines []string) error

Writes log lines to a file named {workerName}-{date}.log. Creates the file and directory if they don't exist.

Reading Log Files
// Get list of log files (newest first)
files := writer.GetLogFiles(workerName string, limit int) []FileInfo

// Read specific log file by date
content, err := writer.ReadLogFile(workerName string, date string) (string, error)
FileInfo
type FileInfo struct {
    Date     string // "2026-02-05"
    Filename string // "worker-2026-02-05.log"
}

File Format

Log files are named using the pattern {workerName}-{YYYY-MM-DD}.log:

  • email-worker-2026-02-19.log
  • cleanup-worker-2026-02-19.log
  • report-worker-2026-02-18.log

Rotation

When the number of log files for a worker exceeds maxFiles, the oldest files are automatically deleted. Files are sorted by date in their filename.

License

MIT License - see LICENSE file

Documentation

Overview

Package workerlog provides file-based logging for background workers with automatic rotation.

The Writer writes log entries to date-stamped files and automatically removes old files when the maximum number of files is exceeded.

Basic usage:

writer := workerlog.NewWriter("var/logs/workers", 7)
err := writer.Write("my-worker", []string{"Task started", "Task completed"})

Features:

  • Date-based file naming: worker-YYYY-MM-DD.log
  • Automatic file rotation (oldest files deleted when limit exceeded)
  • Thread-safe operations
  • Simple API for reading log files

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileInfo

type FileInfo struct {
	Date     string // "2026-02-05"
	Filename string // "greeting-2026-02-05.log"
}

FileInfo contains information about a worker log file

type Writer

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

Writer writes worker logs to files with automatic rotation.

func NewWriter

func NewWriter(baseDir string, maxFiles int) *Writer

NewWriter creates a new Writer.

  • baseDir: directory for log storage (e.g., "var/logs/workers")
  • maxFiles: maximum number of files per worker (oldest are deleted)

func (*Writer) GetLogFiles

func (w *Writer) GetLogFiles(workerName string, limit int) []FileInfo

GetLogFiles returns a list of log files for a worker (most recent files first). Returns files sorted by date (newest first), limited to the specified count.

func (*Writer) ReadLogFile

func (w *Writer) ReadLogFile(workerName, date string) (string, error)

ReadLogFile reads the content of a worker's log file by date

func (*Writer) Write

func (w *Writer) Write(workerName string, lines []string) (err error)

Write writes log lines to a worker's log file. File format: {workerName}-{date}.log (e.g., greeting-2026-02-10.log)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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