-
Notifications
You must be signed in to change notification settings - Fork 73
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
Adding rolled custom logs in support bundle #627
Open
nevingeorgesunny
wants to merge
15
commits into
jenkinsci:master
Choose a base branch
from
nevingeorgesunny:adding-rolled-custom-logs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e0d3aba
adding rolled logs in test
nevingeorgesunny 5b042d8
adding rolled logs in test
nevingeorgesunny 602a7ee
code optimisations
nevingeorgesunny c369fea
optimising code
nevingeorgesunny 5f882c8
optimising logs
nevingeorgesunny 4aaf2cb
optimising code
nevingeorgesunny 64aefaf
adding better test and optimizing log messages
nevingeorgesunny 19e042f
Merge branch 'master' into adding-rolled-custom-logs
jglick d3c0c2b
Merge branch 'master' into adding-rolled-custom-logs
nevingeorgesunny b3c39fd
Apply suggestions from code review
nevingeorgesunny 12b6d22
fixing test issues
nevingeorgesunny 2dfb49c
adding cleanup
nevingeorgesunny fd67636
Merge branch 'master' of https://github.com/jenkinsci/support-core-pl…
jglick d32cd38
empty commit to re trigger build
nevingeorgesunny 63578f3
empty commit to re trigger build
nevingeorgesunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ | |
|
||
private static final Logger LOGGER = Logger.getLogger(CustomLogs.class.getName()); | ||
private static final int MAX_ROTATE_LOGS = Integer.getInteger(CustomLogs.class.getName() + ".MAX_ROTATE_LOGS", 9); | ||
private static final File customLogs = new File(SafeTimerTask.getLogsRoot(), "custom"); | ||
private final File customLogs = new File(SafeTimerTask.getLogsRoot(), "custom"); | ||
private final List<LogRecorder> logRecorders = Jenkins.get().getLog().getRecorders(); | ||
|
||
@NonNull | ||
|
@@ -95,13 +95,38 @@ | |
} | ||
}); | ||
} | ||
|
||
// Add rotated log files | ||
File[] rotatedLogFiles = | ||
customLogs.listFiles((dir, filename) -> filename.matches(name + "[.]log[.][0-9]+")); | ||
if (rotatedLogFiles == null) { | ||
LOGGER.fine("No rotated logs found for : " + name); | ||
return; | ||
} | ||
|
||
for (File rotatedLogFile : rotatedLogFiles) { | ||
String rotatedEntryName = "nodes/master/logs/custom/{0}.log."; | ||
|
||
try { | ||
// Fetch the rotations number of the log | ||
// eg. custom.log.2 , the rotation number is 2 | ||
String[] logNameParts = rotatedLogFile.getName().split("\\."); | ||
String logRotationNumber = logNameParts[logNameParts.length - 1]; | ||
|
||
result.add( | ||
new FileContent(rotatedEntryName + logRotationNumber, new String[] {name}, rotatedLogFile)); | ||
} catch (Exception e) { | ||
LOGGER.log(Level.WARNING, "Error while adding rotated log files for " + name, e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static final class LogFile { | ||
private final RewindableRotatingFileOutputStream stream; | ||
private final Handler handler; | ||
private int count; | ||
private final File customLogs = new File(SafeTimerTask.getLogsRoot(), "custom"); | ||
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. Or pass it in the constructor. |
||
|
||
@SuppressFBWarnings( | ||
value = "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ | |||||
package com.cloudbees.jenkins.support.impl; | ||||||
|
||||||
import static org.hamcrest.MatcherAssert.assertThat; | ||||||
import static org.junit.Assert.assertEquals; | ||||||
import static org.junit.Assert.assertFalse; | ||||||
import static org.junit.Assert.assertTrue; | ||||||
|
||||||
|
@@ -14,10 +15,17 @@ | |||||
import hudson.ExtensionList; | ||||||
import hudson.logging.LogRecorder; | ||||||
import hudson.security.Permission; | ||||||
import hudson.triggers.SafeTimerTask; | ||||||
import java.io.File; | ||||||
import java.io.IOException; | ||||||
import java.nio.file.Files; | ||||||
import java.nio.file.Paths; | ||||||
import java.nio.file.StandardOpenOption; | ||||||
import java.util.Collections; | ||||||
import java.util.Map; | ||||||
import java.util.Objects; | ||||||
import java.util.Set; | ||||||
import java.util.TreeMap; | ||||||
import java.util.logging.Level; | ||||||
import java.util.logging.Logger; | ||||||
import jenkins.model.Jenkins; | ||||||
|
@@ -70,4 +78,73 @@ public void addContents(@NonNull Container container) { | |||||
assertFalse("Should write CustomLogsTest FINE logs", customLogs.isEmpty()); | ||||||
assertThat(customLogs, Matchers.containsString("Testing custom log recorders")); | ||||||
} | ||||||
|
||||||
@Test | ||||||
public void testCustomLogRotation() throws IOException { | ||||||
LogRecorder test1LogRecorder = new LogRecorder("test1"); | ||||||
j.getInstance().getLog().getRecorders().add(test1LogRecorder); | ||||||
|
||||||
LogRecorder test2LogRecorder = new LogRecorder("second.test2"); | ||||||
j.getInstance().getLog().getRecorders().add(test2LogRecorder); | ||||||
|
||||||
LogRecorder nonRotatedCustomLog = new LogRecorder("nonRotatedCustomLog"); | ||||||
j.getInstance().getLog().getRecorders().add(nonRotatedCustomLog); | ||||||
|
||||||
// Create dummy log files | ||||||
File customLogsDir = new File(SafeTimerTask.getLogsRoot(), "custom"); | ||||||
customLogsDir.mkdirs(); | ||||||
|
||||||
// Create dummy log for test1 log recorder | ||||||
Files.write(Paths.get(customLogsDir.getPath(), "test1.log"), "test1 one".getBytes(), StandardOpenOption.CREATE); | ||||||
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. just
Suggested change
suffices (ditto elsewhere) |
||||||
Files.write( | ||||||
Paths.get(customLogsDir.getPath(), "test1.log.1"), "test1 two".getBytes(), StandardOpenOption.CREATE); | ||||||
Files.write( | ||||||
Paths.get(customLogsDir.getPath(), "test1.log.2"), "test1 three".getBytes(), StandardOpenOption.CREATE); | ||||||
|
||||||
// Create dummy log for second.test2 log recorder | ||||||
Files.write( | ||||||
Paths.get(customLogsDir.getPath(), "second.test2.log"), | ||||||
"second.test2 one".getBytes(), | ||||||
StandardOpenOption.CREATE); | ||||||
Files.write( | ||||||
Paths.get(customLogsDir.getPath(), "second.test2.log.1"), | ||||||
"second.test2 two".getBytes(), | ||||||
StandardOpenOption.CREATE); | ||||||
Files.write( | ||||||
Paths.get(customLogsDir.getPath(), "second.test2.log.2"), | ||||||
"second.test2 three".getBytes(), | ||||||
StandardOpenOption.CREATE); | ||||||
|
||||||
// Create dummy log for nonRotatedCustomLog | ||||||
Files.write( | ||||||
Paths.get(customLogsDir.getPath(), "nonRotatedCustomLog.log"), | ||||||
"nonRotatedCustomLog one".getBytes(), | ||||||
StandardOpenOption.CREATE); | ||||||
|
||||||
// Invoke the component and get the result map | ||||||
Map<String, String> resultMap = SupportTestUtils.invokeComponentToMap( | ||||||
Objects.requireNonNull(ExtensionList.lookup(Component.class).get(CustomLogs.class))); | ||||||
|
||||||
// Create the expected map | ||||||
Map<String, String> expectedMap = new TreeMap<>(); | ||||||
expectedMap.put("nodes/master/logs/custom/test1.log", "test1 one"); | ||||||
expectedMap.put("nodes/master/logs/custom/test1.log.1", "test1 two"); | ||||||
expectedMap.put("nodes/master/logs/custom/test1.log.2", "test1 three"); | ||||||
expectedMap.put("nodes/master/logs/custom/second.test2.log", "second.test2 one"); | ||||||
expectedMap.put("nodes/master/logs/custom/second.test2.log.1", "second.test2 two"); | ||||||
expectedMap.put("nodes/master/logs/custom/second.test2.log.2", "second.test2 three"); | ||||||
expectedMap.put("nodes/master/logs/custom/nonRotatedCustomLog.log", "nonRotatedCustomLog one"); | ||||||
|
||||||
// Assert the result map | ||||||
assertEquals(expectedMap, resultMap); | ||||||
|
||||||
// Cleanup all the dummy files | ||||||
Files.deleteIfExists(Paths.get(customLogsDir.getPath(), "test1.log")); | ||||||
Files.deleteIfExists(Paths.get(customLogsDir.getPath(), "test1.log.1")); | ||||||
Files.deleteIfExists(Paths.get(customLogsDir.getPath(), "test1.log.2")); | ||||||
Files.deleteIfExists(Paths.get(customLogsDir.getPath(), "second.test2.log")); | ||||||
Files.deleteIfExists(Paths.get(customLogsDir.getPath(), "second.test2.log.1")); | ||||||
Files.deleteIfExists(Paths.get(customLogsDir.getPath(), "second.test2.log.2")); | ||||||
Files.deleteIfExists(Paths.get(customLogsDir.getPath(), "nonRotatedCustomLog.log")); | ||||||
} | ||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 removed static as when I was running a test class,
[CustomLogsTest.java]
, it has 3 test in it for some reasonSafeTimerTask.getLogsRoot()
was same for all test , due to which test were failing ,removing static fixed this , i think its similar to #617 (comment)
[edited, please paste raw discussion permalinks]