log

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

package rotatelogs is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.

Index

Constants

View Source
const (
	DefaultEnvType            = "dev"                  //默认的开发环境
	DefaultFileCount    uint  = 0                      //0:为不限制
	DefaultFileSize     int64 = 32 * 1024 * 1024       //32兆
	DefaultFilePath           = "./glogs/%Y-%m-%d.log" //日志路径
	DefaultTemplateJoin       = " "                    //日志分割符 msg:"信息1 信息2"
	//错误级别
	LevelInfo  = "info"
	LevelWarn  = "warn"
	LevelError = "error"
)

Variables

View Source
var Local = clockFn(time.Now)

Local is an object satisfying the Clock interface, which returns the current time in the local timezone

View Source
var UTC = clockFn(func() time.Time { return time.Now().UTC() })

UTC is an object satisfying the Clock interface, which returns the current time in UTC

Functions

func Error

func Error(template ...interface{})

日志信息

func GetWriter

func GetWriter(filename string, options ...Option) io.Writer

按天切割按大小切割 filename 文件名 rotationSize 每个文件的大小 maxAge 文件最大保留天数 rotationCount 最大保留文件个数 rotationTime 设置文件分割时间 rotationCount 设置保留的最大文件数量

func Info

func Info(template ...interface{})

日志信息

func InitLog

func InitLog(options ...LogOptionsFunc)

func Warn

func Warn(template ...interface{})

日志信息

Types

type Clock

type Clock interface {
	Now() time.Time
}

Clock is the interface used by the RotateLogs object to determine the current time

type Event

type Event interface {
	Type() EventType
}

type EventType

type EventType int
const (
	InvalidEventType EventType = iota
	FileRotatedEventType
)

type FileRotatedEvent

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

func (*FileRotatedEvent) CurrentFile

func (e *FileRotatedEvent) CurrentFile() string

func (*FileRotatedEvent) PreviousFile

func (e *FileRotatedEvent) PreviousFile() string

func (*FileRotatedEvent) Type

func (e *FileRotatedEvent) Type() EventType

type Handler

type Handler interface {
	Handle(Event)
}

type HandlerFunc

type HandlerFunc func(Event)

func (HandlerFunc) Handle

func (h HandlerFunc) Handle(e Event)

type LogOptionsFunc

type LogOptionsFunc func(*logConfig)

配置回调

func SetEnvType

func SetEnvType(envType string) LogOptionsFunc

设置环境类型

func SetFileCount

func SetFileCount(fileCount uint) LogOptionsFunc

设置文件总数

func SetFilePath

func SetFilePath(logPath string) LogOptionsFunc

设置文件路径

func SetLogField

func SetLogField(key string, value interface{}) LogOptionsFunc

日志扩展字段

func SetLogFields

func SetLogFields(field map[string]interface{}) LogOptionsFunc

日志扩展字段 不会追加 采用覆盖

func SetMaxAge

func SetMaxAge(maxAge time.Duration) LogOptionsFunc

日志最大保留的天数

func SetOnlyFileSize

func SetOnlyFileSize(onlyFileSize int64) LogOptionsFunc

设置单个文件大小(大小限制)

func SetRotationCount

func SetRotationCount(n time.Duration) LogOptionsFunc

设置保留的最大文件数量、没有默认值(表示不限制)

type Option

type Option interface {
	Name() string
	Value() interface{}
}

Option is used to pass optional arguments to the RotateLogs constructor

func ForceNewFile

func ForceNewFile() Option

ForceNewFile ensures a new file is created every time New() is called. If the base file name already exists, an implicit rotation is performed

func WithClock

func WithClock(c Clock) Option

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

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

func WithHandler

func WithHandler(h Handler) Option

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

func WithLinkName

func WithLinkName(s string) Option

WithLinkName creates a new Option that sets the symbolic link name that gets linked to the current file name being used.

func WithLocation

func WithLocation(loc *time.Location) Option

WithLocation creates a new Option that sets up a "Clock" interface that the RotateLogs object will use to determine the current time.

This optin works by always returning the in the given location.

func WithMaxAge

func WithMaxAge(d time.Duration) Option

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

func WithRotationCount

func WithRotationCount(n uint) Option

WithRotationCount creates a new Option that sets the number of files should be kept before it gets purged from the file system.

func WithRotationSize

func WithRotationSize(s int64) Option

WithRotationSize creates a new Option that sets the log file size between rotation.

func WithRotationTime

func WithRotationTime(d time.Duration) Option

WithRotationTime creates a new Option that sets the time between rotation.

type RotateLogs

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

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

func New

func New(p string, options ...Option) (*RotateLogs, error)

New creates a new RotateLogs object. A log filename pattern must be passed. Optional `option` parameters may be passed

func (*RotateLogs) Close

func (rl *RotateLogs) Close() error

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

func (*RotateLogs) CurrentFileName

func (rl *RotateLogs) CurrentFileName() string

CurrentFileName returns the current file name that the RotateLogs object is writing to

func (*RotateLogs) Rotate

func (rl *RotateLogs) 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

Thie 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 (*RotateLogs) Write

func (rl *RotateLogs) Write(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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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