https://github.com/golift/rotatorr
Log Rotation for Go Apps. Epic wave control in a bathing suit!
https://github.com/golift/rotatorr
golang-library golang-package logfiles logrotate logrotation
Last synced: 10 months ago
JSON representation
Log Rotation for Go Apps. Epic wave control in a bathing suit!
- Host: GitHub
- URL: https://github.com/golift/rotatorr
- Owner: golift
- License: mit
- Created: 2020-12-12T10:41:19.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-03-31T15:01:59.000Z (about 1 year ago)
- Last Synced: 2025-04-15T03:55:40.543Z (about 1 year ago)
- Topics: golang-library, golang-package, logfiles, logrotate, logrotation
- Language: Go
- Homepage: https://golift.io/discord
- Size: 84 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Rotatorr
## Go App Log Rotation!
[](https://godoc.org/golift.io/rotatorr)
[](https://goreportcard.com/report/golift.io/rotatorr)
[](https://github.com/golift/rotatorr/blob/master/LICENSE)
[](https://golift.io/discord)
### Description
Rotatorr provides a simple `io.WriteCloser` you can plug into the default `log`
package. This interface handles log rotation while providing many features and
overridable interfaces to customize the rotation experience. Inspired by
[Lumberjack](https://github.com/natefinch/lumberjack). I wrote this because I
wanted integer log files, and I figured why not fix a few of the things
reported in the Lumberjack issues and pull requests.
### Simple Usage
This example rotates logs once they reach 10mb. The backup log files have a
time stamp written to their name.
```go
log.SetOutput(rotatorr.NewMust(&rotatorr.Config{
Filesize: 1024 * 1024 * 10, // 10 megabytes
Filepath: "/var/log/service.log",
Rotatorr: &timerotator.Layout{FileCount: 10},
}))
```
### Advanced Usage
In the example above you can see that the `Rotatorr` interface is satisfied by
`*timerotator.Layout`. The other built-in option is `*introtator.Layout`.
As a version 0 package, some of the interfaces are bound to change as we find bugs
and make further improvements. Feedback and bug reports are welcomed and encouraged!
The [time rotator](https://pkg.go.dev/golift.io/rotatorr/timerotator)
puts time stamps in the backup log file names.
The [int rotator](https://pkg.go.dev/golift.io/rotatorr/introtator)
uses an integer (like `logfile.1.log`). Pick one and stick with it for best results.
You may also enable compression by adding a callback to either rotator that calls
the included [compressor](https://pkg.go.dev/golift.io/rotatorr/compressor) library.
**All the advanced examples are in [godoc](https://pkg.go.dev/golift.io/rotatorr)**,
or just check out the [examples_test.go](examples_test.go) file in this repo and the
[example app](cmd/exampleapp/main.go) that's included.
Below you'll find the three main data structures you can provide to this
package to make log file rotation work just the way you want.
#### Type: `rotatorr.Config`
All of the struct members are optional except the `Rotatorr` interface.
```go
type Config struct {
Filepath string // Full path to log file. Set this, the default is lousy.
FileMode os.FileMode // POSIX mode for new files.
DirMode os.FileMode // POSIX mode for new folders.
Every time.Duration // Maximum log file age. Rotate every hour or day, etc.
FileSize int64 // Maximum log file size in bytes. Default is unlimited (no rotation).
Rotatorr Rotatorr // REQUIRED: Custom log Rotatorr. Use your own or one of the provided interfaces.
}
```
#### Type: `introtator.Layout`
- `Rotatorr` interface.
```go
type Layout struct {
ArchiveDir string // Location where rotated backup logs are moved to.
FileCount int // Maximum number of rotated log files.
FileOrder Order // Control the order of the integer-named backup log files.
PostRotate func(fileName, newFile string)
}
```
#### Type: `timerotator.Layout`
- `Rotatorr` interface.
```go
type Layout struct {
ArchiveDir string // Location where rotated backup logs are moved to.
FileCount int // Maximum number of rotated log files.
FileAge time.Duration // Maximum age of rotated files.
UseUTC bool // Sets the time zone to UTC when writing Time Formats (backup files).
Format string // Format for Go Time. Used as the name.
Joiner string // The string betwene the file name prefix and time stamp. Default: -
PostRotate func(fileName, newFile string)
}
```