reopen

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2024 License: MIT Imports: 6 Imported by: 0

README

Build Status Go Report Card GoDoc license

Makes a standard os.File a "reopenable writer" and allows SIGHUP signals to reopen log files, as needed by logrotated. This is inspired by the C/Posix freopen

The simple version reopen.NewFileWriter does unbuffered writing. A call to .Reopen closes the existing file handle, and then re-opens it using the original filename.

The more advanced version reopen.NewBufferedFileWriter buffers input and flushes when the internal buffer is full (with care) or if 30 seconds has elapsed.

There is also reopen.Stderr and reopen.Stdout which implements the reopen.Reopener interface (and does nothing on a reopen call).

reopen.Discard wraps ioutil.Discard

Samples are in example1 and example2. The run.sh scripts are a dumb test where the file is rotated underneath the server, and nothing is lost. This is not the most robust test but gives you an idea of how it works.

Here's some sample code.

package main

/* Simple logrotate logger
 */
import (
       "fmt"
       "log"
       "net/http"
       "os"
       "os/signal"
       "syscall"

       "github.com/client9/reopen"
)

func main() {
        // setup logger to write to our new *reopenable* log file

        f, err := reopen.NewFileWriter("/tmp/example.log")
        if err != nil {
           log.Fatalf("Unable to set output log: %s", err)
        }
        log.SetOutput(f)

        // Handle SIGHUP
        //
        // channel is number of signals needed to catch  (more or less)
        // we only are working with one here, SIGHUP
        sighup := make(chan os.Signal, 1)
        signal.Notify(sighup, syscall.SIGHUP)
        go func() {
           for {
               <-sighup
              fmt.Println("Got a sighup")
              f.Reopen()
            }
         }()

        // dumb http server that just prints and logs the path
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
            log.Printf("%s", r.URL.Path)
            fmt.Fprintf(w, "%s\n", r.URL.Path)
        })
        log.Fatal(http.ListenAndServe("127.0.0.1:8123", nil))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Stdout  = NopWriter(os.Stdout)
	Stderr  = NopWriter(os.Stderr)
	Discard = NopWriter(ioutil.Discard)
)

Reopenable versions of os.Stdout, os.Stderr, /dev/null (reopen does nothing)

Functions

This section is empty.

Types

type BufferedFileWriter

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

BufferedFileWriter is buffer writer than can be reopned

func NewBufferedFileWriter

func NewBufferedFileWriter(w *FileWriter) *BufferedFileWriter

NewBufferedFileWriter opens a buffered file that is periodically

flushed.

func NewBufferedFileWriterSize

func NewBufferedFileWriterSize(w *FileWriter, size int, flush time.Duration) *BufferedFileWriter

NewBufferedFileWriterSize opens a buffered file with the given size that is periodically

flushed on the given interval.

func (*BufferedFileWriter) Close

func (bw *BufferedFileWriter) Close() error

Close flushes the internal buffer and closes the destination file

func (*BufferedFileWriter) Flush

func (bw *BufferedFileWriter) Flush()

Flush flushes the buffer.

func (*BufferedFileWriter) Reopen

func (bw *BufferedFileWriter) Reopen() error

Reopen implement Reopener

func (*BufferedFileWriter) Write

func (bw *BufferedFileWriter) Write(p []byte) (int, error)

Write implements io.Writer (and reopen.Writer)

type FileWriter

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

FileWriter that can also be reopened

func NewFileWriter

func NewFileWriter(name string) (*FileWriter, error)

NewFileWriter opens a file for appending and writing and can be reopened. it is a ReopenWriteCloser...

func NewFileWriterMode

func NewFileWriterMode(name string, mode os.FileMode) (*FileWriter, error)

NewFileWriterMode opens a Reopener file with a specific permission

func (*FileWriter) Close

func (f *FileWriter) Close() error

Close calls the underlyding File.Close()

func (*FileWriter) Reopen

func (f *FileWriter) Reopen() error

Reopen the file

func (*FileWriter) Write

func (f *FileWriter) Write(p []byte) (int, error)

Write implements the stander io.Writer interface

type Reopener

type Reopener interface {
	Reopen() error
}

Reopener interface defines something that can be reopened

type WriteCloser

type WriteCloser interface {
	Reopener
	io.WriteCloser
}

WriteCloser is a io.WriteCloser that can also be reopened

func NopWriter

func NopWriter(w io.Writer) WriteCloser

NopWriter turns a normal writer into a ReopenWriter

by doing a NOP on Reopen.   See https://en.wikipedia.org/wiki/NOP

type Writer

type Writer interface {
	Reopener
	io.Writer
}

Writer is a writer that also can be reopened

func MultiWriter

func MultiWriter(writers ...Writer) Writer

MultiWriter creates a writer that duplicates its writes to all the provided writers, similar to the Unix tee(1) command.

Also allow reopen

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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