Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@malikvivek
Copy link

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

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
@codecov-io
Copy link

codecov-io commented Oct 5, 2016

Current coverage is 66.52% (diff: 100%)

Merging #177 into develop will decrease coverage by 0.01%

@@            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          

Powered by Codecov. Last update e684872...0dc9b89

* The test succeeds if the roll over takes place as desired else fails.
*/
withTempFolder {
1 to 5 foreach{testRun =>

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 =>

Copy link
Author

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 =>

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 =>

Copy link
Author

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){

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) {

Copy link
Author

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){

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) {

Copy link
Author

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

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.

Copy link
Author

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.

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 =>

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

Copy link
Author

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) {

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?

Copy link
Author

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

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",
Copy link
Contributor

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 {
Copy link
Contributor

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
Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Contributor

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.

Copy link
Author

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

@malikvivek
Copy link
Author

Can I get an update on this please?

@mosesn mosesn added the Ship It label Oct 12, 2016
@mosesn
Copy link
Contributor

mosesn commented Oct 12, 2016

LGTM, I'll work on getting these merged in.

@malikvivek
Copy link
Author

Thanks a lot! Appreciate it!

@mosesn
Copy link
Contributor

mosesn commented Oct 13, 2016

This was merged internally, thanks for the PR!

@ryanoneill
Copy link
Contributor

Hi @malikvivek, thank you for your contribution. This has been pushed back out to GitHub in bdc7809

Going to now close this pull request.

@ryanoneill ryanoneill closed this Oct 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

5 participants