Skip to content

Commit

Permalink
Merge pull request #7 from moul/dev/moul/flush
Browse files Browse the repository at this point in the history
feat: add Flush and FlushAll
  • Loading branch information
moul authored Jun 10, 2021
2 parents 6b2ec57 + 1cad705 commit e86a89b
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 48 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ func Example() {
MaxFiles: 10,
}

/*
// cleanup old log files for a specific app name
err := manager.GCWithName("my-app")
checkErr(err)
// cleanup old log files for a specific app name
err := manager.Flush("my-app")
checkErr(err)

// cleanup old log files for any app sharing this log directory
err = manager.GC()
checkErr(err)
*/
// cleanup old log files for any app sharing this log directory
err = manager.FlushAll()
checkErr(err)

// list existing log files
files, err := manager.Files()
Expand Down Expand Up @@ -93,6 +91,7 @@ type File struct {
File defines a log file with metadata.
func (f File) String() string
String implements Stringer.
type Manager struct {
// Path is the target directory containing the log files.
Expand All @@ -109,6 +108,12 @@ type Manager struct {
func (m Manager) Files() ([]File, error)
Files returns a list of existing log files.
func (m Manager) Flush(name string) error
Flush deletes old log files for the specified app name.
func (m Manager) FlushAll() error
FlushAll deletes old log files for any app name.
func (m Manager) New(name string) (io.WriteCloser, error)
Create a new log file and perform automatic GC of the old log files if
needed.
Expand Down
16 changes: 7 additions & 9 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ func Example() {
MaxFiles: 10,
}

/*
// cleanup old log files for a specific app name
err := manager.GCWithName("my-app")
checkErr(err)
// cleanup old log files for any app sharing this log directory
err = manager.GC()
checkErr(err)
*/
// cleanup old log files for a specific app name
err := manager.Flush("my-app")
checkErr(err)

// cleanup old log files for any app sharing this log directory
err = manager.FlushAll()
checkErr(err)

// list existing log files
files, err := manager.Files()
Expand Down
54 changes: 46 additions & 8 deletions logman.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,56 @@ func (m Manager) gc() error {
return errs
}

/*
// GCWithName cleans up old log files matching a specific name.
func (m Manager) GCWithName(name string) error {
return nil
// Flush deletes old log files for the specified app name.
func (m Manager) Flush(name string) error {
// FIXME: check if we can make something for active log files here
dir := m.dir()

if !u.DirExists(dir) {
return nil
}

files, err := m.Files()
if err != nil {
return err
}

var errs error
for _, file := range files {
if file.Name == name {
if err := os.Remove(file.Path); err != nil {
errs = multierr.Append(errs, err)
}
}
}

return errs
}

// FlushAll deletes old log files for any app name.
func (m Manager) FlushAll() error {
dir := m.dir()

if !u.DirExists(dir) {
return nil
}

// GC cleans up old log files.
func (m Manager) GC() error {
return nil
files, err := m.Files()
if err != nil {
return err
}
*/

var errs error
for _, file := range files {
if err := os.Remove(file.Path); err != nil {
errs = multierr.Append(errs, err)
}
}

return errs
}

// String implements Stringer.
func (f File) String() string {
return f.Path
}
66 changes: 43 additions & 23 deletions logman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"testing"
"time"

Expand Down Expand Up @@ -134,6 +135,7 @@ func TestLogfile(t *testing.T) {
// create 10 new files
{
for i := 0; i < 10; i++ {
time.Sleep(10 * time.Millisecond)
writer, err := manager.New(fmt.Sprintf("hello-%d", i))
require.NoError(t, err)
err = writer.Close()
Expand Down Expand Up @@ -167,33 +169,51 @@ func TestLogfile(t *testing.T) {
files, err := manager.Files()
require.NoError(t, err)
require.Len(t, files, 10)
}

/*
// try to gc with the current amount of files
{
err := logman.LogfileGC(tempdir, 10)
require.NoError(t, err)
names := []string{}
for _, file := range files {
names = append(names, file.Name)
}
sort.Strings(names)
require.Equal(t, names, []string{"hello-0", "hello-1", "hello-2", "hello-3", "hello-4", "hello-5", "hello-6", "hello-7", "hello-8", "hello-9"})
}

// check loading files from the directory, should still have ten
{
files, err := manager.Files()
require.NoError(t, err)
require.Len(t, files, 10)
}
// flush byname
{
err := manager.Flush("hello-3")
require.NoError(t, err)

// try to gc with only one
{
err := logman.LogfileGC(tempdir, 1)
require.NoError(t, err)
}
err = manager.Flush("hello-3")
require.NoError(t, err)

// check loading files from the directory, should now have only one
{
files, err := manager.Files()
require.NoError(t, err)
require.Len(t, files, 1)
err = manager.Flush("hello-7")
require.NoError(t, err)
}

// check loading files from the directory, should have eight now
{
files, err := manager.Files()
require.NoError(t, err)
require.Len(t, files, 8)

names := []string{}
for _, file := range files {
names = append(names, file.Name)
}
*/
sort.Strings(names)
require.Equal(t, names, []string{"hello-0", "hello-1", "hello-2", "hello-4", "hello-5", "hello-6", "hello-8", "hello-9"})
}

// flush all
{
err := manager.FlushAll()
require.NoError(t, err)
}

// check loading files from the directory, should have eight now
{
files, err := manager.Files()
require.NoError(t, err)
require.Len(t, files, 0)
}
}

0 comments on commit e86a89b

Please sign in to comment.