forked from maximecharron/statistics-gatherer-plugin
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-51085] Auto-refresh LOGBack config when changed
When the logback XML configuration has changed, reload the in-memory logger transparently. If the reload fails, keep the existing well-known configuration. Change-Id: I99392adc2c9e3666616946d985c88f0a10dc1c09
- Loading branch information
1 parent
3122a79
commit aa2b301
Showing
8 changed files
with
251 additions
and
34 deletions.
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
39 changes: 37 additions & 2 deletions
39
src/main/java/org/jenkins/plugins/statistics/gatherer/util/LogbackFactory.java
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 |
---|---|---|
@@ -1,9 +1,44 @@ | ||
package org.jenkins.plugins.statistics.gatherer.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.*; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class LogbackFactory { | ||
private static final Logger logger = Logger.getLogger(LogbackFactory.class.getName()); | ||
private static final int REFRESH_THREADS = 1; | ||
private static final int REFRESH_INTERVAL_SECS = 60; | ||
private static final ScheduledExecutorService refresher = Executors.newScheduledThreadPool(REFRESH_THREADS); | ||
private static final Set<Logback> activeLogbacks = new HashSet<>(); | ||
private static ScheduledFuture<?> refresherTask; | ||
|
||
public static Logback create(String loggerName) throws Exception { | ||
public static synchronized Logback create(String loggerName) throws Exception { | ||
Class<Logback> logback = (Class<Logback>) Class.forName(Logback.class.getName() + "Impl"); | ||
return logback.newInstance().setLoggerName(loggerName); | ||
Logback logbackInstance = logback.newInstance().setLoggerName(loggerName); | ||
|
||
activeLogbacks.add(logbackInstance); | ||
|
||
if(refresherTask != null && !refresherTask.isCancelled()) { | ||
refresherTask.cancel(true); | ||
} | ||
|
||
refresherTask = refresher.scheduleWithFixedDelay(new Runnable() { | ||
@Override | ||
public void run() { | ||
for (Logback logback: activeLogbacks) { | ||
try { | ||
logback.refresh(); | ||
} catch (Exception e) { | ||
logger.log(Level.WARNING, "Unable to refresh LOGBack " + logback, e); | ||
} | ||
} | ||
} | ||
}, REFRESH_INTERVAL_SECS, REFRESH_INTERVAL_SECS, TimeUnit.SECONDS); | ||
|
||
return logbackInstance; | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/main/java/org/jenkins/plugins/statistics/gatherer/util/URLSha.java
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.jenkins.plugins.statistics.gatherer.util; | ||
|
||
import org.apache.commons.codec.digest.DigestUtils; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.util.Objects; | ||
|
||
public class URLSha { | ||
private String value; | ||
|
||
public URLSha(URL configurationUrl) throws IOException { | ||
try (InputStream is = configurationUrl.openStream()) { | ||
value = DigestUtils.sha1Hex(is); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return (obj instanceof URLSha) ? Objects.equals(this.value, ((URLSha) obj).value) : false; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "{sha1}" + value; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/java/org/jenkins/plugins/statistics/gatherer/util/LogbackAbstractTest.java
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.jenkins.plugins.statistics.gatherer.util; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class LogbackAbstractTest implements LogbackFixture { | ||
|
||
public String newSimpleLogbackXmlUrl() throws IOException { | ||
Path logbackXml = newSimpleLogbackXmlPath(); | ||
return logbackXml.toUri().toURL().toString(); | ||
} | ||
|
||
public Path newSimpleLogbackXmlPath() throws IOException { | ||
Path logbackXml = Files.createTempFile(LogbackImplTest.class.getName(), ".xml"); | ||
Files.write(logbackXml, SIMPLE_LOGBACK_XML.getBytes()); | ||
return logbackXml; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/test/java/org/jenkins/plugins/statistics/gatherer/util/LogbackFactoryTest.java
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.jenkins.plugins.statistics.gatherer.util; | ||
|
||
import jenkins.model.Jenkins; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.hamcrest.core.IsNot.not; | ||
import static org.powermock.api.mockito.PowerMockito.mockStatic; | ||
import static org.powermock.api.mockito.PowerMockito.when; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({PropertyLoader.class}) | ||
public class LogbackFactoryTest extends LogbackAbstractTest { | ||
Path logbackXmlPath; | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
mockStatic(PropertyLoader.class); | ||
logbackXmlPath = newSimpleLogbackXmlPath(); | ||
Files.write(logbackXmlPath, SIMPLE_LOGBACK_XML.getBytes()); | ||
when(PropertyLoader.getLogbackConfigXmlUrl()).thenReturn(logbackXmlPath.toUri().toURL().toString()); | ||
when(PropertyLoader.getShouldSendToLogback()).thenReturn(true); | ||
} | ||
|
||
@Test | ||
public void givenFactory_whenCreating_thenNewLogbackIsCreated() throws Exception { | ||
Logback logback = LogbackFactory.create("fooLogger"); | ||
|
||
assertThat(logback, is(notNullValue())); | ||
assertThat(logback.getLoggerName(), is("fooLogger")); | ||
} | ||
|
||
@Test | ||
public void givenFactory_whenChangingLogbackXML_thenShouldReload() throws Exception { | ||
Logback logback = LogbackFactory.create("foo"); | ||
URLSha initialSha = logback.getLastSha(); | ||
|
||
Files.write(logbackXmlPath, (SIMPLE_LOGBACK_XML + "\n\t").getBytes()); | ||
logback.refresh(); | ||
|
||
assertThat(logback.getLastSha(), is(not(equalTo(initialSha)))); | ||
} | ||
|
||
@Test | ||
public void givenFactory_whenRefreshing_thenShouldNotReload() throws Exception { | ||
Logback logback = LogbackFactory.create("foo"); | ||
URLSha initialSha = logback.getLastSha(); | ||
|
||
logback.refresh(); | ||
|
||
assertThat(logback.getLastSha(), is(equalTo(initialSha))); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/java/org/jenkins/plugins/statistics/gatherer/util/LogbackFixture.java
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.jenkins.plugins.statistics.gatherer.util; | ||
|
||
public interface LogbackFixture { | ||
|
||
String SIMPLE_LOGBACK_XML = "<configuration>\n" + | ||
"\n" + | ||
" <appender name=\"STDOUT\" class=\"ch.qos.logback.core.ConsoleAppender\">\n" + | ||
" <!-- encoders are assigned the type\n" + | ||
" ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->\n" + | ||
" <encoder>\n" + | ||
" <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>\n" + | ||
" </encoder>\n" + | ||
" </appender>\n" + | ||
"\n" + | ||
" <root level=\"debug\">\n" + | ||
" <appender-ref ref=\"STDOUT\" />\n" + | ||
" </root>\n" + | ||
"</configuration>"; | ||
} |
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