rotatewriter

package module
v0.0.0-...-3df0c1a Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2017 License: MIT Imports: 11 Imported by: 44

README

Rotation log writer

Build StatusGo Report Card

This is log writer to support rotation.

Note: logs directory in library intended for testing purposes

Usage

Example

You can use RotateWriter as standard golang log writer or use as zerolog writer.

Example for zerolog case:

package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
    "time"

    "github.com/ilya1st/rotatewriter"
    "github.com/rs/zerolog"
)

func main() {
    // this is for test
    writer, err := rotatewriter.NewRotateWriter("./logs/test.log", 8)
    if err != nil {
        panic(err)
    }
    sighupChan := make(chan os.Signal, 1)
    signal.Notify(sighupChan, syscall.SIGHUP)
    go func() {
        for {
            _, ok := <-sighupChan
            if !ok {
                return
            }
            fmt.Println("Log rotation")
            writer.Rotate(nil)
        }
    }()
    logger := zerolog.New(writer).With().Timestamp().Logger()
    fmt.Println("Just run in another console and look into logs directory:\n$ killall -HUP rotateexample")
    for {
        logger.Info().Msg("test")
        time.Sleep(500 * time.Millisecond)
    }
}
rotatewriter.NewRotateWriter() arguments

First argument is full log filename.

Second argument - number of files to store.

If it's 0 - writer just reopen file. This is for cases you use logrotate instrument around your app

rotatewriter.Rotate()

You call it when you need rotation. It has specific locks do not run rotation twice in same moment. you may pass callback tu function to know rotation is ready.

rotatewriter.RotationInProgress()

You can determine is rotation operation in progress or not if you need

Why

This package was inspired by lumberjack - but there is an issue with blocking while log rotation and it's overloaded with features as size counting, gzip but I need just correctly handle reopen.

Install

go get github.com/ilya1st/rotatewriter

Documentation

Overview

Package rotatewriter contains additional tool for logging packages - RotateWriter Writer which implement normal fast smooth rotation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RotateWriter

type RotateWriter struct {
	Filename string
	NumFiles int

	IsBuffered   bool
	FlushTimeout time.Duration

	BufferSize int
	// contains filtered or unexported fields
}

RotateWriter is Writer with Rotate function to make correctly rotation of

func NewRotateBufferedWriter

func NewRotateBufferedWriter(fileName string, numfiles int, flushTimeout time.Duration, bufferSize int) (rw *RotateWriter, err error)

NewRotateBufferedWriter creates new buffered instance make some checks there fileName: filename, must contain existing directory file numfiles: 0 if no rotation at all - just reopen file on rotation. e.g. you would like use logrotate numfiles: >0 if rotation enabled flush timeout - flush after timeout when there are no writes buffer size to work with

func NewRotateWriter

func NewRotateWriter(fileName string, numfiles int) (rw *RotateWriter, err error)

NewRotateWriter creates new instance make some checks there fileName: filename, must contain existing directory file numfiles: 0 if no rotation at all - just reopen file on rotation. e.g. you would like use logrotate numfiles: >0 if rotation enabled

func (*RotateWriter) CloseWriteFile

func (rw *RotateWriter) CloseWriteFile() error

CloseWriteFile use to close writer if you need

func (*RotateWriter) Rotate

func (rw *RotateWriter) Rotate(ready func()) error

Rotate rotates file

func (*RotateWriter) RotationInProgress

func (rw *RotateWriter) RotationInProgress() bool

RotationInProgress detects rotation is running now

func (*RotateWriter) Write

func (rw *RotateWriter) Write(p []byte) (n int, err error)

Write implements io.Writer

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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