Skip to content

Commit

Permalink
feat: add compress and reserved disk size
Browse files Browse the repository at this point in the history
  • Loading branch information
huskar-t committed Aug 27, 2024
1 parent 7a5c6ec commit d5bc7a1
Show file tree
Hide file tree
Showing 14 changed files with 697 additions and 192 deletions.
34 changes: 9 additions & 25 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,17 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.15', '1.14' ]
go: [ '1.17', 'stable' ]
name: Go ${{ matrix.go }} test
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install Go stable version
if: matrix.go != 'tip'
uses: actions/setup-go@v2
- name: checkout TDengine by pr
uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
- name: Install Go tip
if: matrix.go == 'tip'
run: |
git clone --depth=1 https://go.googlesource.com/go $HOME/gotip
cd $HOME/gotip/src
./make.bash
echo "::set-env name=GOROOT::$HOME/gotip"
echo "::add-path::$HOME/gotip/bin"
echo "::add-path::$(go env GOPATH)/bin"
- name: Install GolangCI-Lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.27.0
- name: Test with coverage
run: make cover
- name: Upload code coverage to codecov
if: matrix.go == 'tip'
uses: codecov/codecov-action@v1
with:
file: ./coverage.out
- run: make lint

- name: test
id: test
run: sudo go test -v --count=1

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ _testmain.go
/.idea/file-rotatelogs.iml
/.idea/modules.xml
/.idea/vcs.xml
/.idea/remote-targets.xml
3 changes: 2 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

rotatelogs "github.com/taosdata/file-rotatelogs/v2"
)
Expand All @@ -15,7 +16,7 @@ func ExampleForceNewFile() {

return
}
logPath := fmt.Sprintf("%s/test.log", logDir)
logPath := filepath.Join(logDir, "test.log")

for i := 0; i < 2; i++ {
writer, err := rotatelogs.New(logPath,
Expand Down
21 changes: 12 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
module github.com/taosdata/file-rotatelogs/v2

go 1.12
go 1.17

require (
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect
github.com/jonboulle/clockwork v0.1.0
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc // indirect
github.com/lestrrat-go/strftime v0.0.0-20180821113735-8b31f9c59b0f
github.com/pkg/errors v0.8.1
github.com/stretchr/testify v1.3.0
github.com/tebeka/strftime v0.1.3 // indirect
github.com/jonboulle/clockwork v0.4.0
github.com/lestrrat-go/strftime v1.0.6
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.9.0
golang.org/x/sys v0.24.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
37 changes: 23 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 h1:Ghm4eQYC0nEPnSJdVkTrXpu9KtoVCSo1hg7mtI7G9KU=
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239/go.mod h1:Gdwt2ce0yfBxPvZrHkprdPPTTS3N5rwmLE8T22KBXlw=
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 h1:IPJ3dvxmJ4uczJe5YQdrYB16oTJlGSC/OyZDqUk9xX4=
github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj7dQo+O6GJNiMx+Pa94qKj+TG8ONdKHgMNIyyag=
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
github.com/lestrrat-go/strftime v0.0.0-20180821113735-8b31f9c59b0f h1:/o/LRlB6dBTBNViFglNdGfsDHBjdL8Yvfm7qQE4ZUh0=
github.com/lestrrat-go/strftime v0.0.0-20180821113735-8b31f9c59b0f/go.mod h1:RMlXygAD3c48Psmr06d2G75L4E4xxzxkIe/+ppX9eAU=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ=
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/tebeka/strftime v0.1.3 h1:5HQXOqWKYRFfNyBMNVc9z5+QzuBtIXy03psIhtdJYto=
github.com/tebeka/strftime v0.1.3/go.mod h1:7wJm3dZlpr4l/oVK0t1HYIc4rMzQ2XJlOMIUJUJH6XQ=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
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.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
40 changes: 25 additions & 15 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,31 @@ type FileRotatedEvent struct {
// RotateLogs represents a log file that gets
// automatically rotated as you write to it.
type RotateLogs struct {
clock Clock
curFn string
curBaseFn string
globPattern string
generation int
linkName string
maxAge time.Duration
mutex sync.RWMutex
eventHandler Handler
outFh *os.File
pattern *strftime.Strftime
rotationTime time.Duration
rotationSize int64
rotationCount uint
forceNewFile bool
clock Clock
curFn string
curBaseFn string
globPattern string
generation int
linkName string
lockFilename string
maxAge time.Duration
mutex sync.RWMutex
eventHandler Handler
outFh *os.File
lockFh *os.File
pattern *strftime.Strftime
rotationTime time.Duration
rotationSize int64
rotationCount uint
forceNewFile bool
logPath string
reservedDiskSize int64
availDiskSize int64
compress bool
rotateCleanChan chan struct{}
exitChan chan struct{}
done chan struct{}
cleanLockName string
}

// Clock is the interface used by the RotateLogs
Expand Down
15 changes: 13 additions & 2 deletions internal/fileutil/fileutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fileutil
import (
"os"
"path/filepath"
"runtime"
"time"

"github.com/lestrrat-go/strftime"
Expand Down Expand Up @@ -41,15 +42,25 @@ func GenerateFn(pattern *strftime.Strftime, clock interface{ Now() time.Time },

// CreateFile creates a new file in the given path, creating parent directories
// as necessary
func CreateFile(filename string) (*os.File, error) {
func CreateFile(filename string, appendFile bool) (*os.File, error) {
// make sure the dir is existed, eg:
// ./foo/bar/baz/hello.log must make sure ./foo/bar/baz is existed
dirname := filepath.Dir(filename)
if err := os.MkdirAll(dirname, 0755); err != nil {
return nil, errors.Wrapf(err, "failed to create directory %s", dirname)
}
// if we got here, then we need to create a file
fh, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
var fh *os.File
var err error
if appendFile {
if runtime.GOOS == "windows" {
fh, err = os.OpenFile(filename, os.O_CREATE|os.O_APPEND, 0644)
} else {
fh, err = os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
}
} else {
fh, err = os.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
}
if err != nil {
return nil, errors.Errorf("failed to open file %s: %s", filename, err)
}
Expand Down
17 changes: 17 additions & 0 deletions lock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build !windows
// +build !windows

package rotatelogs

import (
"os"
"syscall"
)

func LockFile(file *os.File) error {
return syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
}

func UnlockFile(file *os.File) error {
return syscall.Flock(int(file.Fd()), syscall.LOCK_UN|syscall.LOCK_NB)
}
20 changes: 20 additions & 0 deletions lock_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build windows
// +build windows

package rotatelogs

import (
"os"

"golang.org/x/sys/windows"
)

func LockFile(file *os.File) error {
ol := new(windows.Overlapped)
return windows.LockFileEx(windows.Handle(file.Fd()), uint32(windows.LOCKFILE_EXCLUSIVE_LOCK|windows.LOCKFILE_FAIL_IMMEDIATELY), 0, ^uint32(0), ^uint32(0), ol)
}

func UnlockFile(file *os.File) error {
ol := new(windows.Overlapped)
return windows.UnlockFileEx(windows.Handle(file.Fd()), 0, ^uint32(0), ^uint32(0), ol)
}
36 changes: 28 additions & 8 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import (
)

const (
optkeyClock = "clock"
optkeyHandler = "handler"
optkeyLinkName = "link-name"
optkeyMaxAge = "max-age"
optkeyRotationTime = "rotation-time"
optkeyRotationSize = "rotation-size"
optkeyRotationCount = "rotation-count"
optkeyForceNewFile = "force-new-file"
optkeyClock = "clock"
optkeyHandler = "handler"
optkeyLinkName = "link-name"
optkeyMaxAge = "max-age"
optkeyRotationTime = "rotation-time"
optkeyRotationSize = "rotation-size"
optkeyRotationCount = "rotation-count"
optkeyForceNewFile = "force-new-file"
optKeyReservedDiskSize = "reserved-disk-size"
optKeyGlobPattern = "glob-pattern"
optKeyCompress = "compress"
optKeyCleanLockFile = "clean-lock-file"
)

// WithClock creates a new Option that sets a clock
Expand Down Expand Up @@ -87,3 +91,19 @@ func WithHandler(h Handler) Option {
func ForceNewFile() Option {
return option.New(optkeyForceNewFile, true)
}

func WithReservedDiskSize(size int64) Option {
return option.New(optKeyReservedDiskSize, size)
}

func WithRotateGlobPattern(pattern string) Option {
return option.New(optKeyGlobPattern, pattern)
}

func WithCompress(compress bool) Option {
return option.New(optKeyCompress, compress)
}

func WithCleanLockFile(lockName string) Option {
return option.New(optKeyCleanLockFile, lockName)
}
Loading

0 comments on commit d5bc7a1

Please sign in to comment.