-
Notifications
You must be signed in to change notification settings - Fork 138
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
CBG-3925: Add FileLogger RotationInterval #6835
Changes from all commits
8db3dd5
e999283
f813dd8
1e52188
7809941
afc65de
6db3d23
09c52ef
7cdd728
3074ae6
75c7c26
9cc8b16
1f8c22d
f90d2f4
4271bff
2813cb2
0f61288
0b5307f
d799c7a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -10,15 +10,17 @@ package base | |||||
|
||||||
import ( | ||||||
"bytes" | ||||||
"context" | ||||||
"fmt" | ||||||
"os" | ||||||
"path/filepath" | ||||||
"runtime" | ||||||
"testing" | ||||||
"time" | ||||||
|
||||||
"github.com/natefinch/lumberjack" | ||||||
"github.com/stretchr/testify/assert" | ||||||
"github.com/stretchr/testify/require" | ||||||
"gopkg.in/natefinch/lumberjack.v2" | ||||||
) | ||||||
|
||||||
func TestRedactedLogFuncs(t *testing.T) { | ||||||
|
@@ -57,6 +59,71 @@ func Benchmark_LoggingPerformance(b *testing.B) { | |||||
} | ||||||
} | ||||||
|
||||||
func TestLogRotationInterval(t *testing.T) { | ||||||
LongRunningTest(t) | ||||||
|
||||||
// override min rotation interval for testing | ||||||
originalMinRotationInterval := minLogRotationInterval | ||||||
minLogRotationInterval = time.Millisecond * 10 | ||||||
defer func() { minLogRotationInterval = originalMinRotationInterval }() | ||||||
|
||||||
rotationInterval := time.Millisecond * 100 | ||||||
config := &FileLoggerConfig{ | ||||||
Enabled: BoolPtr(true), | ||||||
CollationBufferSize: IntPtr(0), | ||||||
Rotation: logRotationConfig{ | ||||||
RotationInterval: NewConfigDuration(rotationInterval), | ||||||
Compress: BoolPtr(false), | ||||||
}, | ||||||
} | ||||||
|
||||||
// On Windows, cleanup of t.TempDir() fails due to open log file handle from Lumberjack. Cannot be fixed from SG. | ||||||
// https://github.com/natefinch/lumberjack/issues/185 | ||||||
var logPath string | ||||||
if runtime.GOOS == "windows" { | ||||||
var err error | ||||||
logPath, err = os.MkdirTemp("", t.Name()) | ||||||
require.NoError(t, err) | ||||||
t.Cleanup(func() { | ||||||
if err := os.RemoveAll(logPath); err != nil { | ||||||
// log instead of error because it's likely this is going to fail on Windows for this test. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I wonder if the windows build machines are set up to clear temporary files, this could end up filing up the disks on the machine. We could consider writing to cwd if the builds do a fresh checkpoint of sync_gateway or do a Any reason here to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I battled with this for a day and ended up on this solution. The sleep doesn't prevent this error, because Lumberjack keeps the current file handle open for some reason, even after calling On *nix this doesn't matter because the OS will still let us remove the file/directory. |
||||||
t.Logf("couldn't remove temp dir: %v", err) | ||||||
} | ||||||
}) | ||||||
} else { | ||||||
logPath = t.TempDir() | ||||||
} | ||||||
|
||||||
countBefore := numFilesInDir(t, logPath, false) | ||||||
t.Logf("countBefore: %d", countBefore) | ||||||
|
||||||
ctx, ctxCancel := context.WithCancel(TestCtx(t)) | ||||||
defer ctxCancel() | ||||||
|
||||||
fl, err := NewFileLogger(ctx, config, LevelTrace, "test", logPath, 0, nil) | ||||||
require.NoError(t, err) | ||||||
defer func() { require.NoError(t, fl.Close()) }() | ||||||
|
||||||
fl.logf("test 1") | ||||||
countAfter1 := numFilesInDir(t, logPath, false) | ||||||
t.Logf("countAfter1: %d", countAfter1) | ||||||
assert.Greater(t, countAfter1, countBefore) | ||||||
|
||||||
time.Sleep(rotationInterval * 5) | ||||||
countAfterSleep := numFilesInDir(t, logPath, false) | ||||||
t.Logf("countAfterSleep: %d", countAfterSleep) | ||||||
assert.Greater(t, countAfterSleep, countAfter1) | ||||||
|
||||||
fl.logf("test 2") | ||||||
countAfter2 := numFilesInDir(t, logPath, false) | ||||||
t.Logf("countAfter2: %d", countAfter2) | ||||||
assert.GreaterOrEqual(t, countAfter2, countAfterSleep) | ||||||
|
||||||
// Wait for Lumberjack to finish its async log compression work | ||||||
// we have no way of waiting for this to finish, or even stopping the millRun() process inside Lumberjack. | ||||||
time.Sleep(time.Millisecond * 500) | ||||||
} | ||||||
|
||||||
// Benchmark the time it takes to write x bytes of data to a logger, and optionally rotate and compress it. | ||||||
func BenchmarkLogRotation(b *testing.B) { | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think this reads just a bit easier to see
rotationTicker.C
since I had to look a back to see if this was a normal ticker.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up writing it this way because
rotationTicker
can be nil if you haven't enabled a rotation interval. The select panics trying to accessC
on a nil ticker.You can however, safely select over a nil channel, and this avoids having to write a separate conditional select if you have log rotation enabled.