rfw

package module
v0.0.0-...-6f0a6f3 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2017 License: MIT Imports: 3 Imported by: 60

README

Rotating File writer

wercker status GoDoc

An io.writer & io.Closer compliant file writer that will always write to the path that you give it, even if somebody deletes/renames that path out from under you.

Created so that Go programs can be used with the standard Linux logrotate: writes following a removal / rename will occur in a newly created file rather than the previously opened filehandle.

As the current implementation relies on remembering and checking the current inode of the desired file, this code will not work or compile on Windows.

Example
package main

import (
  "github.com/mipearson/rfw"
  "log"
)

func main() {
  writer, err := rfw.Open("/var/log/myprogram", 0644)
  if err != nil {
    log.Fatalln("Could not open '/var/log/myprogram': ", err)
  }

  log := log.New(writer, "[myprogram] ", log.LstdFlags)
  log.Println("Logging as normal")
}
TODO
  • Use exp/fsnotify so that we don't need to call stat on every write
  • Use exp/winfsnotify to allow for Windows support
  • Trap SIGHUP and close/reopen all files
  • Force a check every minute so that programs that rarely write can release filehandles

Documentation

Overview

Package rfw implements a log rotation aware file writer. It will always write to the path that you give it, even if that file is deleted or moved out from under it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

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

A file rotation aware writer

func Open

func Open(path string, mode os.FileMode) (*Writer, error)

Open returns a new Writer at the specified path.

If the file does not exist it will be created with the specified mode. If the file does exist it will be appended to.

func (*Writer) Close

func (l *Writer) Close() error

Close our writer. Subsequent writes will fail.

func (*Writer) Write

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

Write p bytes to our file.

If our file has been deleted or has been moved out from under us, a new file will be created with the mode specified at Open time.

Jump to

Keyboard shortcuts

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