rotate

package
v0.0.0-...-349f3ea Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 18 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// UTC is an object satisfying the Clock interface, which
	// returns the current time in UTC.
	UTC = clockFn(func() time.Time { return time.Now().UTC() })

	// Local is an object satisfying the Clock interface, which
	// returns the current time in the local timezone.
	Local = clockFn(time.Now)
)

nolint:gochecknoglobals

View Source
var GologDebug = func() bool {
	v := os.Getenv("GOLOG_DEBUG")
	v = strings.ToLower(v)
	switch v {
	case "on", "1", "yes", "true":
		return true
	default:
		return false
	}
}()

GologDebug 加载后立即执行,此时还未获得设置的参数,在 startup 中获取

Functions

func InnerPrint

func InnerPrint(format string, a ...interface{})

func WrapWriter

func WrapWriter(w LevelWriter) io.Writer

Types

type BufioWriteCloser

type BufioWriteCloser struct {
	*bufio.Writer
	// contains filtered or unexported fields
}

func NewBufioWriteCloser

func NewBufioWriteCloser(w io.WriteCloser) *BufioWriteCloser

func (*BufioWriteCloser) Close

func (b *BufioWriteCloser) Close() error

type Clock

type Clock interface {
	Now() time.Time
}

Clock is the interface used by the Rotate object to determine the current time.

type Event

type Event interface{}

Event defines the event interface to the handler.

type FileRotatedEvent

type FileRotatedEvent struct {
	PreviousFile string // previous filename
	CurrentFile  string // current, new filename
}

FileRotatedEvent is the event when file rotating occurred.

type FlushWriteCloser

type FlushWriteCloser interface {
	io.WriteCloser
	Flush() error
}

type Handler

type Handler interface {
	Handle(Event)
}

Handler defines the event handler interface.

type HandlerFunc

type HandlerFunc func(Event)

HandlerFunc is the handler interface function type.

func (HandlerFunc) Handle

func (h HandlerFunc) Handle(e Event)

Handle handles the event.

type LevelWriter

type LevelWriter interface {
	Write(level logrus.Level, p []byte) (n int, err error)
}

func WrapLevelWriter

func WrapLevelWriter(w io.Writer) LevelWriter

type Option

type Option interface {
	Apply(r *Rotate)
}

Option defines the option interface.

type OptionFn

type OptionFn func(*Rotate)

OptionFn is the option function prototype.

func WithClock

func WithClock(c Clock) OptionFn

WithClock creates a new Option that sets a clock that the Rotate object will use to determine the current time.

By default, Rotate.Local, which returns the current time in the local time zone, is used. If you would rather use UTC, use Rotate.UTC as the argument to this option, and pass it to the constructor.

func WithGzipAge

func WithGzipAge(v time.Duration) OptionFn

WithGzipAge creates a new Option that sets the max age of a log file before it gets compressed in gzip.

func WithHandler

func WithHandler(v Handler) OptionFn

WithHandler creates a new Option that specifies the Handler object that gets invoked when an event occurs. Currently, `FileRotated` event is supported.

func WithMaxAge

func WithMaxAge(v time.Duration) OptionFn

WithMaxAge creates a new Option that sets the max age of a log file before it gets purged from the file system.

func WithMaxSize

func WithMaxSize(v int64) OptionFn

WithMaxSize set how much max size should a log file be rotated. e.g. 100*spec.MiB

func WithRotateFullLayout

func WithRotateFullLayout(v string) OptionFn

WithRotateFullLayout creates a layout of the final rotated file. eg. log/2006-01-02/file.log for daily rotation layout.

func WithRotateLayout

func WithRotateLayout(v string) OptionFn

WithRotateLayout creates a layout for the postfix of rotated file. eg. .2006-01-02 for daily rotation.

func WithTotalSizeCap

func WithTotalSizeCap(v int64) OptionFn

WithTotalSizeCap 用来指定所有日志文件的总大小上限(可选), 例如设置为3GB的话,那么到了这个值,就会删除旧的日志.

func (OptionFn) Apply

func (f OptionFn) Apply(r *Rotate)

Apply applies the option.

type OptionFns

type OptionFns []OptionFn

OptionFns is the slice of option.

func (OptionFns) Apply

func (o OptionFns) Apply(r *Rotate)

Apply applies the options.

type Rotate

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

Rotate represents a log file that gets automatically rotated as you write to it.

func New

func New(logfile string, options ...OptionFn) (*Rotate, error)

New creates a new Rotate object. A logfile filename must be passed. Optional `Option` parameters may be passed.

Example
package main

import (
	"fmt"
	"os"

	"github.com/bingoohuang/golog/pkg/rotate"
	"github.com/sirupsen/logrus"
)

func main() {
	logDir, err := os.MkdirTemp("", "rotate_test")
	if err != nil {
		fmt.Println("could not create log directory ", err)
		return
	}

	logPath := fmt.Sprintf("%s/test.log", logDir)

	for i := 0; i < 2; i++ {
		writer, err := rotate.New(logPath)
		if err != nil {
			fmt.Println("Could not open log file ", err)
			return
		}

		n, err := writer.Write(logrus.InfoLevel, []byte("test"))
		if err != nil || n != 4 {
			fmt.Println("Write failed ", err, " number written ", n)
			return
		}

		err = writer.Close()

		if err != nil {
			fmt.Println("Close failed ", err)
			return
		}
	}

	files, err := os.ReadDir(logDir)
	if err != nil {
		fmt.Println("ReadDir failed ", err)
		return
	}

	for _, file := range files {
		info, _ := file.Info()
		fmt.Println(file.Name(), info.Size())
	}

	err = os.RemoveAll(logDir)
	if err != nil {
		fmt.Println("RemoveAll failed ", err)
		return
	}
}

func (*Rotate) Close

func (rl *Rotate) Close() error

Close satisfies the io.Closer interface. You must call this method if you performed any writes to the object.

func (*Rotate) CurrentFileName

func (rl *Rotate) CurrentFileName() string

CurrentFileName returns the current file name that the Rotate object is writing to.

func (*Rotate) GenBaseFilename

func (rl *Rotate) GenBaseFilename() (string, time.Time)

func (*Rotate) LogFile

func (rl *Rotate) LogFile() string

LogFile returns the current file name that the Rotate object is writing to.

func (*Rotate) Rotate

func (rl *Rotate) Rotate() error

Rotate forcefully rotates the log files. If the generated file name clash because file already exists, a numeric suffix of the form ".1", ".2", ".3" and so forth are appended to the end of the log file

This method can be used in conjunction with a signal handler so to emulate servers that generate new log files when they receive a SIGHUP.

func (*Rotate) Write

func (rl *Rotate) Write(level logrus.Level, p []byte) (n int, err error)

Write satisfies the io.Writer interface. It writes to the appropriate file handle that is currently being used. If we have reached rotation time, the target file gets automatically rotated, and also purged if necessary.

type WriterFormatter

type WriterFormatter struct {
	LevelWriter
	Formatter logrus.Formatter
}

WriterFormatter is map for mapping a log level to an io.Writer. Multiple levels may share a writer, but multiple writers may not be used for one level.

Jump to

Keyboard shortcuts

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