Documentation ¶
Overview ¶
Copyright (C) BABEC. All rights reserved. Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
SPDX-License-Identifier: Apache-2.0
Copyright (C) BABEC. All rights reserved. Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
SPDX-License-Identifier: Apache-2.0
Copyright (C) BABEC. All rights reserved. Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
SPDX-License-Identifier: Apache-2.0
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
- Variables
- type Clock
- type Event
- type EventType
- type FileRotatedEvent
- type Handler
- type HandlerFunc
- type Option
- func ForceNewFile() Option
- func WithArchivePath(archivePath string) Option
- func WithClock(c Clock) Option
- func WithCompress(compress bool) Option
- func WithHandler(h Handler) Option
- func WithHmacKey(hmacKey string) Option
- func WithLinkName(s string) Option
- func WithLocation(loc *time.Location) Option
- func WithMaxAge(d time.Duration) Option
- func WithNoCompressCount(noCompressCount int) Option
- func WithRotationCount(n uint) Option
- func WithRotationSize(s int64) Option
- func WithRotationTime(d time.Duration) Option
- type RotateLogs
Constants ¶
const ( GZ = ".gz" ZIP = ".zip" )
Variables ¶
Local is an object satisfying the Clock interface, which returns the current time in the local timezone
UTC is an object satisfying the Clock interface, which returns the current time in UTC
Functions ¶
This section is empty.
Types ¶
type Clock ¶
Clock is the interface used by the RotateLogs object to determine the current time
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 HandlerFunc ¶
type HandlerFunc func(Event)
func (HandlerFunc) Handle ¶
func (h HandlerFunc) Handle(e Event)
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 ¶
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 ¶
WithHandler creates a new Option that specifies the Handler object that gets invoked when an event occurs. Currently `FileRotated` event is supported
func WithLinkName ¶
WithLinkName creates a new Option that sets the symbolic link name that gets linked to the current file name being used.
func WithLocation ¶
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 ¶
WithMaxAge creates a new Option that sets the max age of a log file before it gets purged from the file system.
func WithRotationCount ¶
WithRotationCount creates a new Option that sets the number of files should be kept before it gets purged from the file system.
func WithRotationSize ¶
WithRotationSize creates a new Option that sets the log file size 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 强制旋转日志文件。如果生成的文件名发生冲突,因为文件已经存在, 则形如 ".1", ".2", ".3" 等数字后缀将附加到日志文件的末尾。
该方法可以与信号处理程序一起使用,以模拟在接收到 SIGHUP 时生成新日志文件的服务器。
func (*RotateLogs) Write ¶
func (rl *RotateLogs) Write(p []byte) (n int, err error)
Write 方法满足 io.Writer 接口。它将数据写入当前正在使用的适当文件句柄。 如果已到达旋转时间,则目标文件会自动旋转,并在必要时清除。