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

rollout

package module
v0.0.0-...-274199f Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2017 License: MIT Imports: 11 Imported by: 0

README

Rollout

Rollout is an io.WriteCloser. It is mainly used for writing logs to rolling files.

Features

  1. Rotate by time interval.
  2. Customized rotation interval.
  3. Customized output destination (file name if you are using built-in FileBuffer).
  4. Write to buffer first to reduce IO.
  5. Thread safe.

Install

go get github.com/jerray/rollout

Usage

func main() {
	w := rollout.New(rollout.Options{
		Rotation: rollout.RotateDaily,
		Template: "rollout-{{.Time}}.log",
	})

	log.SetOutput(w)

	for i := 0; i < 5; i++ {
		go func(i int) {
			for {
				log.Printf("%d - %s\n", i, "OK")
				time.Sleep(1 * time.Second)
			}
		}(i)
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	<-c

    // Make sure any data in buffer is flushed to file before main goroutine quits.
	w.Close()
}

License

MIT

Documentation

Index

Constants

View Source
const (

	// RotateSecondly rotate every second
	RotateSecondly = 1

	// RotateMinutely rorate every minute
	RotateMinutely = 60 * RotateSecondly

	// RotateHourly rotate every hour
	RotateHourly = 60 * RotateMinutely

	// RotateDaily rotate every day
	RotateDaily = 24 * RotateHourly

	// RotateWeekly rotate every week
	RotateWeekly = 7 * RotateDaily
)

Variables

View Source
var (
	ErrClosed = errors.New("write stream closed")
)

Functions

This section is empty.

Types

type Buffer

type Buffer interface {
	io.WriteCloser

	// Flush writes any data in the buffer to underlying writer.
	Flush() error
}

Buffer interface defines buffer's common behaviors used by Rollout. A Buffer must implement io.WriteCloser interface. A Flush method is used to flush data to underlying writer before Rollout closes.

func NewFileBuffer

func NewFileBuffer(dest string, size int, interval time.Duration) (Buffer, error)

NewFileBuffer creates a new FileBuffer instance.

type BufferFunc

type BufferFunc func(dest string, size int, interval time.Duration) (Buffer, error)

BufferFunc is function to generate a new Buffer.

type BufferWriter

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

func NewWriterSize

func NewWriterSize(w io.Writer, size int) *BufferWriter

NewWriterSize returns a new Writer whose buffer has at least the specified size. If the argument io.Writer is already a Writer with large enough size, it returns the underlying Writer.

func (*BufferWriter) Available

func (b *BufferWriter) Available() int

Available returns how many bytes are unused in the buffer.

func (*BufferWriter) Buffered

func (b *BufferWriter) Buffered() int

Buffered returns the number of bytes that have been written into the current buffer.

func (*BufferWriter) Flush

func (b *BufferWriter) Flush() error

Flush writes any buffered data to the underlying io.Writer.

func (*BufferWriter) Write

func (b *BufferWriter) Write(p []byte) (nn int, err error)

Write writes the contents of p into the buffer. It returns the number of bytes written. If nn < len(p), it also returns an error explaining why the write is short.

type Clock

type Clock func() time.Time

Clock function used to get time. Mostly for testing purpose.

type FileBuffer

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

FileBuffer is a thread safe file writer with buffer. It is used to reduce disk IO. Guarantee atomic in single process writing situation.

func (*FileBuffer) Close

func (b *FileBuffer) Close() error

Close stops timer, flushes data, and closes the file.

func (*FileBuffer) Flush

func (b *FileBuffer) Flush() error

Flush writes buffered data to file.

func (*FileBuffer) Write

func (b *FileBuffer) Write(p []byte) (int, error)

Write writes contents of p into the buffer.

type Options

type Options struct {

	// Template is a template string for output destination name. Useable variables are `Host`, `Pid` and `Time`.
	// You can change time format by providing `TimeFormat` option.
	// In the situation of multiple processes, it is highly recommended to add `{{.Pid}}` in the template to avoid
	// writing conflicts. If you run multiple processes in docker in the same machine, and they all write to the
	// same directory in the host, add `{{.Host}}` in the template.
	Template string

	// TimeFormat is format string for `Template`'s Time field value. Default is "2016-01-02".
	TimeFormat string

	// Root is prefix of output destination name. In the built-in file buffer, it is treated as file directory.
	Root string

	// Rotation is the frequency how often write to a new destination. Default is RotateDaily.
	Rotation int

	// Keeps is how many destination copies will be retained. Default is 30.
	Keeps int

	// BufferSize is the size of underlying buffer. Default is 4096.
	BufferSize int

	// Flush is the interval for buffer automaticly flushing. Default is 10.
	Flush int

	// BufferFunc is a function generating new buffer. Default value is the built-in NewFileBuffer.
	BufferFunc BufferFunc

	// Clock is function to get current time.
	Clock Clock
}

Options is data for create Rollout instance.

type Rollout

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

Rollout is an io.WriteCloser. It is used for writing logs to rolling files. Output Buffer is an interface, so you can define your own Buffer and BufferFunc to use another underlying writer other than built-in file buffer.

func New

func New(options Options) *Rollout

New creates Rollout instance.

func (*Rollout) Close

func (r *Rollout) Close() error

Close the writer. There may be data present in current buffer when main goroutine quits. Such data will lost if you don't flush it to the underlying writer. Close will flushes any data in the buffer to current logging file and then closes the file descriptor. So make sure Rollout is closed before main goroutine quits.

func (*Rollout) Flush

func (r *Rollout) Flush() error

Flush writes buffered data to current file.

func (*Rollout) Write

func (r *Rollout) Write(p []byte) (n int, err error)

Write writes the contents of p into the buffer. It returns an error if its status is closed or it fails to create the logging file.

Directories

Path Synopsis
examples
log command

Jump to

Keyboard shortcuts

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