Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/easycz/logrotate
Concurrency safe log rotating writer for Golang
https://github.com/easycz/logrotate
go golang logging writer
Last synced: 2 months ago
JSON representation
Concurrency safe log rotating writer for Golang
- Host: GitHub
- URL: https://github.com/easycz/logrotate
- Owner: easyCZ
- License: mit
- Created: 2020-02-01T20:58:25.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-02T23:43:23.000Z (almost 5 years ago)
- Last Synced: 2023-08-12T12:39:10.855Z (over 1 year ago)
- Topics: go, golang, logging, writer
- Language: Go
- Homepage: https://godoc.org/github.com/easyCZ/logrotate
- Size: 17.6 KB
- Stars: 12
- Watchers: 3
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# logrotate
Concurrency safe logging writer for Golang for application-side log rotation.## Motivation
In general, application-side log rotation should be avoided. Log rotation is best done through [logrotate](https://linux.die.net/man/8/logrotate) and other tools.
However, some applications are constrained to only application-side log rotation and benefit from this package.## Usage
```
go get github.com/easyCZ/logrotate
``````go
package mainimport (
"fmt"
"log"
"os"
"time"
"github.com/easyCZ/logrotate"
)func main() {
logger := log.New(os.Stderr, "logrotate", log.LstdFlags) // Or any other logger conforming to golang's log.Logger
writer, err := logrotate.New(logger, logrotate.Options{
// Where should the writer be outputting files?
// If the directory does not exist, it will be created.
// Required.
Directory: "path/to/my/logs/directory",
// What is the maximum size of each file?
// Optional. Use 0 for unlimited.
MaximumFileSize: 1024 * 1024 * 1024,
// How often should a new file be created, based on time?
// Optional. Use 0 to disable time based log rotation.
MaximumLifetime: time.Hour,
// How would you like to name your files?
// Invoked each time a new file is being created.
FileNameFunc: logrotate.DefaultFilenameFunc,
})
if err != nil {
// handle err
}
// ...
// Ensure all messages are flushed to files before exiting
if err := writer.Close(); err != nil {
// handle err
}
}
```### Usage with Golang's log.Logger
```go
package mainimport (
"log"
"os""github.com/easyCZ/logrotate"
)func main() {
// Write logrotate's own log lines to stderr
stderrLogger := log.New(os.Stderr, "logrotate", log.LstdFlags)writer, err := logrotate.New(stderrLogger, logrotate.Options{
Directory: "/path/to/my/logs",
// see other options above
})// Your application logger, using logrotate as the backing Writer implementation.
logger := log.New(writer, "application", log.LstdFlags)// Ensure all messages are flushed to files before exiting
if err := writer.Close(); err != nil {
// handle err
}
}
```### Usage with logrus
```go
package mainimport (
"os"
log "github.com/sirupsen/logrus"
logrotate "github.com/easyCZ/logrotate"
)func init() {
writer, err := logrotate.New(logger, logrotate.Options{
Directory: "path/to/my/logs/directory",
})
if err != nil {
// handle err
}
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(writer)
}
```