rotator

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2022 License: MIT Imports: 9 Imported by: 0

README

Go Log Rotator

The Log Rotator provides a simple io.WriteCloser to be used as output for your logger of choice.

Usage examples (refer to examples folder)

Standard package logger:

package main

import (
	"fmt"
	"log"
	"sync"

	rotator "github.com/KaiserWerk/go-log-rotator"
)

func main() {
	// this creates a new Rotator with a maximum file size of 10KB and 3 rotated files are to be kept on disk
	// the default logger does NOT take care of thread-safe writes, so supply 'true' as last parameter
	rotator, err := rotator.New(".", "standard-logger.log", 10<<10, 0644, 3, true)
	if err != nil {
		log.Fatalf("could not create rotator: %s", err.Error())
	}
	defer rotator.Close()
	logger := log.New(rotator, "", 0)

	var wg sync.WaitGroup
	wg.Add(3)
	go func(w *sync.WaitGroup) {
		for i := 0; i < 3000; i++ {
			logger.Println("Hello World!")
		}
		w.Done()
	}(&wg)
	go func(w *sync.WaitGroup) {
		for i := 0; i < 3000; i++ {
			logger.Println("Goodbye...")
		}
		w.Done()
	}(&wg)
	go func(w *sync.WaitGroup) {
		for i := 0; i < 3000; i++ {
			logger.Println("How's it going?")
		}
		w.Done()
	}(&wg)

	wg.Wait()

	// there should be 4 rotator files by now
}

Logrus:

package main

import (
	"log"
	"sync"

	rotator "github.com/KaiserWerk/go-log-rotator"

	"github.com/sirupsen/logrus"
)

func main() {
	// this creates a new Rotator with a maximum file size of 2KB and 15 rotated files are to be kept on disk
	// logrus DOES take care of thread-safe writes, so supply 'false' as last parameter to avoid unnecessary overhead
	rotator, err := rotator.New(".", "logrus-logger.log", 2<<10, 0644, 15, false)
	if err != nil {
		log.Fatal("could not create rotator:", err.Error())
	}

	logger := logrus.New()
	logger.SetOutput(rotator) // use the rotator here
	logger.SetLevel(logrus.DebugLevel)
	logger.SetFormatter(&logrus.TextFormatter{})

	var wg sync.WaitGroup
	wg.Add(3)
	go func(w *sync.WaitGroup) {
		for i := 0; i < 100; i++ {
			logger.Info("Hello World!")
		}
		w.Done()
	}(&wg)
	go func(w *sync.WaitGroup) {
		for i := 0; i < 100; i++ {
			logger.Warn("Goodbye...")
		}
		w.Done()
	}(&wg)
	go func(w *sync.WaitGroup) {
		for i := 0; i < 100; i++ {
			logger.Error("How's it going?")
		}
		w.Done()
	}(&wg)
	wg.Wait()

	// done? then close up
	_ = rotator.Close()

	// you should see 16 files by now
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rotator

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

Rotator represents a struct responsible for writing into a log file while rotating the file when it reached maxSize.

func New

func New(path, filename string, maxSize uint64, perms fs.FileMode, filesToKeep uint8, useMutex bool) (*Rotator, error)

New returns a new rotator prepared to be written to. The rotator is NOT thread-safe by default, since most logging libraries already take care of that.

path: the path where log files should be written to, e.g. "/var/logs/myapp" or `C:\Logs`

filename: the name the log files are supposed to have, e.g. 'test.log'

maxSize: the maximum size in bytes a created log file may reach before it is rotated, e.g. 10 << 20 for 10MB

perms: the file permissions in octal notation, e.g. 0744 (not relevant for windows)

filesToKeep: the number of rotated files to keep. The currently written file is not counted towards this limit

func (*Rotator) Close

func (r *Rotator) Close() error

Close closes the io.Writer of the Rotator.

func (*Rotator) Write

func (r *Rotator) Write(data []byte) (int, error)

Write writes the data into the log file and initiates rotation, if necessary

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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