Documentation ¶
Overview ¶
Package logrotate can automatically rotate log files when you write to them according to the specified filename pattern and options.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultClock = systemClock{}
DefaultClock is the default clock used by logrotate in operations that require time. This clock uses the system clock for all operations.
Functions ¶
This section is empty.
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is an io.WriteCloser that writes to the appropriate filename. It can get automatically rotated as you write to it.
func New ¶
New creates a new concurrent safe Logger object with the provided filename pattern and options.
Example ¶
dir := "_logs/example/" defer os.RemoveAll(dir) l, _ := New( dir+"test.log", WithMaxSize(10), // 10 bytes ) log.SetOutput(l) log.Printf("Hello, World!") // 13 bytes log.Printf("Hello, World!") // 13 bytes time.Sleep(time.Second) // ensure already sink to files l.Close() files, _ := os.ReadDir(dir) for _, file := range files { fmt.Println(file.Name()) }
Output: test.log test.log.1 test.log.2
func (*Logger) Close ¶
Close implements io.Closer. It closes the writeLoop and millLoop goroutines and the current log file.
func (*Logger) Rotate ¶
Rotate forcefully rotates the log files. It will close the existing log file and immediately create a new one. This is a helper function for applications that want to initiate rotations outside of the normal rotation rules, such as in response to SIGHUP. After rotating, this initiates removal of old log files according to the configuration.
If the new generated log file name clash because file already exists, a sequence suffix of the form ".1", ".2", ".3" and so forth are appended to the end of the log file.
func (*Logger) Write ¶
Write implements io.Writer. If writeChSize <= 0, then it writes to the current file directly. Otherwise, it just writes to writeCh, so there is no blocking disk I/O operations and would not block unless writeCh is full. In the meantime, the writeLoop goroutine will sink the writeCh to files asynchronously in background.
NOTE: It's an undefined behavior if you still call Write after Close called. Maybe it would sink to files, maybe not, but it won't panic.
type Option ¶
type Option func(*Options)
Option is the functional option type.
func WithBufferedWrite ¶
WithBufferedWrite sets the buffered write channel size. If BufferedWrite <= 0, that means do not use buffered write.
Default: 0
func WithClock ¶
WithClock specifies the clock used by Logger to determine the current time. It defaults to the system clock with time.Now.
func WithMaxAge ¶
WithMaxAge sets the max age to retain old log files based on the timestamp encoded in their filename. If MaxAge <= 0, that means not remove old log files based on age.
Default: 0
func WithMaxBackups ¶
WithMaxBackups sets the maximum number of old log files to retain. If MaxBackups <= 0, that means retain all old log files (though MaxAge may still cause them to be removed.)
Default: 0
func WithMaxInterval ¶
WithMaxInterval sets the maximum interval between file rotation. In particular, the minimal interval unit is in time.Second level.
Default: 24 hours
func WithMaxSize ¶
WithMaxSize sets the maximum size of log file before it gets rotated. If MaxSize <= 0, that means not rotate log file based on size.
Default: 100 MB
func WithSymlink ¶
WithSymlink sets the symbolic link name that gets linked to the current filename being used.
Default: ""