Skip to content

Commit

Permalink
[WFMP-232] Remove the Log API in favor of jboss-logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
jamezp committed Dec 14, 2023
1 parent ca346f2 commit 11363e9
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.jboss.dmr.ModelNode;
import org.jboss.dmr.ModelType;
import org.jboss.dmr.Property;
import org.jboss.logging.Logger;

/**
* Generates a new {@code logging.properties} file based on the logging subsystem model.
Expand All @@ -54,9 +55,9 @@
*
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
// @TODO, we can't use AbstractLogEnabled, it is not in the maven plugin classloader.
public class BootLoggingConfiguration { // extends AbstractLogEnabled {
public class BootLoggingConfiguration {

private static final Logger LOGGER = Logger.getLogger(BootLoggingConfiguration.class);
private static final Pattern SIZE_PATTERN = Pattern.compile("(\\d+)([kKmMgGbBtT])?");
private static final String NEW_LINE = System.lineSeparator();

Expand Down Expand Up @@ -164,11 +165,8 @@ public void generate(final Path configDir, final ModelControllerClient client) t
if (properties.containsKey(key)) {
requiredProperties.put(key, properties.get(key));
} else {
// @TODO, we can't use AbstractLogEnabled, it is not in the maven plugin classloader.
// getLogger().warn(String.format("The value for the expression \"%s\" could not be resolved " +
// "and may not be set at boot if no default value is available.", entry.getValue()));
System.err.println(String.format("The value for the expression \"%s\" could not be resolved "
+ "and may not be set at boot if no default value is available.", entry.getValue()));
LOGGER.warnf("The value for the expression \"%s\" could not be resolved "
+ "and may not be set at boot if no default value is available.", entry.getValue());
}
iter.remove();
}
Expand Down Expand Up @@ -829,12 +827,9 @@ private void resolveRelativeTo(final ModelNode pathModel, final String relativeT
for (Expression expression : expressions) {
for (String key : expression.getKeys()) {
if (!properties.containsKey(key)) {
// @TODO, we can't use AbstractLogEnabled, it is not in the maven plugin classloader.
// getLogger().warn(String.format("The path %s is an undefined property. If not set at boot time
// unexpected results may occur.", pathEntry.asString()));
System.err.println(String.format(
LOGGER.warnf(
"The path %s is an undefined property. If not set at boot time unexpected results may occur.",
pathEntry.asString()));
pathEntry.asString());
} else {
// We use the property name and value directly rather than referencing the path
usedProperties.put(key, properties.get(key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.jboss.galleon.universe.maven.repo.MavenRepoManager;
import org.jboss.galleon.util.IoUtils;
import org.jboss.galleon.util.ZipUtils;
import org.jboss.logging.Logger;
import org.wildfly.plugins.core.cli.CLIForkedBootConfigGenerator;
import org.wildfly.plugins.core.cli.ForkedCLIUtil;

Expand All @@ -50,7 +51,7 @@ public class BootableJarSupport {
*/
public static void packageBootableJar(Path targetJarFile, Path target,
GalleonProvisioningConfig config, Path serverHome, MavenRepoManager resolver,
MessageWriter writer, Log log) throws Exception {
MessageWriter writer, Logger log) throws Exception {
Path contentRootDir = target.resolve("bootable-jar-build-artifacts");
if (Files.exists(contentRootDir)) {
IoUtils.recursiveDelete(contentRootDir);
Expand Down Expand Up @@ -109,7 +110,8 @@ private static void cleanupServer(Path jbossHome) throws IOException {
Files.deleteIfExists(jbossHome.resolve("README.txt"));
}

public static ScannedArtifacts scanArtifacts(Provisioning pm, GalleonProvisioningConfig config, Log log) throws Exception {
public static ScannedArtifacts scanArtifacts(Provisioning pm, GalleonProvisioningConfig config, Logger log)
throws Exception {
Set<MavenArtifact> cliArtifacts = new HashSet<>();
MavenArtifact jbossModules = null;
MavenArtifact bootArtifact = null;
Expand Down
21 changes: 0 additions & 21 deletions core/src/main/java/org/wildfly/plugins/core/bootablejar/Log.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import org.jboss.galleon.BaseErrors;
import org.jboss.galleon.ProvisioningException;
import org.wildfly.plugins.core.bootablejar.Log;
import org.jboss.logging.Logger;

/**
*
Expand All @@ -40,7 +40,7 @@ private static String getJavaCmd() {
return javaCmd == null ? javaCmd = Paths.get(getJavaHome()).resolve("bin").resolve("java").toString() : javaCmd;
}

public static void fork(Log log, String[] artifacts, Class<?> clazz, Path home, Path output, String... args)
public static void fork(Logger log, String[] artifacts, Class<?> clazz, Path home, Path output, String... args)
throws Exception {
// prepare the classpath
final StringBuilder cp = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.apache.maven.settings.crypto.SettingsDecryptionResult;
import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.ModelControllerClientConfiguration;
import org.wildfly.security.manager.WildFlySecurityManager;

/**
* The default implementation for connecting to a running WildFly instance
Expand All @@ -39,8 +38,11 @@ public abstract class AbstractServerConnection extends AbstractMojo {
public static final String DEBUG_MESSAGE_SETTINGS_HAS_ID = "Found the server's id in the settings.xml file";

static {
// Ensure jboss-logging binds to slf4j
WildFlySecurityManager.setPropertyPrivileged("org.jboss.logging.provider", "slf4j");
// This is odd, but if not set we should set the JBoss Logging provider to slf4j as that is what Maven uses
final String provider = System.getProperty("org.jboss.logging.provider");
if (provider == null || provider.isBlank()) {
System.setProperty("org.jboss.logging.provider", "slf4j");
}
}

/**
Expand Down
100 changes: 100 additions & 0 deletions plugin/src/main/java/org/wildfly/plugin/common/MavenJBossLogger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.wildfly.plugin.common;

import java.text.MessageFormat;

import org.apache.maven.plugin.logging.Log;
import org.jboss.logging.Logger;

/**
* A logger which delegates to a {@link Log}.
* <p>
* For {@link #isEnabled(Level)} {@link org.jboss.logging.Logger.Level#TRACE} is ignored and
* {@link org.jboss.logging.Logger.Level#FATAL}
* is treated as an {@link Log#error(CharSequence, Throwable) error}.
* </p>
* <p>
* For the log methods, {@link org.jboss.logging.Logger.Level#TRACE} is treated as a {@link Log#debug(CharSequence, Throwable)
* debug}
* and {@link org.jboss.logging.Logger.Level#FATAL} is treated as {@link Log#error(CharSequence, Throwable) error}.
* </p>
*
* @author <a href="mailto:[email protected]">James R. Perkins</a>
*/
public class MavenJBossLogger extends Logger {
private final Log mavenLogger;

public MavenJBossLogger(final Log mavenLogger) {
super(mavenLogger.toString());
this.mavenLogger = mavenLogger;
}

@Override
protected void doLog(final Level level, final String loggerClassName, final Object message, final Object[] parameters,
final Throwable thrown) {
final String msg = parameters == null ? String.valueOf(message)
: MessageFormat.format(String.valueOf(message), parameters);
doMavenLog(level, msg, thrown);
}

@Override
protected void doLogf(final Level level, final String loggerClassName, final String format, final Object[] parameters,
final Throwable thrown) {
final String msg = String.format(format, parameters);
doMavenLog(level, msg, thrown);
}

@Override
public boolean isEnabled(final Level level) {
switch (level) {
case DEBUG:
return mavenLogger.isDebugEnabled();
case INFO:
return mavenLogger.isInfoEnabled();
case WARN:
return mavenLogger.isWarnEnabled();
case FATAL:
case ERROR:
return mavenLogger.isErrorEnabled();
}
return false;
}

private void doMavenLog(final Level level, final String msg, final Throwable thrown) {
switch (level) {
case TRACE:
case DEBUG:
if (thrown == null) {
mavenLogger.debug(msg);
} else {
mavenLogger.debug(msg, thrown);
}
break;
case WARN:
if (thrown == null) {
mavenLogger.warn(msg);
} else {
mavenLogger.warn(msg, thrown);
}
break;
case FATAL:
case ERROR:
if (thrown == null) {
mavenLogger.error(msg);
} else {
mavenLogger.error(msg, thrown);
}
break;
default:
if (thrown == null) {
mavenLogger.info(msg);
} else {
mavenLogger.info(msg, thrown);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@
* @author jfdenise
*/
abstract class AbstractProvisionServerMojo extends AbstractMojo {
static {
// This is odd, but if not set we should set the JBoss Logging provider to slf4j as that is what Maven uses
final String provider = System.getProperty("org.jboss.logging.provider");
if (provider == null || provider.isBlank()) {
System.setProperty("org.jboss.logging.provider", "slf4j");
}
}

@Component
RepositorySystem repoSystem;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@
import org.wildfly.plugin.cli.BaseCommandConfiguration;
import org.wildfly.plugin.cli.CliSession;
import org.wildfly.plugin.cli.OfflineCommandExecutor;
import org.wildfly.plugin.common.MavenJBossLogger;
import org.wildfly.plugin.common.PropertyNames;
import org.wildfly.plugin.common.StandardOutput;
import org.wildfly.plugin.common.Utils;
import org.wildfly.plugin.deployment.MojoDeploymentException;
import org.wildfly.plugin.deployment.PackageType;
import org.wildfly.plugins.core.bootablejar.BootableJarSupport;
import org.wildfly.plugins.core.bootablejar.Log;

/**
* Provision a server, copy extra content and deploy primary artifact if it
Expand Down Expand Up @@ -381,29 +381,7 @@ private void packageBootableJar(Path jbossHome, GalleonProvisioningConfig active
BootableJarSupport.packageBootableJar(targetJarFile, targetPath,
activeConfig, jbossHome,
artifactResolver,
new MvnMessageWriter(getLog()), new Log() {
@Override
public void warn(String string) {
getLog().warn(string);
}

@Override
public void debug(String string) {
if (getLog().isDebugEnabled()) {
getLog().debug(string);
}
}

@Override
public void error(String string) {
getLog().error(string);
}

@Override
public void info(String string) {
getLog().info(string);
}
});
new MvnMessageWriter(getLog()), new MavenJBossLogger(getLog()));
attachJar(targetJarFile);
getLog().info("Bootable JAR packaging DONE. To run the server: java -jar " + targetJarFile);

Expand Down

0 comments on commit 11363e9

Please sign in to comment.