Help us shape the future of Go! Take the 2026 Go Developer Survey

logrotate

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

README

Golang logrotate files wrapper

GoDoc

logrotated can be configured to send a SIGHUP signal to a process after rotating it's logs. This library provides os.File-wrapper, that will reopen files when a SIGHUP is received by the app.

Examples
Wrapping files
err := config.Load()
if err != nil {
    return err
}

if logFile, err = logrotate.NewFile(config.LogPath); err != nil {
    return err
}
defer logFile.Close()

if logSlowFile, err = logrotate.NewFile(config.LogSlowPath); err != nil {
    return err
}
defer logSlowFile.Close()

logrotate.StartWatcher(sigHandler, errHandler)
defer logrotate.StopWatcher()
errHandler

errHandler will be called while opening file error is received. You can use it for errors logging, program terminating, or you can omit it in logrotate.StartWatcher (use nil value).

func errHandler(err error) {
	log.Fatal("fatal error while opening log: " + err.Error())
}
sigHandler

sigHandler will be called while SIGHUP signal is received. You can use it for some things, for example - reloading config. If you don't need it - you can omit it in logrotate.StartWatcher (use nil value).

func sigHandler() bool {
	if err := config.Load(); err != nil {
		log.Println("error while reloading config: " + err.Error())
		return true
	}
	logFile.SetPath(config.LogPath)
	logSlowFile.SetPath(config.LogSlowPath)
	return true
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartWatcher

func StartWatcher(sigHandler func() bool, errHandler func(error))

StartWatcher starts goroutine listening for 'SIGHUP' signals. When it gets SIGHUP, it will call sigHandler, if sigHandler returns true - files will be reopened, else - goroutine will waiting for next signal. Func errHandler will be called for each error when opening files. sigHandler and errHandler can be nil.

func StopWatcher

func StopWatcher()

StopWatcher stops SIGHUP-watcher goroutine.

Types

type File

type File struct {
	*os.File
	// contains filtered or unexported fields
}

File wraps an *os.File and listens for a 'SIGHUP' signal from logrotated so it can reopen the new file.

func NewFile

func NewFile(path string, flag int, perm os.FileMode) (*File, error)

NewFile creates a File pointer and registers it for SIGHUP-watcher goroutine.

func (*File) Close

func (f *File) Close() error

Close unregisters file for SIGHUP-watcher and then close the underlying os.File.

func (*File) SetPath

func (f *File) SetPath(path string)

SetPath sets a new path for file (file will be reopened when SIGHUP-watcher goroutine gets signal).

func (*File) Write

func (f *File) Write(b []byte) (int, error)

Write will write to the underlying file. It uses a sync.Mutex to ensure uninterrupted writes during logrotates.

Jump to

Keyboard shortcuts

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