maven-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject [2/6] git commit: fix transfer listener with slf4j
Date Sat, 01 Dec 2012 20:28:07 GMT
fix transfer listener with slf4j


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

Branch: refs/heads/dynamic-logging-impl
Commit: ce3ae268d2125cd468879d109b35305629febe9b
Parents: 59404a2
Author: olivier lamy <olamy@apache.org>
Authored: Sat Dec 1 21:22:47 2012 +0100
Committer: olivier lamy <olamy@apache.org>
Committed: Sat Dec 1 21:22:47 2012 +0100

----------------------------------------------------------------------
 apache-maven/src/conf/logging/log4j2.xml           |    6 +-
 apache-maven/src/conf/logging/logback.xml          |   10 ++
 .../main/java/org/apache/maven/cli/MavenCli.java   |   15 ++-
 .../maven/cli/logging/Slf4jLoggerManager.java      |    8 +
 .../org/apache/maven/cli/startup/MavenStartup.java |   31 ++++-
 .../transfer/AbstractMavenTransferListener.java    |   20 +++
 .../cli/transfer/ConsoleMavenTransferListener.java |   20 +---
 .../cli/transfer/Slf4jMavenTransferListener.java   |  111 +++++++++++++--
 8 files changed, 179 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/ce3ae268/apache-maven/src/conf/logging/log4j2.xml
----------------------------------------------------------------------
diff --git a/apache-maven/src/conf/logging/log4j2.xml b/apache-maven/src/conf/logging/log4j2.xml
index 367122f..f5c33e1 100644
--- a/apache-maven/src/conf/logging/log4j2.xml
+++ b/apache-maven/src/conf/logging/log4j2.xml
@@ -28,14 +28,14 @@
   <appenders>
     <Console name="console" target="SYSTEM_OUT">
       <!--PatternLayout pattern="%highlight[%p{WARN=WARNING}] %msg%n{FATAL=white, ERROR=red,
WARN=blue, INFO=black, DEBUG=green, TRACE=blue}"/-->
-      <PatternLayout pattern="%highlight{%c{} [%p] %msg%n%throwable}{FATAL=white, ERROR=red,
WARN=blue, INFO=black, DEBUG=green, TRACE=blue}" />
+      <PatternLayout pattern="%highlight{[%p] %msg%n%throwable}{FATAL=white, ERROR=red,
WARN=blue, INFO=black, DEBUG=green, TRACE=blue}" />
     </Console>
     <Console name="console-transfer" target="SYSTEM_OUT">
       <PatternLayout pattern="[%p{WARN=WARNING}] %msg"/>
     </Console>
     <Console name="debug-console" target="SYSTEM_OUT">
       <!--PatternLayout pattern="%highlight[%p{WARN=WARNING}] %msg%n{FATAL=white, ERROR=red,
WARN=blue, INFO=black, DEBUG=green, TRACE=blue}"/-->
-      <PatternLayout pattern="%highlight{%c{} [%p] %msg%n%throwable}{FATAL=white, ERROR=red,
WARN=blue, INFO=black, DEBUG=green, TRACE=blue}" />
+      <PatternLayout pattern="%highlight{[%p] %msg%n%throwable}{FATAL=white, ERROR=red,
WARN=blue, INFO=black, DEBUG=green, TRACE=blue}" />
     </Console>
   </appenders>
   <loggers>
@@ -50,7 +50,7 @@
       <appender-ref ref="debug-console"/>
     </logger>
 
-    <logger name="consoleTransfer" level="info" additivity="false">
+    <logger name="console-transfer" level="info" additivity="false">
       <appender-ref ref="console-transfer"/>
     </logger>
 

http://git-wip-us.apache.org/repos/asf/maven/blob/ce3ae268/apache-maven/src/conf/logging/logback.xml
----------------------------------------------------------------------
diff --git a/apache-maven/src/conf/logging/logback.xml b/apache-maven/src/conf/logging/logback.xml
index 5a192eb..821685c 100644
--- a/apache-maven/src/conf/logging/logback.xml
+++ b/apache-maven/src/conf/logging/logback.xml
@@ -25,6 +25,16 @@
     </encoder>
   </appender>
 
+  <appender name="console-transfer" class="ch.qos.logback.core.ConsoleAppender">
+    <encoder>
+      <pattern>[%-5level] %msg</pattern>
+    </encoder>
+  </appender>
+
+  <logger name="console-transfer" level="info" additivity="false">
+    <appender-ref ref="console-transfer"/>
+  </logger>
+
   <root level="${maven.logging.root.level}">
     <appender-ref ref="STDOUT" />
   </root>

http://git-wip-us.apache.org/repos/asf/maven/blob/ce3ae268/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 eea35c2..e84d792 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
@@ -1155,15 +1155,22 @@ public class MavenCli
     //
     // Customizations available via the CLI
     //
-    
-    protected TransferListener getConsoleTransferListener() 
+
+    protected TransferListener getConsoleTransferListener()
     {
-        return new ConsoleMavenTransferListener( System.out );
+        Logger logger = slf4jLoggerFactory.getLogger( "console-transfer" );
+        // in case of slf4j-simple we use system.out as transfer listener
+        if ( "org.slf4j.impl.SimpleLogger".equals( logger.getClass().getName() ) )
+        {
+            return new ConsoleMavenTransferListener( System.out );
+        }
+        // we use a different logger here as we don't want to go to line after each log statement
+        return new Slf4jMavenTransferListener( logger );
     }
     
     protected TransferListener getBatchTransferListener()
     {
-        return new Slf4jMavenTransferListener();
+        return new Slf4jMavenTransferListener( slf4jLoggerFactory.getLogger( "console-transfer"
) );
     }
     
     protected void customizeContainer( PlexusContainer container )

http://git-wip-us.apache.org/repos/asf/maven/blob/ce3ae268/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jLoggerManager.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jLoggerManager.java
b/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jLoggerManager.java
index f30ea8f..2c63f0b 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jLoggerManager.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/logging/Slf4jLoggerManager.java
@@ -23,6 +23,12 @@ import org.codehaus.plexus.logging.Logger;
 import org.codehaus.plexus.logging.LoggerManager;
 import org.slf4j.ILoggerFactory;
 import org.slf4j.LoggerFactory;
+import org.sonatype.aether.transfer.TransferCancelledException;
+import org.sonatype.aether.transfer.TransferEvent;
+import org.sonatype.aether.transfer.TransferResource;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Use an SLF4J {@link org.slf4j.ILoggerFactory} as a backing for a Plexus {@link org.codehaus.plexus.logging.LoggerManager},
@@ -37,6 +43,8 @@ public class Slf4jLoggerManager
 
     private ILoggerFactory loggerFactory;
 
+    private Map<TransferResource, Long> downloads = new ConcurrentHashMap<TransferResource,
Long>();
+
     public Slf4jLoggerManager()
     {
         loggerFactory = LoggerFactory.getILoggerFactory();

http://git-wip-us.apache.org/repos/asf/maven/blob/ce3ae268/maven-embedder/src/main/java/org/apache/maven/cli/startup/MavenStartup.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/startup/MavenStartup.java b/maven-embedder/src/main/java/org/apache/maven/cli/startup/MavenStartup.java
index 1760f28..d0fc7dd 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/startup/MavenStartup.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/startup/MavenStartup.java
@@ -31,6 +31,8 @@ import java.net.URL;
  */
 public class MavenStartup
 {
+    private static final String DEFAULT_LOGGING_REALM = "log4j2";
+
     /**
      * just here to prevent "warning" from classworld which try to get this method
      * @param args
@@ -44,28 +46,49 @@ public class MavenStartup
 
     public static int main( String[] args, ClassWorld classWorld )
     {
-        String loggerImplRealmId = System.getProperty( "maven.logger.impl", "log4j2" );
+        String loggerImplRealmId = System.getProperty( "maven.logger.impl", DEFAULT_LOGGING_REALM
);
 
         // take care of empty sys props !
         if (loggerImplRealmId == null || loggerImplRealmId.length()<1)
         {
-            loggerImplRealmId = "log4j2";
+            loggerImplRealmId = DEFAULT_LOGGING_REALM;
         }
 
         try
         {
-            ClassRealm classRealm = classWorld.getRealm( loggerImplRealmId );
+            ClassRealm classRealm = getLoggingClassRealm( loggerImplRealmId, classWorld );
+            if (classRealm == null)
+            {
+                classRealm = classWorld.getClassRealm( DEFAULT_LOGGING_REALM );
+            }
             for (URL url : classRealm.getURLs())
             {
                 classWorld.getRealm( "plexus.core" ).addURL( url );
             }
-            //return org.apache.maven.cli.MavenCli.doMain( args, classWorld );
             return MavenCli.doMain( args, classWorld );
         }
         catch ( NoSuchRealmException e )
         {
+            System.out.println("Issue configuring Apache Maven: " + e.getMessage());
             e.printStackTrace();
         }
+
         return 1;
     }
+
+    private static ClassRealm getLoggingClassRealm(String loggerImplRealmId, ClassWorld classWorld)
+    {
+        ClassRealm classRealm = null;
+        try
+        {
+            classRealm = classWorld.getRealm( loggerImplRealmId );
+        }
+        catch ( NoSuchRealmException e )
+        {
+            System.out.println("Cannot find realm logger with id: '" + loggerImplRealmId
+ "', revert to default");
+            return null;
+        }
+
+        return classRealm;
+    }
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/ce3ae268/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
index d63cdfc..af3235c 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java
@@ -87,4 +87,24 @@ public abstract class AbstractMavenTransferListener
         return ( bytes + 1023 ) / 1024;
     }
 
+    protected String getStatus( long complete, long total )
+    {
+        if ( total >= 1024 )
+        {
+            return toKB( complete ) + "/" + toKB( total ) + " KB ";
+        }
+        else if ( total >= 0 )
+        {
+            return complete + "/" + total + " B ";
+        }
+        else if ( complete >= 1024 )
+        {
+            return toKB( complete ) + " KB ";
+        }
+        else
+        {
+            return complete + " B ";
+        }
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/ce3ae268/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java
b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java
index 2263c93..6895b5e 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/ConsoleMavenTransferListener.java
@@ -73,25 +73,7 @@ public class ConsoleMavenTransferListener
         out.print( buffer.toString() );
     }
 
-    private String getStatus( long complete, long total )
-    {
-        if ( total >= 1024 )
-        {
-            return toKB( complete ) + "/" + toKB( total ) + " KB ";
-        }
-        else if ( total >= 0 )
-        {
-            return complete + "/" + total + " B ";
-        }
-        else if ( complete >= 1024 )
-        {
-            return toKB( complete ) + " KB ";
-        }
-        else
-        {
-            return complete + " B ";
-        }
-    }
+
 
     private void pad( StringBuilder buffer, int spaces )
     {

http://git-wip-us.apache.org/repos/asf/maven/blob/ce3ae268/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java
----------------------------------------------------------------------
diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java
b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java
index f09c768..f4905c7 100644
--- a/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java
+++ b/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java
@@ -22,6 +22,8 @@ package org.apache.maven.cli.transfer;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
 import java.util.Locale;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -34,14 +36,16 @@ public class Slf4jMavenTransferListener
     extends AbstractTransferListener
 {
 
-    protected final Logger out;
+    protected Logger logger = LoggerFactory.getLogger( getClass() );
+
+    protected Logger out;
+
+
+    private Map<TransferResource, Long> downloads = new ConcurrentHashMap<TransferResource,
Long>();
+
+    private int lastLength;
 
-    public Slf4jMavenTransferListener()
-    {
-        this.out = LoggerFactory.getLogger( Slf4jMavenTransferListener.class );
-    }
 
-    // TODO should we deprecate?
     public Slf4jMavenTransferListener( Logger out )
     {
         this.out = out;
@@ -52,7 +56,7 @@ public class Slf4jMavenTransferListener
     {
         String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading"
: "Downloading";
 
-        out.info( message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName()
);
+        logger.info( message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName()
);
     }
 
     @Override
@@ -61,12 +65,80 @@ public class Slf4jMavenTransferListener
     {
         TransferResource resource = event.getResource();
 
-        out.warn( event.getException().getMessage() + " for " + resource.getRepositoryUrl()
+ resource.getResourceName() );
+        logger.warn( "[WARNING] " + event.getException().getMessage() + " for " + resource.getRepositoryUrl()
+                         + resource.getResourceName() );
+    }
+
+    protected long toKB( long bytes )
+    {
+        return ( bytes + 1023 ) / 1024;
+    }
+
+
+    @Override
+    public void transferProgressed( TransferEvent event )
+        throws TransferCancelledException
+    {
+        TransferResource resource = event.getResource();
+        downloads.put( resource, Long.valueOf( event.getTransferredBytes() ) );
+
+        StringBuilder buffer = new StringBuilder( 64 );
+
+        for ( Map.Entry<TransferResource, Long> entry : downloads.entrySet() )
+        {
+            long total = entry.getKey().getContentLength();
+            Long complete = entry.getValue();
+            // NOTE: This null check guards against http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6312056
+            if ( complete != null )
+            {
+                buffer.append( getStatus( complete.longValue(), total ) ).append( "  " );
+            }
+        }
+
+        int pad = lastLength - buffer.length();
+        lastLength = buffer.length();
+        pad( buffer, pad );
+        buffer.append( '\r' );
+
+        out.info( buffer.toString() );
+    }
+
+    private String getStatus( long complete, long total )
+    {
+        if ( total >= 1024 )
+        {
+            return toKB( complete ) + "/" + toKB( total ) + " KB ";
+        }
+        else if ( total >= 0 )
+        {
+            return complete + "/" + total + " B ";
+        }
+        else if ( complete >= 1024 )
+        {
+            return toKB( complete ) + " KB ";
+        }
+        else
+        {
+            return complete + " B ";
+        }
+    }
+
+    private void pad( StringBuilder buffer, int spaces )
+    {
+        String block = "                                        ";
+        while ( spaces > 0 )
+        {
+            int n = Math.min( spaces, block.length() );
+            buffer.append( block, 0, n );
+            spaces -= n;
+        }
     }
 
     @Override
     public void transferSucceeded( TransferEvent event )
     {
+        transferCompleted( event );
+
         TransferResource resource = event.getResource();
         long contentLength = event.getTransferredBytes();
         if ( contentLength >= 0 )
@@ -83,14 +155,29 @@ public class Slf4jMavenTransferListener
                 throughput = " at " + format.format( kbPerSec ) + " KB/sec";
             }
 
-            out.info( type + ": " + resource.getRepositoryUrl() + resource.getResourceName()
+ " (" + len
-                + throughput + ")" );
+            logger.info(
+                type + ": " + resource.getRepositoryUrl() + resource.getResourceName() +
" (" + len + throughput
+                    + ")" );
         }
     }
 
-    protected long toKB( long bytes )
+    @Override
+    public void transferFailed( TransferEvent event )
     {
-        return ( bytes + 1023 ) / 1024;
+        transferCompleted( event );
+
+        super.transferFailed( event );
+    }
+
+    private void transferCompleted( TransferEvent event )
+    {
+        downloads.remove( event.getResource() );
+
+        StringBuilder buffer = new StringBuilder( 64 );
+        pad( buffer, lastLength );
+        buffer.append( '\r' );
+        out.info( buffer.toString() );
     }
 
+
 }


Mime
View raw message