Skip to content

Commit

Permalink
Merge pull request #200 from jonesbusy/feature/cleanup-logging
Browse files Browse the repository at this point in the history
Add shutdown hook and cleanup necessary log message in non-debug mode
  • Loading branch information
jonesbusy authored Aug 20, 2024
2 parents 86c3da7 + 86faee3 commit a0da88a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ public class Main implements Runnable {

static {
System.setProperty("slf4j.internal.verbosity", "WARN");
System.setProperty("jdk.httpclient.HttpClient.log", "errors,requests");
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}

private static Logger LOG = LoggerFactory.getLogger(Main.class);

public static void main(final String[] args) {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
LOG.info("Plugin Modernizer aborted.");
}
});
new CommandLine(new Main()).setOptionsCaseInsensitive(true).execute(args);
}

Expand Down
1 change: 1 addition & 0 deletions plugin-modernizer-cli/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<logger name="io.jenkins.tools.pluginmodernizer" level="TRACE" />
<logger name="jdk.event.security" level="INFO" />
<logger name="java.lang.ProcessBuilder" level="WARN" />
<logger name="java.lang.Shutdown" level="WARN" />
<logger name="org.eclipse.jgit.internal.storage.file" level="INFO" />
<logger name="org.eclipse.jgit.internal.util" level="INFO" />
<logger name="org.eclipse.jgit.transport" level="INFO" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import io.jenkins.tools.pluginmodernizer.core.model.ModernizerException;
import io.jenkins.tools.pluginmodernizer.core.utils.JsonUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.Clock;
import java.time.Duration;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -105,7 +103,8 @@ public <T extends CacheEntry<T>> T get(Path path, String cacheKey, Class<T> claz
return null;
}
}
return JsonUtils.fromJson(FileUtils.readFileToString(cachedPath.toFile(), StandardCharsets.UTF_8), clazz);
LOG.debug("Cache entry found for cache {} at path {} and key {}", location, path, cacheKey);
return JsonUtils.fromJson(cachedPath, clazz);
} catch (IOException e) {
LOG.debug("Cache entry not found for cache {} at path {} and key {}", location, path, cacheKey);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public final String getKey() {
* @return The path
*/
public final Path getPath() {
if (path == null) {
return null;
}
return Path.of(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonUtils {

private static final Logger LOG = LoggerFactory.getLogger(JsonUtils.class);

private static final Gson gson;

private JsonUtils() {
Expand All @@ -35,6 +39,7 @@ public static String toJson(Object object) {
*/
public static void toJsonFile(Object object, Path path) {
try {
LOG.trace("Writing JSON file to {}", path);
FileUtils.writeStringToFile(path.toFile(), gson.toJson(object), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new ModernizerException("Unable to write JSON file due to IO error", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,12 @@ public static UpdateCenterData download(Config config) {
.GET()
.uri(config.getJenkinsUpdateCenter().toURI())
.build();
LOG.debug("Fetching update center data from: {}", config.getJenkinsUpdateCenter());
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() != 200) {
throw new ModernizerException("Failed to fetch update center data: " + response.statusCode());
}
LOG.debug("Fetched update center data from: {}", config.getJenkinsUpdateCenter());
return JsonUtils.fromJson(response.body(), UpdateCenterData.class);
} catch (IOException | JsonSyntaxException | URISyntaxException | InterruptedException e) {
throw new ModernizerException("Unable to fetch update center data", e);
Expand Down

0 comments on commit a0da88a

Please sign in to comment.