-
Notifications
You must be signed in to change notification settings - Fork 578
util-logging: Pick up log file size #177
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
Conversation
Problem FileHandler sets 'bytesWrittenToFile' as 0 in 'openLog()'. This forces log file size to 0 every-time application is restarted, even if it exists. Solution Set 'bytesWrittenToFile' as file size in openStream(), if log file exists. Else set it to 0. Remove 'bytesWrittenToFile' from 'openLog()' ISSUE: twitter#176
Current coverage is 66.52% (diff: 100%)@@ develop #177 diff @@
==========================================
Files 196 196
Lines 8473 8474 +1
Methods 7691 7681 -10
Messages 0 0
Branches 732 743 +11
==========================================
- Hits 5638 5637 -1
- Misses 2835 2837 +2
Partials 0 0
|
| * The test succeeds if the roll over takes place as desired else fails. | ||
| */ | ||
| withTempFolder { | ||
| 1 to 5 foreach{testRun => |
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.
style: (1 to 5).foreach { testRun =>
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.
Noted.
| } | ||
| } | ||
| val files = listLogFiles(folderName+"/LogFileDir") | ||
| files.foreach{f: File => |
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.
style: files.foreach { f: File =>
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.
Will add white spaces as per style and push again.
| formatter = BareFormatter | ||
| ).apply() | ||
|
|
||
| for (a <- 1 to 10000){ |
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.
style: for (a <- 1 to 10000) {
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.
Noted
| val files = listLogFiles(folderName+"/LogFileDir") | ||
| files.foreach{f: File => | ||
| val len = f.length().bytes | ||
| if (len > fileSizeInMegaBytes.megabytes){ |
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.
style: if (len > fileSizeInMegaBytes.megabytes) {
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.
Noted.
| val file = new File(filename) | ||
| val dir = file.getParentFile | ||
| if ((dir ne null) && !dir.exists) dir.mkdirs | ||
| bytesWrittenToFile = if (file.exists()) file.length() else 0 |
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.
It's a bit awkward this is setting bytesWrittenToFile outside an immediate synchronized block which seems like it would be safer. It does appear it's wrapped in a synchronized block by callers though.
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.
The bytesWrittenToFile is set by openStream() which is called inside a synchronized block under openLog(). That preserves it's synchronized lock.
The purpose of removing it from openLog and adding it to openStream is to make sure that as and when the log file opened up or created, the file size is set at that moment.
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.
Yep, I understand both of those points and I still think it's awkward. I'll drop this if nobody else has concerns though.
| * The test succeeds if the roll over takes place as desired else fails. | ||
| */ | ||
| withTempFolder { | ||
| (1 to 5) foreach { testRun => |
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.
style: (1 to 5) foreach => (1 to 5).foreach
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.
Sorry! My bad. Pushing the update.
| val files = listLogFiles(folderName+"/LogFileDir") | ||
| files.foreach { f: File => | ||
| val len = f.length().bytes | ||
| if (len > fileSizeInMegaBytes.megabytes) { |
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.
Is this comparing bytes to megabytes?
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.
No it is not. It is comparing bytes (return type of Storage unit). fileSizeinMegaBytes is a Long type and to convert it into storage type, .megabytes is added. This just multiplies the constant Long and gives the storage size and type in bytes equivalent to megabytes
Making len = f.lenth().megabytes will not set as current size during execution in megabytes but will multiply it with (1024 * 1024), thereby setting the file size as very large which will cause the test to fail and is not the right functionality.
println(s"File size is ${len} and the set limit is ${fileSizeInMegaBytes.megabytes}")
Adding this print statement in test, just before the 'if' clause will tell us that it compares the byte sizes each time.
The output that it generates:
Testing started at 9:07 AM ...
File size is 300000.bytes and the set limit is 1048576.bytes
File size is 600000.bytes and the set limit is 1048576.bytes
File size is 900000.bytes and the set limit is 1048576.bytes
File size is 1048560.bytes and the set limit is 1048576.bytes
File size is 151440.bytes and the set limit is 1048576.bytes
File size is 1048560.bytes and the set limit is 1048576.bytes
File size is 451440.bytes and the set limit is 1048576.bytes
Process finished with exit code 0
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.
Ah, didn't realize this was comparing StorageUnit types at first glance. Thx.
| val fileSizeInMegaBytes: Long = 1 | ||
| val record1 = new javalog.LogRecord(logLevel, "Sending bytes to fill up file") | ||
| val handler = FileHandler( | ||
| filename = folderName+"/LogFileDir/testFileSize.log", |
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: s/folderName+"/LogFileDir/testFileSize.log"/folderName + "/LogFileDir/testFileSize.log"
| */ | ||
| withTempFolder { | ||
| (1 to 5).foreach { testRun => | ||
| s" should succeed to rollover at set limit when test is restarted, execution ${testRun}" in { |
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.
should is implicit here.
s/ should succeed to rollover/rollover
| /** | ||
| * Execute this test individually to affirm that the roll policy based upon the size works | ||
| * even after the test case is executed couple of times. | ||
| * This test mimics the scenario that, if log file exists, FileHandler should pick up it's size |
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 don't love this testing style. how about instead having two Filehandlers in one test?
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.
Well, I initially thought of executing this test individually. I think I forgot to remove the comments.
It'll not be executed in that manner and WILL be executed every-time, just like other tests. This is definitely NOT a test that has to be executed individually, but every-time.
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.
@malikvivek that's not the part of it that bothers me–it's that we execute the test multiple times so that subsequent runs of the test will be affected by previous runs. we should instead have all of our tests be isolated from one another.
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.
Oh! Okay. Got you. I've made the changes and instead of (1 to 5).foreach, using two handlers. I'll make the code more Scalastyle and push changes asap. Thanks
|
Can I get an update on this please? |
|
LGTM, I'll work on getting these merged in. |
|
Thanks a lot! Appreciate it! |
|
This was merged internally, thanks for the PR! |
|
Hi @malikvivek, thank you for your contribution. This has been pushed back out to GitHub in bdc7809 Going to now close this pull request. |
Problem
FileHandler sets 'bytesWrittenToFile' as 0 in 'openLog()'.
This forces log file size to 0 every-time application is restarted, even if it exists.
Solution
Set 'bytesWrittenToFile' as file size in openStream(), if log file exists.
Else set it to 0. Remove 'bytesWrittenToFile' from 'openLog()'
Result
As a result of this merge, the FileHandler will rotate log files with log policy based on Max file size when the limit is reached and prevent the files from growing too large.
ISSUE: #176