logrotate

package module
v0.0.0-...-7384375 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: Apache-2.0 Imports: 17 Imported by: 2

README

glogrotation

Advanged Go! Log Rotation library implementation.

Installation

go get -u github.com/moisespsena-go/glogrotation

Usage

The Rotator struct is the concurrent io.Writer implementation.

import library:

import "github.com/moisespsena-go/glogrotation"
Rotator object

Basic:

rotator := glogrotation.New("app.log")

With custom options:

rotator := glogrotation.New("app.log", glogrotation.Options{})
Options

The Options structure:

type Options struct {
	HistoryPath  string // path inside root
	MaxSize      int64 // -1 disables limitter
	Duration     RotationDuration // Default is glogrotation.MaxSize (10M)
	FileMode     os.FileMode
	DirMode      os.FileMode
	HistoryDir   string
	HistoryCount int
}

Options.Duration field values:

  • glogrotation.Monthly or char 'M'
  • glogrotation.Weeklyor char 'W'
  • glogrotation.Dailyor char 'D'
  • glogrotation.Hourly or char 'h'
  • glogrotation.Minutely or char 'm'
  • glogrotation.Yearly or char 'Y'

Load Options From config file:

var opt glogrotation.Options
if f, err := os.Open("options.yml"); err == nil {
	var cfg glogrotation.Config
    if err = yaml.NewDecoder(f).Decode(&cfg); err != nil {
    	panic(err)
    }
    if opt, err = cfg.Options(); err != nil {
    	panic(err)
    }
} else {
	panic(err)
}

rotator := glogrotation.New("app.log", opt)
Examples

Direct write:

n, err := rotator.Write([]byte(`message\n`)) 

using io.Copy:

var r io.Reader = os.Stdin // your reader
err := io.Copy(rotator, r) 
Implementation example

See to cli source code for example.

Author

Moises P. Sena

Documentation

Index

Constants

View Source
const T_FORMAT = "%Y%M%DT%h%m%s%Z"

Variables

View Source
var MaxSize int64 = 1024 * 1024 * 10

Functions

func TFormat

func TFormat(t time.Time, fmt string) string

Types

type Config

type Config struct {
	MaxSize      string `yaml:"max_size" mapstructure:"max_size"`
	Duration     string
	FileMode     os.FileMode `yaml:"file_mode" mapstructure:"file_mode"`
	DirMode      os.FileMode `yaml:"dir_mode" mapstructure:"dir_mode"`
	HistoryDir   string      `yaml:"history_dir" mapstructure:"history_dir"`
	HistoryPath  string      `yaml:"history_path" mapstructure:"history_path"`
	HistoryCount int         `yaml:"history_count" mapstructure:"history_count"`
}

func (Config) Options

func (this Config) Options() (opt Options, err error)

func (Config) Yaml

func (this Config) Yaml() string

type Control

type Control struct {
	Last time.Time
}

type Entries

type Entries []*FileHistroyEntry

type FileHistroyEntry

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

func (*FileHistroyEntry) AbsPath

func (this *FileHistroyEntry) AbsPath() string

func (*FileHistroyEntry) At

func (this *FileHistroyEntry) At() time.Time

func (*FileHistroyEntry) Path

func (this *FileHistroyEntry) Path() string

func (*FileHistroyEntry) Reader

func (this *FileHistroyEntry) Reader() (r io.ReadCloser, err error)

type Options

type Options struct {
	HistoryPath  string // path inside root
	MaxSize      int64  // -1 disables limitter
	Duration     RotationDuration
	FileMode     os.FileMode
	DirMode      os.FileMode
	HistoryDir   string
	HistoryCount int
}

type RotationDuration

type RotationDuration byte
const (
	Monthly  RotationDuration = 'M'
	Weekly   RotationDuration = 'W'
	Daily    RotationDuration = 'D'
	Hourly   RotationDuration = 'h'
	Minutely RotationDuration = 'm'
	Yearly   RotationDuration = 'Y'
)

func (RotationDuration) Valid

func (this RotationDuration) Valid() bool

type Rotator

type Rotator struct {
	Path    string
	Options Options

	Log logging.Logger
	// contains filtered or unexported fields
}

func New

func New(path string, options ...Options) *Rotator

func (*Rotator) AutoRotate

func (this *Rotator) AutoRotate(increaseSize int) (entry *FileHistroyEntry, err error)

func (*Rotator) Close

func (this *Rotator) Close() (err error)

func (*Rotator) Each

func (this *Rotator) Each(cb func(name, info string, finfo os.FileInfo) error) (err error)

func (*Rotator) File

func (this *Rotator) File() *os.File

func (*Rotator) History

func (this *Rotator) History(from, to time.Time, count int64) (events Entries, err error)

func (*Rotator) NewName

func (this *Rotator) NewName() (name, pth string)

func (*Rotator) NewNameT

func (this *Rotator) NewNameT(t time.Time) (name, pth string)

func (*Rotator) Open

func (this *Rotator) Open() (f *os.File, err error)

func (*Rotator) Rotate

func (this *Rotator) Rotate() (entry *FileHistroyEntry, err error)

func (*Rotator) RotateOptions

func (this *Rotator) RotateOptions() Options

func (*Rotator) Send

func (this *Rotator) Send(p []byte) (n int, err error)

func (*Rotator) Wait

func (this *Rotator) Wait()

func (*Rotator) Write

func (this *Rotator) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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