From 54708a3c474f51dff7ce2a7f100a79061ae4bac0 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Tue, 23 Feb 2016 08:51:05 -0800 Subject: [PATCH 1/2] Convert logging to JDK logging --- pom.xml | 6 ++ resolver-integration/pom.xml | 1 - resolver/pom.xml | 12 +--- .../io/airlift/resolver/ArtifactResolver.java | 4 +- .../internal/ConsoleRepositoryListener.java | 66 ++++++++++--------- .../internal/ConsoleTransferListener.java | 24 ++----- .../{Slf4jLogger.java => JdkLogger.java} | 66 ++++++++++++------- ...ggerManager.java => JdkLoggerManager.java} | 36 +++++----- 8 files changed, 113 insertions(+), 102 deletions(-) rename resolver/src/main/java/io/airlift/resolver/internal/{Slf4jLogger.java => JdkLogger.java} (63%) rename resolver/src/main/java/io/airlift/resolver/internal/{Slf4jLoggerManager.java => JdkLoggerManager.java} (80%) diff --git a/pom.xml b/pom.xml index 2d56f23..a69eabb 100644 --- a/pom.xml +++ b/pom.xml @@ -165,6 +165,12 @@ plexus-classworlds 2.4 + + + io.airlift + log + 0.124 + diff --git a/resolver-integration/pom.xml b/resolver-integration/pom.xml index bf6b98a..a8281d3 100644 --- a/resolver-integration/pom.xml +++ b/resolver-integration/pom.xml @@ -7,7 +7,6 @@ 1.4-SNAPSHOT - io.airlift.resolver resolver-integration jar diff --git a/resolver/pom.xml b/resolver/pom.xml index 5acb687..275b10c 100644 --- a/resolver/pom.xml +++ b/resolver/pom.xml @@ -7,7 +7,6 @@ 1.4-SNAPSHOT - io.airlift.resolver resolver @@ -99,20 +98,15 @@ - org.slf4j - slf4j-api + io.airlift + log + org.testng testng test - - - org.slf4j - slf4j-nop - test - diff --git a/resolver/src/main/java/io/airlift/resolver/ArtifactResolver.java b/resolver/src/main/java/io/airlift/resolver/ArtifactResolver.java index 8b5e1cd..15f41f1 100644 --- a/resolver/src/main/java/io/airlift/resolver/ArtifactResolver.java +++ b/resolver/src/main/java/io/airlift/resolver/ArtifactResolver.java @@ -17,7 +17,7 @@ import com.google.common.collect.ImmutableList; import io.airlift.resolver.internal.ConsoleRepositoryListener; import io.airlift.resolver.internal.ConsoleTransferListener; -import io.airlift.resolver.internal.Slf4jLoggerManager; +import io.airlift.resolver.internal.JdkLoggerManager; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.MavenProject; @@ -221,7 +221,7 @@ private static PlexusContainer container() // NOTE: To avoid inconsistencies, we'll use the Thread context class loader exclusively for lookups container.setLookupRealm(null); - container.setLoggerManager(new Slf4jLoggerManager()); + container.setLoggerManager(new JdkLoggerManager()); container.getLoggerManager().setThresholds(Logger.LEVEL_INFO); return container; diff --git a/resolver/src/main/java/io/airlift/resolver/internal/ConsoleRepositoryListener.java b/resolver/src/main/java/io/airlift/resolver/internal/ConsoleRepositoryListener.java index 13e39cc..4227609 100644 --- a/resolver/src/main/java/io/airlift/resolver/internal/ConsoleRepositoryListener.java +++ b/resolver/src/main/java/io/airlift/resolver/internal/ConsoleRepositoryListener.java @@ -13,8 +13,7 @@ */ package io.airlift.resolver.internal; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import io.airlift.log.Logger; import org.sonatype.aether.AbstractRepositoryListener; import org.sonatype.aether.RepositoryEvent; @@ -24,100 +23,107 @@ public class ConsoleRepositoryListener extends AbstractRepositoryListener { - private final Logger logger; - - public ConsoleRepositoryListener() - { - this(LoggerFactory.getLogger(ConsoleRepositoryListener.class)); - } - - public ConsoleRepositoryListener(Logger logger) - { - this.logger = logger; - } + private static final Logger log = Logger.get(ConsoleRepositoryListener.class); + @Override public void artifactDeployed(RepositoryEvent event) { - logger.debug("Deployed " + event.getArtifact() + " to " + event.getRepository()); + log.debug("Deployed %s to %s", event.getArtifact(), event.getRepository()); } + @Override public void artifactDeploying(RepositoryEvent event) { - logger.debug("Deploying " + event.getArtifact() + " to " + event.getRepository()); + log.debug("Deploying %s to %s", event.getArtifact(), event.getRepository()); } + @Override public void artifactDescriptorInvalid(RepositoryEvent event) { - logger.debug("Invalid artifact descriptor for " + event.getArtifact() + ": " + event.getException().getMessage()); + log.debug("Invalid artifact descriptor for %s: %s", event.getArtifact(), event.getException().getMessage()); } + @Override public void artifactDescriptorMissing(RepositoryEvent event) { - logger.debug("Missing artifact descriptor for " + event.getArtifact()); + log.debug("Missing artifact descriptor for %s", event.getArtifact()); } + @Override public void artifactInstalled(RepositoryEvent event) { - logger.debug("Installed " + event.getArtifact() + " to " + event.getFile()); + log.debug("Installed %s to %s", event.getArtifact(), event.getFile()); } + @Override public void artifactInstalling(RepositoryEvent event) { - logger.debug("Installing " + event.getArtifact() + " to " + event.getFile()); + log.debug("Installing %s to %s", event.getArtifact(), event.getFile()); } + @Override public void artifactResolved(RepositoryEvent event) { - logger.debug("Resolved artifact " + event.getArtifact() + " from " + event.getRepository()); + log.debug("Resolved artifact %s from %s", event.getArtifact(), event.getRepository()); } + @Override public void artifactDownloading(RepositoryEvent event) { - logger.debug("Downloading artifact " + event.getArtifact() + " from " + event.getRepository()); + log.debug("Downloading artifact %s from %s", event.getArtifact(), event.getRepository()); } + @Override public void artifactDownloaded(RepositoryEvent event) { - logger.debug("Downloaded artifact " + event.getArtifact() + " from " + event.getRepository()); + log.debug("Downloaded artifact %s from %s", event.getArtifact(), event.getRepository()); } + @Override public void artifactResolving(RepositoryEvent event) { - logger.debug("Resolving artifact " + event.getArtifact()); + log.debug("Resolving artifact %s", event.getArtifact()); } + @Override public void metadataDeployed(RepositoryEvent event) { - logger.debug("Deployed " + event.getMetadata() + " to " + event.getRepository()); + log.debug("Deployed %s to %s", event.getMetadata(), event.getRepository()); } + @Override public void metadataDeploying(RepositoryEvent event) { - logger.debug("Deploying " + event.getMetadata() + " to " + event.getRepository()); + log.debug("Deploying %s to %s", event.getMetadata(), event.getRepository()); } + @Override public void metadataInstalled(RepositoryEvent event) { - logger.debug("Installed " + event.getMetadata() + " to " + event.getFile()); + log.debug("Installed %s to %s", event.getMetadata(), event.getFile()); } + @Override public void metadataInstalling(RepositoryEvent event) { - logger.debug("Installing " + event.getMetadata() + " to " + event.getFile()); + log.debug("Installing %s to %s", event.getMetadata(), event.getFile()); } + @Override public void metadataInvalid(RepositoryEvent event) { - logger.debug("Invalid metadata " + event.getMetadata()); + log.debug("Invalid metadata %s", event.getMetadata()); } + @Override public void metadataResolved(RepositoryEvent event) { - logger.debug("Resolved metadata " + event.getMetadata() + " from " + event.getRepository()); + log.debug("Resolved metadata %s from %s", event.getMetadata(), event.getRepository()); } + @Override public void metadataResolving(RepositoryEvent event) { - logger.debug("Resolving metadata " + event.getMetadata() + " from " + event.getRepository()); + log.debug("Resolving metadata %s from %s", event.getMetadata(), event.getRepository()); } } diff --git a/resolver/src/main/java/io/airlift/resolver/internal/ConsoleTransferListener.java b/resolver/src/main/java/io/airlift/resolver/internal/ConsoleTransferListener.java index c6fa1ad..7dacc82 100644 --- a/resolver/src/main/java/io/airlift/resolver/internal/ConsoleTransferListener.java +++ b/resolver/src/main/java/io/airlift/resolver/internal/ConsoleTransferListener.java @@ -13,8 +13,7 @@ */ package io.airlift.resolver.internal; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import io.airlift.log.Logger; import org.sonatype.aether.transfer.AbstractTransferListener; import org.sonatype.aether.transfer.TransferEvent; import org.sonatype.aether.transfer.TransferResource; @@ -29,24 +28,14 @@ public class ConsoleTransferListener extends AbstractTransferListener { - private final Logger logger; - - public ConsoleTransferListener() - { - this(LoggerFactory.getLogger(ConsoleTransferListener.class)); - } - - public ConsoleTransferListener(Logger logger) - { - this.logger = logger; - } + private static final Logger log = Logger.get(ConsoleTransferListener.class); @Override public void transferInitiated(TransferEvent event) { String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading"; - logger.debug(message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName()); + log.debug("%s: %s%s", message, event.getResource().getRepositoryUrl(), event.getResource().getResourceName()); } @Override @@ -71,19 +60,20 @@ public void transferSucceeded(TransferEvent event) throughput = " at " + format.format(kbPerSec) + " KB/sec"; } - logger.debug(type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len + throughput + ")"); + log.debug("%s: %s%s (%s%s)", type, resource.getRepositoryUrl(), resource.getResourceName(), len, throughput); } } @Override public void transferFailed(TransferEvent event) { - logger.debug("transfer failed", event.getException()); + log.debug(event.getException(), "Transfer failed"); } + @Override public void transferCorrupted(TransferEvent event) { - logger.debug("transfer corrupted", event.getException()); + log.debug(event.getException(), "Transfer corrupted"); } protected long toKB(long bytes) diff --git a/resolver/src/main/java/io/airlift/resolver/internal/Slf4jLogger.java b/resolver/src/main/java/io/airlift/resolver/internal/JdkLogger.java similarity index 63% rename from resolver/src/main/java/io/airlift/resolver/internal/Slf4jLogger.java rename to resolver/src/main/java/io/airlift/resolver/internal/JdkLogger.java index 1c3df09..25a45f5 100644 --- a/resolver/src/main/java/io/airlift/resolver/internal/Slf4jLogger.java +++ b/resolver/src/main/java/io/airlift/resolver/internal/JdkLogger.java @@ -20,100 +20,119 @@ import org.codehaus.plexus.logging.Logger; +import static java.util.logging.Level.FINE; +import static java.util.logging.Level.INFO; +import static java.util.logging.Level.SEVERE; +import static java.util.logging.Level.WARNING; + /** - * Adapt an SLF4J logger to a Plexus logger, ignoring Plexus logger API parts that are not classical and - * probably not really used. - * - * @author Jason van Zyl + * Adapt JDK logger to a Plexus logger, ignoring Plexus logger API parts that are not classical and + * probably not really used. This is based on Slf4jLogger by Json van Zyl. */ -public class Slf4jLogger +public class JdkLogger implements Logger { - private final org.slf4j.Logger logger; + private final java.util.logging.Logger logger; - public Slf4jLogger(org.slf4j.Logger logger) + public JdkLogger(java.util.logging.Logger logger) { this.logger = logger; } + @Override public void debug(String message) { - logger.debug(message); + logger.log(FINE, message); } + @Override public void debug(String message, Throwable throwable) { - logger.debug(message, throwable); + logger.log(FINE, message, throwable); } + @Override public boolean isDebugEnabled() { - return logger.isDebugEnabled(); + return logger.isLoggable(FINE); } + @Override public void info(String message) { - logger.info(message); + logger.log(INFO, message); } + @Override public void info(String message, Throwable throwable) { - logger.info(message, throwable); + logger.log(INFO, message, throwable); } + @Override public boolean isInfoEnabled() { - return logger.isInfoEnabled(); + return logger.isLoggable(INFO); } + @Override public void warn(String message) { - logger.warn(message); + logger.log(WARNING, message); } + @Override public void warn(String message, Throwable throwable) { - logger.warn(message, throwable); + logger.log(WARNING, message, throwable); } + @Override public boolean isWarnEnabled() { - return logger.isWarnEnabled(); + return logger.isLoggable(WARNING); } + @Override public void error(String message) { - logger.error(message); + logger.log(SEVERE, message); } + @Override public void error(String message, Throwable throwable) { - logger.error(message, throwable); + logger.log(SEVERE, message, throwable); } + @Override public boolean isErrorEnabled() { - return logger.isErrorEnabled(); + return logger.isLoggable(SEVERE); } + @Override public void fatalError(String message) { - logger.error(message); + error(message); } + @Override public void fatalError(String message, Throwable throwable) { - logger.error(message, throwable); + error(message, throwable); } + @Override public boolean isFatalErrorEnabled() { - return logger.isErrorEnabled(); + return isErrorEnabled(); } /** * Warning: ignored (always return 0 == Logger.LEVEL_DEBUG). */ + @Override public int getThreshold() { return 0; @@ -122,6 +141,7 @@ public int getThreshold() /** * Warning: ignored. */ + @Override public void setThreshold(int threshold) { } @@ -129,11 +149,13 @@ public void setThreshold(int threshold) /** * Warning: ignored (always return null). */ + @Override public Logger getChildLogger(String name) { return null; } + @Override public String getName() { return logger.getName(); diff --git a/resolver/src/main/java/io/airlift/resolver/internal/Slf4jLoggerManager.java b/resolver/src/main/java/io/airlift/resolver/internal/JdkLoggerManager.java similarity index 80% rename from resolver/src/main/java/io/airlift/resolver/internal/Slf4jLoggerManager.java rename to resolver/src/main/java/io/airlift/resolver/internal/JdkLoggerManager.java index 7422b9e..811f1bf 100644 --- a/resolver/src/main/java/io/airlift/resolver/internal/Slf4jLoggerManager.java +++ b/resolver/src/main/java/io/airlift/resolver/internal/JdkLoggerManager.java @@ -20,29 +20,21 @@ import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.LoggerManager; -import org.slf4j.ILoggerFactory; -import org.slf4j.LoggerFactory; + +import static java.util.logging.Logger.getLogger; /** - * Use an SLF4J {@link org.slf4j.ILoggerFactory} as a backing for a Plexus {@link org.codehaus.plexus.logging.LoggerManager}, + * Use JDK logging as a backing for a Plexus {@link LoggerManager}, * ignoring Plexus logger API parts that are not classical and probably not really used. - * - * @author Jason van Zyl - * @since 3.1 + * This is based on Slf4jLoggerManager by Json van Zyl. */ -public class Slf4jLoggerManager +public class JdkLoggerManager implements LoggerManager { - private final ILoggerFactory loggerFactory; - - public Slf4jLoggerManager() - { - loggerFactory = LoggerFactory.getILoggerFactory(); - } - + @Override public Logger getLoggerForComponent(String role) { - return new Slf4jLogger(loggerFactory.getLogger(role)); + return new JdkLogger(getLogger(role)); } /** @@ -50,18 +42,14 @@ public Logger getLoggerForComponent(String role) * Warning: this does not conform to logger name as class name convention. * (and what about null and default hint equivalence?) */ + @Override public Logger getLoggerForComponent(String role, String hint) { return (null == hint ? getLoggerForComponent(role) - : new Slf4jLogger(loggerFactory.getLogger(role + '.' + hint))); + : new JdkLogger(getLogger(role + '.' + hint))); } - // - // Trying to give loggers back is a bad idea. Ceki said so :-) - // notice to self: what was this method supposed to do? - // - /** * Warning: ignored. */ @@ -99,6 +87,7 @@ public int getThreshold(String role, String roleHint) /** * Warning: ignored. */ + @Override public void returnComponentLogger(String role) { } @@ -106,6 +95,7 @@ public void returnComponentLogger(String role) /** * Warning: ignored. */ + @Override public void returnComponentLogger(String role, String hint) { } @@ -113,6 +103,7 @@ public void returnComponentLogger(String role, String hint) /** * Warning: ignored (always return 0). */ + @Override public int getThreshold() { return 0; @@ -121,6 +112,7 @@ public int getThreshold() /** * Warning: ignored. */ + @Override public void setThreshold(int threshold) { } @@ -128,6 +120,7 @@ public void setThreshold(int threshold) /** * Warning: ignored. */ + @Override public void setThresholds(int threshold) { } @@ -135,6 +128,7 @@ public void setThresholds(int threshold) /** * Warning: ignored (always return 0). */ + @Override public int getActiveLoggerCount() { return 0; From a163abacb1ce6ffd6d44bdbd0cdb29c5bc696ec1 Mon Sep 17 00:00:00 2001 From: David Phillips Date: Tue, 23 Feb 2016 08:52:18 -0800 Subject: [PATCH 2/2] Move everything to main package --- .../src/main/java/io/airlift/resolver/ArtifactResolver.java | 3 --- .../resolver/{internal => }/ConsoleRepositoryListener.java | 4 ++-- .../resolver/{internal => }/ConsoleTransferListener.java | 4 ++-- .../java/io/airlift/resolver/{internal => }/JdkLogger.java | 4 ++-- .../io/airlift/resolver/{internal => }/JdkLoggerManager.java | 4 ++-- 5 files changed, 8 insertions(+), 11 deletions(-) rename resolver/src/main/java/io/airlift/resolver/{internal => }/ConsoleRepositoryListener.java (98%) rename resolver/src/main/java/io/airlift/resolver/{internal => }/ConsoleTransferListener.java (97%) rename resolver/src/main/java/io/airlift/resolver/{internal => }/JdkLogger.java (98%) rename resolver/src/main/java/io/airlift/resolver/{internal => }/JdkLoggerManager.java (97%) diff --git a/resolver/src/main/java/io/airlift/resolver/ArtifactResolver.java b/resolver/src/main/java/io/airlift/resolver/ArtifactResolver.java index 15f41f1..2095909 100644 --- a/resolver/src/main/java/io/airlift/resolver/ArtifactResolver.java +++ b/resolver/src/main/java/io/airlift/resolver/ArtifactResolver.java @@ -15,9 +15,6 @@ import com.google.common.collect.ImmutableList; -import io.airlift.resolver.internal.ConsoleRepositoryListener; -import io.airlift.resolver.internal.ConsoleTransferListener; -import io.airlift.resolver.internal.JdkLoggerManager; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.project.DefaultProjectBuildingRequest; import org.apache.maven.project.MavenProject; diff --git a/resolver/src/main/java/io/airlift/resolver/internal/ConsoleRepositoryListener.java b/resolver/src/main/java/io/airlift/resolver/ConsoleRepositoryListener.java similarity index 98% rename from resolver/src/main/java/io/airlift/resolver/internal/ConsoleRepositoryListener.java rename to resolver/src/main/java/io/airlift/resolver/ConsoleRepositoryListener.java index 4227609..7225ff6 100644 --- a/resolver/src/main/java/io/airlift/resolver/internal/ConsoleRepositoryListener.java +++ b/resolver/src/main/java/io/airlift/resolver/ConsoleRepositoryListener.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.airlift.resolver.internal; +package io.airlift.resolver; import io.airlift.log.Logger; import org.sonatype.aether.AbstractRepositoryListener; @@ -20,7 +20,7 @@ /** * A simplistic repository listener that logs events to the console. */ -public class ConsoleRepositoryListener +class ConsoleRepositoryListener extends AbstractRepositoryListener { private static final Logger log = Logger.get(ConsoleRepositoryListener.class); diff --git a/resolver/src/main/java/io/airlift/resolver/internal/ConsoleTransferListener.java b/resolver/src/main/java/io/airlift/resolver/ConsoleTransferListener.java similarity index 97% rename from resolver/src/main/java/io/airlift/resolver/internal/ConsoleTransferListener.java rename to resolver/src/main/java/io/airlift/resolver/ConsoleTransferListener.java index 7dacc82..4e6c367 100644 --- a/resolver/src/main/java/io/airlift/resolver/internal/ConsoleTransferListener.java +++ b/resolver/src/main/java/io/airlift/resolver/ConsoleTransferListener.java @@ -11,7 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.airlift.resolver.internal; +package io.airlift.resolver; import io.airlift.log.Logger; import org.sonatype.aether.transfer.AbstractTransferListener; @@ -25,7 +25,7 @@ /** * A simplistic transfer listener that logs uploads/downloads. */ -public class ConsoleTransferListener +class ConsoleTransferListener extends AbstractTransferListener { private static final Logger log = Logger.get(ConsoleTransferListener.class); diff --git a/resolver/src/main/java/io/airlift/resolver/internal/JdkLogger.java b/resolver/src/main/java/io/airlift/resolver/JdkLogger.java similarity index 98% rename from resolver/src/main/java/io/airlift/resolver/internal/JdkLogger.java rename to resolver/src/main/java/io/airlift/resolver/JdkLogger.java index 25a45f5..b2f2066 100644 --- a/resolver/src/main/java/io/airlift/resolver/internal/JdkLogger.java +++ b/resolver/src/main/java/io/airlift/resolver/JdkLogger.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package io.airlift.resolver.internal; +package io.airlift.resolver; import org.codehaus.plexus.logging.Logger; @@ -29,7 +29,7 @@ * Adapt JDK logger to a Plexus logger, ignoring Plexus logger API parts that are not classical and * probably not really used. This is based on Slf4jLogger by Json van Zyl. */ -public class JdkLogger +class JdkLogger implements Logger { private final java.util.logging.Logger logger; diff --git a/resolver/src/main/java/io/airlift/resolver/internal/JdkLoggerManager.java b/resolver/src/main/java/io/airlift/resolver/JdkLoggerManager.java similarity index 97% rename from resolver/src/main/java/io/airlift/resolver/internal/JdkLoggerManager.java rename to resolver/src/main/java/io/airlift/resolver/JdkLoggerManager.java index 811f1bf..f2f3101 100644 --- a/resolver/src/main/java/io/airlift/resolver/internal/JdkLoggerManager.java +++ b/resolver/src/main/java/io/airlift/resolver/JdkLoggerManager.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package io.airlift.resolver.internal; +package io.airlift.resolver; import org.codehaus.plexus.logging.Logger; import org.codehaus.plexus.logging.LoggerManager; @@ -28,7 +28,7 @@ * ignoring Plexus logger API parts that are not classical and probably not really used. * This is based on Slf4jLoggerManager by Json van Zyl. */ -public class JdkLoggerManager +class JdkLoggerManager implements LoggerManager { @Override