From 3511b09d1e0fbea5a6e77593c297704a0b77600c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Boutemy?= Date: Tue, 18 Dec 2012 23:44:18 +0100 Subject: [PATCH] added Slf4jConfigurationFactory to detect actual slf4j logging implementation and load appropriate Slf4jConfiguration implementation from META-INF/maven/slf4j-configuration.properties --- .../java/org/apache/maven/cli/MavenCli.java | 8 +- ...ation.java => BaseSlf4jConfiguration.java} | 4 +- .../logging/Slf4jConfigurationFactory.java | 81 +++++++++++++++++++ .../impl/Slf4jSimpleConfiguration.java | 4 +- .../maven/slf4j-configuration.properties | 3 + 5 files changed, 92 insertions(+), 8 deletions(-) rename maven-embedder/src/main/java/org/apache/maven/cli/logging/{AbstractSlf4jConfiguration.java => BaseSlf4jConfiguration.java} (90%) create mode 100644 maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jConfigurationFactory.java create mode 100644 maven-embedder/src/main/resources/META-INF/maven/slf4j-configuration.properties diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java index 23d2ba1dcd..6737463768 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java @@ -40,9 +40,9 @@ import org.apache.maven.Maven; import org.apache.maven.cli.event.DefaultEventSpyContext; import org.apache.maven.cli.event.ExecutionEventLogger; import org.apache.maven.cli.logging.Slf4jConfiguration; +import org.apache.maven.cli.logging.Slf4jConfigurationFactory; import org.apache.maven.cli.logging.Slf4jLoggerManager; import org.apache.maven.cli.logging.Slf4jStdoutLogger; -import org.apache.maven.cli.logging.impl.Slf4jSimpleConfiguration; import org.apache.maven.cli.transfer.ConsoleMavenTransferListener; import org.apache.maven.cli.transfer.QuietMavenTransferListener; import org.apache.maven.cli.transfer.Slf4jMavenTransferListener; @@ -133,8 +133,6 @@ public class MavenCli private DefaultSecDispatcher dispatcher; - private Slf4jConfiguration slf4jConfiguration = new Slf4jSimpleConfiguration(); - public MavenCli() { this( null ); @@ -307,6 +305,9 @@ public class MavenCli cliRequest.quiet = !cliRequest.debug && cliRequest.commandLine.hasOption( CLIManager.QUIET ); cliRequest.showErrors = cliRequest.debug || cliRequest.commandLine.hasOption( CLIManager.ERRORS ); + slf4jLoggerFactory = LoggerFactory.getILoggerFactory(); + Slf4jConfiguration slf4jConfiguration = Slf4jConfigurationFactory.getConfiguration( slf4jLoggerFactory ); + if ( cliRequest.debug ) { cliRequest.request.setLoggingLevel( MavenExecutionRequest.LOGGING_LEVEL_DEBUG ); @@ -346,7 +347,6 @@ public class MavenCli } plexusLoggerManager = new Slf4jLoggerManager(); - slf4jLoggerFactory = LoggerFactory.getILoggerFactory(); slf4jLogger = slf4jLoggerFactory.getLogger( this.getClass().getName() ); } diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/logging/AbstractSlf4jConfiguration.java b/maven-embedder/src/main/java/org/apache/maven/cli/logging/BaseSlf4jConfiguration.java similarity index 90% rename from maven-embedder/src/main/java/org/apache/maven/cli/logging/AbstractSlf4jConfiguration.java rename to maven-embedder/src/main/java/org/apache/maven/cli/logging/BaseSlf4jConfiguration.java index 4f89a94c5e..f82137eed8 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/logging/AbstractSlf4jConfiguration.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/logging/BaseSlf4jConfiguration.java @@ -29,10 +29,10 @@ import org.slf4j.LoggerFactory; * * @author Hervé Boutemy */ -public class AbstractSlf4jConfiguration +public class BaseSlf4jConfiguration implements Slf4jConfiguration { - private final Logger logger = LoggerFactory.getLogger( AbstractSlf4jConfiguration.class ); + private final Logger logger = LoggerFactory.getLogger( BaseSlf4jConfiguration.class ); public void setRootLoggerLevel( Level level ) { diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jConfigurationFactory.java b/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jConfigurationFactory.java new file mode 100644 index 0000000000..2442a835f5 --- /dev/null +++ b/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jConfigurationFactory.java @@ -0,0 +1,81 @@ +package org.apache.maven.cli.logging; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.IOException; +import java.net.URL; +import java.util.Enumeration; +import java.util.Properties; + +import org.codehaus.plexus.util.PropertyUtils; +import org.slf4j.ILoggerFactory; + +/** + * Slf4jConfiguration factory, loading implementations from META-INF/maven/slf4j-configuration.properties + * configuration files in class loader. + * + * @author Hervé Boutemy + */ +public class Slf4jConfigurationFactory +{ + public static final String RESOURCE = "META-INF/maven/slf4j-configuration.properties"; + + public static Slf4jConfiguration getConfiguration( ILoggerFactory loggerFactory ) + { + try + { + Enumeration resources = Slf4jConfigurationFactory.class.getClassLoader().getResources( RESOURCE ); + + String key = loggerFactory.getClass().getCanonicalName(); + + while ( resources.hasMoreElements() ) + { + URL resource = resources.nextElement(); + + Properties conf = PropertyUtils.loadProperties( resource.openStream() ); + + String impl = conf.getProperty( key ); + + if ( impl != null ) + { + return (Slf4jConfiguration) Class.forName( impl ).newInstance(); + } + } + } + catch ( IOException e ) + { + e.printStackTrace(); + } + catch ( InstantiationException e ) + { + e.printStackTrace(); + } + catch ( IllegalAccessException e ) + { + e.printStackTrace(); + } + catch ( ClassNotFoundException e ) + { + e.printStackTrace(); + } + + return new BaseSlf4jConfiguration(); + } +} diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/logging/impl/Slf4jSimpleConfiguration.java b/maven-embedder/src/main/java/org/apache/maven/cli/logging/impl/Slf4jSimpleConfiguration.java index 901239492d..d2d8aa8212 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/logging/impl/Slf4jSimpleConfiguration.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/logging/impl/Slf4jSimpleConfiguration.java @@ -21,7 +21,7 @@ package org.apache.maven.cli.logging.impl; import java.io.File; -import org.apache.maven.cli.logging.AbstractSlf4jConfiguration; +import org.apache.maven.cli.logging.BaseSlf4jConfiguration; /** * Configuration for slf4j-simple. @@ -29,7 +29,7 @@ import org.apache.maven.cli.logging.AbstractSlf4jConfiguration; * @author Hervé Boutemy */ public class Slf4jSimpleConfiguration - extends AbstractSlf4jConfiguration + extends BaseSlf4jConfiguration { @Override public void setRootLoggerLevel( Level level ) diff --git a/maven-embedder/src/main/resources/META-INF/maven/slf4j-configuration.properties b/maven-embedder/src/main/resources/META-INF/maven/slf4j-configuration.properties new file mode 100644 index 0000000000..07d06e8aaa --- /dev/null +++ b/maven-embedder/src/main/resources/META-INF/maven/slf4j-configuration.properties @@ -0,0 +1,3 @@ +# key = Slf4j effective logger factory implementation +# value = corresponding o.a.m.cli.logging.Slf4jConfiguration class +org.slf4j.impl.SimpleLoggerFactory org.apache.maven.cli.logging.impl.Slf4jSimpleConfiguration