maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hbout...@apache.org
Subject git commit: added Slf4jConfigurationFactory to detect actual slf4j logging implementation and load appropriate Slf4jConfiguration implementation from META-INF/maven/slf4j-configuration.properties
Date Tue, 18 Dec 2012 22:44:28 GMT
Updated Branches:
  refs/heads/master eb190f029 -> 3511b09d1


added Slf4jConfigurationFactory to detect actual slf4j logging
implementation and load appropriate Slf4jConfiguration implementation
from META-INF/maven/slf4j-configuration.properties

Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/3511b09d
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/3511b09d
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/3511b09d

Branch: refs/heads/master
Commit: 3511b09d1e0fbea5a6e77593c297704a0b77600c
Parents: eb190f0
Author: Hervé Boutemy <hboutemy@apache.org>
Authored: Tue Dec 18 23:44:18 2012 +0100
Committer: Hervé Boutemy <hboutemy@apache.org>
Committed: Tue Dec 18 23:44:18 2012 +0100

----------------------------------------------------------------------
 .../main/java/org/apache/maven/cli/MavenCli.java   |    8 +-
 .../cli/logging/AbstractSlf4jConfiguration.java    |   46 --------
 .../maven/cli/logging/BaseSlf4jConfiguration.java  |   46 ++++++++
 .../cli/logging/Slf4jConfigurationFactory.java     |   81 +++++++++++++++
 .../cli/logging/impl/Slf4jSimpleConfiguration.java |    4 +-
 .../META-INF/maven/slf4j-configuration.properties  |    3 +
 6 files changed, 136 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/3511b09d/maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
----------------------------------------------------------------------
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 23d2ba1..6737463 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() );
     }
 

http://git-wip-us.apache.org/repos/asf/maven/blob/3511b09d/maven-embedder/src/main/java/org/apache/maven/cli/logging/AbstractSlf4jConfiguration.java
----------------------------------------------------------------------
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/AbstractSlf4jConfiguration.java
deleted file mode 100644
index 4f89a94..0000000
--- a/maven-embedder/src/main/java/org/apache/maven/cli/logging/AbstractSlf4jConfiguration.java
+++ /dev/null
@@ -1,46 +0,0 @@
-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.File;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Abstract implementation.
- * 
- * @author Hervé Boutemy
- */
-public class AbstractSlf4jConfiguration
-    implements Slf4jConfiguration
-{
-    private final Logger logger = LoggerFactory.getLogger( AbstractSlf4jConfiguration.class
);
-
-    public void setRootLoggerLevel( Level level )
-    {
-        logger.warn( "setRootLoggerLevel: operation not supported" );
-    }
-
-    public void setLoggerFile( File output )
-    {
-        logger.warn( "setLoggerFile: operation not supported" );
-    }
-}

http://git-wip-us.apache.org/repos/asf/maven/blob/3511b09d/maven-embedder/src/main/java/org/apache/maven/cli/logging/BaseSlf4jConfiguration.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/logging/BaseSlf4jConfiguration.java
b/maven-embedder/src/main/java/org/apache/maven/cli/logging/BaseSlf4jConfiguration.java
new file mode 100644
index 0000000..f82137e
--- /dev/null
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/logging/BaseSlf4jConfiguration.java
@@ -0,0 +1,46 @@
+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.File;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Abstract implementation.
+ * 
+ * @author Hervé Boutemy
+ */
+public class BaseSlf4jConfiguration
+    implements Slf4jConfiguration
+{
+    private final Logger logger = LoggerFactory.getLogger( BaseSlf4jConfiguration.class );
+
+    public void setRootLoggerLevel( Level level )
+    {
+        logger.warn( "setRootLoggerLevel: operation not supported" );
+    }
+
+    public void setLoggerFile( File output )
+    {
+        logger.warn( "setLoggerFile: operation not supported" );
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven/blob/3511b09d/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jConfigurationFactory.java
----------------------------------------------------------------------
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 0000000..2442a83
--- /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 <code>META-INF/maven/slf4j-configuration.properties</code>
+ * 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<URL> 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();
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven/blob/3511b09d/maven-embedder/src/main/java/org/apache/maven/cli/logging/impl/Slf4jSimpleConfiguration.java
----------------------------------------------------------------------
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 9012394..d2d8aa8 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 )

http://git-wip-us.apache.org/repos/asf/maven/blob/3511b09d/maven-embedder/src/main/resources/META-INF/maven/slf4j-configuration.properties
----------------------------------------------------------------------
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 0000000..07d06e8
--- /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


Mime
View raw message