Skip to content
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

Add log discriminator to separate log file when modernized plugin #29

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import ch.qos.logback.core.spi.FilterReply;
import io.jenkins.tools.pluginmodernizer.core.config.Config;

public class ApplicationLogFilter extends ThresholdFilter {
public class ConsoleLogFilter extends ThresholdFilter {

@Override
public FilterReply decide(ILoggingEvent event) {
if (event.getMarkerList() != null && !event.getMarkerList().isEmpty()) {
return FilterReply.DENY;
}
if (Config.DEBUG) {
setLevel(Level.DEBUG.levelStr);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.jenkins.tools.pluginmodernizer.cli;

import java.util.List;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.sift.AbstractDiscriminator;
import org.slf4j.Marker;

public class PluginLoggerDiscriminator extends AbstractDiscriminator<ILoggingEvent> {

@Override
public String getDiscriminatingValue(ILoggingEvent iLoggingEvent) {
List<Marker> markers = iLoggingEvent.getMarkerList();
if (markers == null || markers.isEmpty()) {
return "modernizer";
}
final Marker marker = markers.get(0);
return marker.getName();
}

@Override
public String getKey() {
return "filename";
}

}
36 changes: 21 additions & 15 deletions plugin-modernizer-cli/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,36 @@
<resetJUL>true</resetJUL>
</contextListener>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="io.jenkins.tools.pluginmodernizer.cli.ApplicationLogFilter" >
<filter class="io.jenkins.tools.pluginmodernizer.cli.ConsoleLogFilter" >
<level>INFO</level>
</filter>
<encoder>
<pattern>%msg %n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z', 'UTC'} [%level] [Thread=%t] - %logger{36} # %msg %n</pattern>
</encoder>
<file>logs/modernizer.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/modernizer-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>5MB</maxFileSize>
</rollingPolicy>
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator class="io.jenkins.tools.pluginmodernizer.cli.PluginLoggerDiscriminator"></discriminator>
<sift>
<appender name="FILE-${filename}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z', 'UTC'} [%level] [Thread=%t] - %logger{36} # %msg %n</pattern>
</encoder>
<file>logs/${filename}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>logs/${filename}-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>5MB</maxFileSize>
</rollingPolicy>
</appender>
</sift>
</appender>
<root>
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
<appender-ref ref="SIFT" />
</root>
<logger name="io.jenkins.tools.pluginmodernizer" level="TRACE" />
<logger name="java.lang.ProcessBuilder" level="WARN" />
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.maven.shared.invoker.MavenInvocationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MarkerFactory;

@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "false positive")
public class MavenInvoker {
Expand All @@ -35,20 +36,20 @@ public MavenInvoker(Config config) {
this.config = config;
}

public void invokeGoal(String pluginPath, String goal) {
public void invokeGoal(String plugin, String pluginPath, String goal) {
List<String> goals = new ArrayList<>();
goals.add(goal);
invokeGoals(pluginPath, goals);
invokeGoals(plugin, pluginPath, goals);
}

public void invokeRewrite(String pluginPath) {
public void invokeRewrite(String plugin, String pluginPath) {
try {
List<String> goals = createGoalsList();
if (goals == null) {
LOG.info("No active recipes.");
LOG.info(MarkerFactory.getMarker(plugin), "No active recipes.");
return;
}
invokeGoals(pluginPath, goals);
invokeGoals(plugin, pluginPath, goals);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
Expand Down Expand Up @@ -102,20 +103,27 @@ private List<String> getRecipeArtifactCoordinates(List<String> recipes, JsonNode
return recipeArtifactCoordinates;
}

private void invokeGoals(String pluginPath, List<String> goals) {
private void invokeGoals(String plugin, String pluginPath, List<String> goals) {
if (!validateMavenHome()) {
return;
}

Invoker invoker = new DefaultInvoker();
invoker.setMavenHome(new File(config.getMavenHome()));

try {
InvocationRequest request = createInvocationRequest(pluginPath, goals);
request.setBatchMode(true);
request.setNoTransferProgress(false);
request.setErrorHandler((message) -> {
LOG.error(MarkerFactory.getMarker(plugin), String.format("Something went wrong when running maven: %s", message));
});
request.setOutputHandler((message) -> {
LOG.info(MarkerFactory.getMarker(plugin), message);
});
InvocationResult result = invoker.execute(request);
handleInvocationResult(result);
handleInvocationResult(plugin, result);
} catch (MavenInvocationException e) {
LOG.error("Maven invocation failed: ", e);
LOG.error(MarkerFactory.getMarker(plugin), "Maven invocation failed: ", e);
}
}

Expand Down Expand Up @@ -147,14 +155,14 @@ private InvocationRequest createInvocationRequest(String pluginPath, List<String
return request;
}

private void handleInvocationResult(InvocationResult result) {
private void handleInvocationResult(String plugin, InvocationResult result) {
if (result.getExitCode() != 0) {
LOG.error("Build failed.");
LOG.error(MarkerFactory.getMarker(plugin), "Build fail with code: {}", result.getExitCode());
if (result.getExecutionException() != null) {
LOG.error("Execution exception occurred: ", result.getExecutionException());
LOG.error(MarkerFactory.getMarker(plugin), "Execution exception occurred: ", result.getExecutionException());
}
} else {
LOG.info("Build succeeded.");
LOG.info(MarkerFactory.getMarker(plugin), "Build success!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ public PluginModernizer(Config config) {

public void start() {
String projectRoot = System.getProperty("user.dir");
LOG.info("Plugins: {}", config.getPlugins());
LOG.info("Recipes: {}", config.getRecipes());
LOG.debug("Cache Path: {}", config.getCachePath());
for (String plugin : config.getPlugins()) {
String pluginPath = projectRoot + "/test-plugins/" + plugin;
mavenInvoker.invokeGoal(pluginPath, "clean");
mavenInvoker.invokeRewrite(pluginPath);
LOG.info("Invoking clean phase for plugin: {}", plugin);
mavenInvoker.invokeGoal(plugin, pluginPath, "clean");
LOG.info("Invoking rewrite plugin for plugin: {}", plugin);
mavenInvoker.invokeRewrite(plugin, pluginPath);
}
}

Expand Down