logro

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2019 License: MIT Imports: 11 Imported by: 0

README

Logro

Build Status

Introduction

Logro is a Go package for writing logs to rolling files. Inspired by lumberjack

Logro is built for high performance:

  • Write with buffer

    All log data will be written to a user-space buffer first, then flush to the log file.

  • Sync in background

    Use sync in background avoiding write stall in user-facing.

    ps: OS can do the page cache flush by itself, but it may create a burst of write I/O when dirty pages hit a threshold.

  • Clean page cache

    It's meaningless to keep log files' data in page cache, so when the dirty pages are too many or we need reopen a new file, Logro will sync data to disk, then drop the page cache.

  • ...

Methods

  • WriteSyncCloser

    Logro implements such methods:

        Write(p []byte) (written int, err error)
        Sync() (err error)
        Close() (err error)
    

    Could satisfy most of log packages.

Rotation

e.g. The log file's name is a.log, the log files will be:

    a.log
    a-time.log
    a-time.log
    a-time.log
    a-time.log
    ....

Log shippers such as ELK's filebeat can set path to:

    a.log    
Control

Logro control rotation by file size only, it's simple and enough for the most cases. (Now we usually use log shippers to collect logs to databases, but not login machines and grep data)

Warning
  1. The date in backup file name maybe not correct all the time. (some log entries won't belong to this interval)

  2. It will clean up log files when they are too many (see Config.MaxBackups).

Example

Stdlib Logger
    r, _ := New(&conf)
    log.New(r, "", log.Ldate)
Zap Logger
    r, _ := New(&conf)
    zapcore.AddSync(r)
    ...

Acknowledgments

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backup

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

Backup holds backup log file' path & create time.

type Backups

type Backups []Backup

Backups implements heap interface.

func (*Backups) Len

func (b *Backups) Len() int

func (*Backups) Less

func (b *Backups) Less(i, j int) bool

func (*Backups) Pop

func (b *Backups) Pop() (v interface{})

func (*Backups) Push

func (b *Backups) Push(v interface{})

func (*Backups) Swap

func (b *Backups) Swap(i, j int)

type Config

type Config struct {
	// Configs of Log Rotation.
	OutputPath string `json:"output_path" toml:"output_path"` // Log file path.
	// Maximum size of a log file before it gets rotated.
	// Unit is MB.
	MaxSize int64 `json:"max_size" toml:"max_size"`
	// Maximum number of backup log files to retain.
	MaxBackups int `json:"max_backups" toml:"max_backups"`
	// Timestamp in backup log file. Default is to use UTC time.
	LocalTime bool `json:"local_time" toml:"local_time"`

	// After write BytesPerSync bytes flush data to storage media(hint).
	BytesPerSync int64 `json:"bytes_per_sync" toml:"bytes_per_sync"`

	// Develop mode. Default is false.
	// It' used for testing, if it's true, the page cache control unit could not be aligned to page cache size.
	Developed bool `json:"developed" toml:"developed"`
}

Config of Logro.

type Rotation

type Rotation struct {
	sync.Mutex

	Backups *Backups // all backups information.
	// contains filtered or unexported fields
}

Rotation is implement io.WriteCloser interface with func Sync() (err error).

func New

func New(conf *Config) (r *Rotation, err error)

New create a Rotation.

func (*Rotation) Close

func (r *Rotation) Close() (err error)

func (*Rotation) Sync

func (r *Rotation) Sync() (err error)

Sync buf & dirty_page_cache to the storage media.

func (*Rotation) Write

func (r *Rotation) Write(p []byte) (written int, err error)

Write data to log file.

Jump to

Keyboard shortcuts

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