Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extend logger to include compression rate and capacity #169

Open
wants to merge 1 commit into
base: v2.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module github.com/natefinch/lumberjack

require (
github.com/BurntSushi/toml v0.3.1
github.com/juju/ratelimit v1.0.2 // indirect
gopkg.in/yaml.v2 v2.2.2
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/juju/ratelimit v1.0.2 h1:sRxmtRiajbvrcLQT7S+JbqU0ntsb9W2yhSdNN8tWfaI=
github.com/juju/ratelimit v1.0.2/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
Expand Down
17 changes: 14 additions & 3 deletions lumberjack.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"strings"
"sync"
"time"

"github.com/juju/ratelimit"
)

const (
Expand Down Expand Up @@ -107,6 +109,14 @@ type Logger struct {
// using gzip. The default is not to perform compression.
Compress bool `json:"compress" yaml:"compress"`

// CompressRate determines the rate tokens per second up to the given
// capacity using gzip.
CompressRate float64 `json:"compressRate" yaml:"compress_rate"`

// CompressCapacity determines the rate tokens per second up to the given
// capacity using gzip.
CompressCapacity int64 `json:"compressCapacity" yaml:"compress_capacity"`

size int64
file *os.File
mu sync.Mutex
Expand Down Expand Up @@ -364,7 +374,7 @@ func (l *Logger) millRunOnce() error {
}
for _, f := range compress {
fn := filepath.Join(l.dir(), f.Name())
errCompress := compressLogFile(fn, fn+compressSuffix)
errCompress := compressLogFile(fn, fn+compressSuffix, l.CompressRate, l.CompressCapacity)
if err == nil && errCompress != nil {
err = errCompress
}
Expand Down Expand Up @@ -465,7 +475,7 @@ func (l *Logger) prefixAndExt() (prefix, ext string) {

// compressLogFile compresses the given log file, removing the
// uncompressed log file if successful.
func compressLogFile(src, dst string) (err error) {
func compressLogFile(src string, dst string, compressRate float64, compressCapacity int64) (err error) {
f, err := os.Open(src)
if err != nil {
return fmt.Errorf("failed to open log file: %v", err)
Expand Down Expand Up @@ -498,7 +508,8 @@ func compressLogFile(src, dst string) (err error) {
}
}()

if _, err := io.Copy(gz, f); err != nil {
bucket := ratelimit.NewBucketWithRate(compressRate, compressCapacity)
if _, err := io.Copy(gz, ratelimit.Reader(f, bucket)); err != nil {
return err
}
if err := gz.Close(); err != nil {
Expand Down