logrotate

package module
v0.0.0-...-2c1c889 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2020 License: Apache-2.0 Imports: 14 Imported by: 3

README

logrotate

Automatically cut log files by time and size, written in go.

logrotate 是用go写的用于根据时间或日志文件大小自动进行日志分割和压缩。

Examples

package main

import (
	"log"

	"github.com/gggwvg/logrotate"
)

func main() {
	// default
	logger, err := logrotate.NewLogger()
	if err != nil {
		panic(err)
	}
	log.SetOutput(logger)
	log.Println("default")
	logger.Close()

	// specify a log file
	opts := []logrotate.Option{
		logrotate.File("/tmp/test.log"),
	}
	logger, err = logrotate.NewLogger(opts...)
	if err != nil {
		panic(err)
	}
	log.SetOutput(logger)
	log.Println("log to test.log")
	logger.Close()

	// rotate via time period
	opts = append(opts, logrotate.RotatePeriod(logrotate.PeriodDaily))
	logger, err = logrotate.NewLogger(opts...)
	if err != nil {
		panic(err)
	}
	log.SetOutput(logger)
	log.Println("rotate by daily")
	logger.Close()

	// rotate via file size and time period
	opts = append(opts, logrotate.RotateSize("100m"))
	logger, err = logrotate.NewLogger(opts...)
	if err != nil {
		panic(err)
	}
	log.SetOutput(logger)
	log.Println("rotate by daily and file size 100m")
	logger.Close()
}

Documentation

Index

Constants

View Source
const (
	// PeriodHourly rotates log every hour
	PeriodHourly period = "hourly"
	// PeriodDaily rotates log by every day
	PeriodDaily period = "daily"
	// PeriodWeekly rotates log by every week
	PeriodWeekly period = "weekly"
	// PeriodMonthly rotates log by every month
	PeriodMonthly period = "monthly"
)

Variables

View Source
var (
	DefaultArchiveTimeFormat = "2006-01-02_15:04:05.000"
	DefaultMaxArchives       = 100
	DefaultMaxArchiveDays    = 14
)

Functions

This section is empty.

Types

type Logger

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

Logger is a logger with rotation function

func NewLogger

func NewLogger(opts ...Option) (*Logger, error)

NewLogger creates a new logger

func (*Logger) Close

func (l *Logger) Close() error

Close closes file resource

func (*Logger) Rotate

func (l *Logger) Rotate() error

Rotate cut logs by rules

func (*Logger) Write

func (l *Logger) Write(bs []byte) (n int, err error)

Write writes content into file. If the length of the content is greater than RotateSize, an error is returned.

type Option

type Option func(*Options)

func ArchiveTimeFormat

func ArchiveTimeFormat(format string) Option

func Compress

func Compress(compress bool) Option

func File

func File(name string) Option

func MaxArchiveDays

func MaxArchiveDays(days int) Option

func MaxArchives

func MaxArchives(number int) Option

func RotatePeriod

func RotatePeriod(p period) Option

func RotateSize

func RotateSize(size string) Option

type Options

type Options struct {
	// File is the file to write logs to.
	// It uses <process name>.log in os.TempDir() if empty.
	File string `json:"file" toml:"file" yaml:"file"`

	// RotatePeriod is time period for rotate log.
	// It supports hourly, daily, weekly, monthly.
	RotatePeriod string `json:"rotate_period" toml:"rotate_period" yaml:"rotate_period"`

	// RotateSize is the maximum size of the log file before it gets rotated
	RotateSize string `json:"rotate_size" toml:"rotate_size" yaml:"rotate_size"`

	// MaxArchives is the maximum number of old log files to retain
	MaxArchives int `json:"max_archives" toml:"max_archives" yaml:"max_archives"`

	// MaxArchiveDays is the maximum number of days to archived files
	MaxArchiveDays int `json:"max_archive_days" toml:"max_archive_days" yaml:"max_archive_days"`

	// ArchiveTimeFormat is the format of the archived files
	ArchiveTimeFormat string `json:"archive_time_format" toml:"archive_time_format" yaml:"archive_time_format"`

	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool `json:"compress" toml:"compress" yaml:"compress"`
	// contains filtered or unexported fields
}

func (*Options) Apply

func (o *Options) Apply() error

Directories

Path Synopsis
examples module

Jump to

Keyboard shortcuts

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