[MNG-6915] Adapt the logging width to the terminal width, including sensible limits.

Slightly modified version of e47d647eee
This commit is contained in:
Guillaume Nodet 2021-05-19 14:20:09 +02:00 committed by Martin Kanters
parent 3fabb639a3
commit a20230829c
2 changed files with 77 additions and 20 deletions

View File

@ -39,6 +39,7 @@ import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.descriptor.MojoDescriptor; import org.apache.maven.plugin.descriptor.MojoDescriptor;
import org.apache.maven.project.MavenProject; import org.apache.maven.project.MavenProject;
import org.apache.maven.shared.utils.logging.MessageBuilder; import org.apache.maven.shared.utils.logging.MessageBuilder;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.StringUtils;
import org.slf4j.ILoggerFactory; import org.slf4j.ILoggerFactory;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -53,22 +54,42 @@ public class ExecutionEventLogger extends AbstractExecutionListener
{ {
private final Logger logger; private final Logger logger;
private static final int LINE_LENGTH = 72; private static final int MAX_LOG_PREFIX_SIZE = 8; // "[ERROR] "
private static final int PROJECT_STATUS_SUFFIX_SIZE = 20; // "SUCCESS [ 0.000 s]"
private static final int MIN_TERMINAL_WIDTH = 60;
private static final int DEFAULT_TERMINAL_WIDTH = 80;
private static final int MAX_TERMINAL_WIDTH = 130;
private static final int MAX_PADDED_BUILD_TIME_DURATION_LENGTH = 9; private static final int MAX_PADDED_BUILD_TIME_DURATION_LENGTH = 9;
private static final int MAX_PROJECT_NAME_LENGTH = 52;
private final int terminalWidth;
private final int lineLength;
private final int maxProjectNameLength;
private int totalProjects; private int totalProjects;
private volatile int currentVisitedProjectCount; private volatile int currentVisitedProjectCount;
public ExecutionEventLogger() public ExecutionEventLogger()
{ {
logger = LoggerFactory.getLogger( ExecutionEventLogger.class ); this( LoggerFactory.getLogger( ExecutionEventLogger.class ) );
}
public ExecutionEventLogger( int terminalWidth )
{
this( LoggerFactory.getLogger( ExecutionEventLogger.class ), terminalWidth );
} }
// TODO should we deprecate?
public ExecutionEventLogger( Logger logger ) public ExecutionEventLogger( Logger logger )
{
this( logger, MessageUtils.getTerminalWidth() );
}
public ExecutionEventLogger( Logger logger, int terminalWidth )
{ {
this.logger = Objects.requireNonNull( logger, "logger cannot be null" ); this.logger = Objects.requireNonNull( logger, "logger cannot be null" );
this.terminalWidth = Math.min( MAX_TERMINAL_WIDTH,
Math.max( terminalWidth < 0 ? DEFAULT_TERMINAL_WIDTH : terminalWidth,
MIN_TERMINAL_WIDTH ) );
this.lineLength = this.terminalWidth - MAX_LOG_PREFIX_SIZE;
this.maxProjectNameLength = this.lineLength - PROJECT_STATUS_SUFFIX_SIZE;
} }
private static String chars( char c, int count ) private static String chars( char c, int count )
@ -85,7 +106,7 @@ public class ExecutionEventLogger extends AbstractExecutionListener
private void infoLine( char c ) private void infoLine( char c )
{ {
infoMain( chars( c, LINE_LENGTH ) ); infoMain( chars( c, lineLength ) );
} }
private void infoMain( String msg ) private void infoMain( String msg )
@ -116,7 +137,7 @@ public class ExecutionEventLogger extends AbstractExecutionListener
final List<MavenProject> projects = event.getSession().getProjects(); final List<MavenProject> projects = event.getSession().getProjects();
for ( MavenProject project : projects ) for ( MavenProject project : projects )
{ {
int len = LINE_LENGTH - project.getName().length() - project.getPackaging().length() - 2; int len = lineLength - project.getName().length() - project.getPackaging().length() - 2;
logger.info( "{}{}[{}]", logger.info( "{}{}[{}]",
project.getName(), chars( ' ', ( len > 0 ) ? len : 1 ), project.getPackaging() ); project.getName(), chars( ' ', ( len > 0 ) ? len : 1 ), project.getPackaging() );
} }
@ -211,9 +232,9 @@ public class ExecutionEventLogger extends AbstractExecutionListener
buffer.append( ' ' ); buffer.append( ' ' );
} }
if ( buffer.length() <= MAX_PROJECT_NAME_LENGTH ) if ( buffer.length() <= maxProjectNameLength )
{ {
while ( buffer.length() < MAX_PROJECT_NAME_LENGTH ) while ( buffer.length() < maxProjectNameLength )
{ {
buffer.append( '.' ); buffer.append( '.' );
} }
@ -320,10 +341,10 @@ public class ExecutionEventLogger extends AbstractExecutionListener
final int headerLen = preHeader.length() + projectKey.length() + postHeader.length(); final int headerLen = preHeader.length() + projectKey.length() + postHeader.length();
String prefix = chars( '-', Math.max( 0, ( LINE_LENGTH - headerLen ) / 2 ) ) + preHeader; String prefix = chars( '-', Math.max( 0, ( lineLength - headerLen ) / 2 ) ) + preHeader;
String suffix = postHeader + chars( '-', String suffix = postHeader + chars( '-',
Math.max( 0, LINE_LENGTH - headerLen - prefix.length() + preHeader.length() ) ); Math.max( 0, lineLength - headerLen - prefix.length() + preHeader.length() ) );
logger.info( buffer().strong( prefix ).project( projectKey ).strong( suffix ).toString() ); logger.info( buffer().strong( prefix ).project( projectKey ).strong( suffix ).toString() );
@ -344,14 +365,14 @@ public class ExecutionEventLogger extends AbstractExecutionListener
} }
String progress = " [" + number + '/' + totalProjects + ']'; String progress = " [" + number + '/' + totalProjects + ']';
int pad = LINE_LENGTH - building.length() - progress.length(); int pad = lineLength - building.length() - progress.length();
infoMain( building + ( ( pad > 0 ) ? chars( ' ', pad ) : "" ) + progress ); infoMain( building + ( ( pad > 0 ) ? chars( ' ', pad ) : "" ) + progress );
} }
// ----------[ packaging ]---------- // ----------[ packaging ]----------
prefix = chars( '-', Math.max( 0, ( LINE_LENGTH - project.getPackaging().length() - 4 ) / 2 ) ); prefix = chars( '-', Math.max( 0, ( lineLength - project.getPackaging().length() - 4 ) / 2 ) );
suffix = chars( '-', Math.max( 0, LINE_LENGTH - project.getPackaging().length() - 4 - prefix.length() ) ); suffix = chars( '-', Math.max( 0, lineLength - project.getPackaging().length() - 4 - prefix.length() ) );
infoMain( prefix + "[ " + project.getPackaging() + " ]" + suffix ); infoMain( prefix + "[ " + project.getPackaging() + " ]" + suffix );
} }
} }

View File

@ -30,12 +30,11 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.mockito.Mockito;
import org.slf4j.Logger; import org.slf4j.Logger;
public class ExecutionEventLoggerTest class ExecutionEventLoggerTest
{ {
private ExecutionEventLogger executionEventLogger;
@BeforeAll @BeforeAll
public static void setUp() public static void setUp()
{ {
@ -49,12 +48,12 @@ public class ExecutionEventLoggerTest
} }
@Test @Test
public void testProjectStarted() void testProjectStarted()
{ {
// prepare // prepare
Logger logger = mock( Logger.class ); Logger logger = mock( Logger.class );
when( logger.isInfoEnabled() ).thenReturn( true ); when( logger.isInfoEnabled() ).thenReturn( true );
executionEventLogger = new ExecutionEventLogger( logger ); ExecutionEventLogger executionEventLogger = new ExecutionEventLogger( logger );
ExecutionEvent event = mock( ExecutionEvent.class ); ExecutionEvent event = mock( ExecutionEvent.class );
MavenProject project = mock( MavenProject.class ); MavenProject project = mock( MavenProject.class );
@ -77,12 +76,12 @@ public class ExecutionEventLoggerTest
} }
@Test @Test
public void testProjectStartedOverflow() void testProjectStartedOverflow()
{ {
// prepare // prepare
Logger logger = mock( Logger.class ); Logger logger = mock( Logger.class );
when( logger.isInfoEnabled() ).thenReturn( true ); when( logger.isInfoEnabled() ).thenReturn( true );
executionEventLogger = new ExecutionEventLogger( logger ); ExecutionEventLogger executionEventLogger = new ExecutionEventLogger( logger );
ExecutionEvent event = mock( ExecutionEvent.class ); ExecutionEvent event = mock( ExecutionEvent.class );
MavenProject project = mock( MavenProject.class ); MavenProject project = mock( MavenProject.class );
@ -103,4 +102,41 @@ public class ExecutionEventLoggerTest
inOrder.verify( logger ).info( "Building Apache Maven Project Info Reports Plugin 3.0.0-SNAPSHOT" ); inOrder.verify( logger ).info( "Building Apache Maven Project Info Reports Plugin 3.0.0-SNAPSHOT" );
inOrder.verify( logger ).info( "----------------------------[ maven-plugin ]----------------------------" ); inOrder.verify( logger ).info( "----------------------------[ maven-plugin ]----------------------------" );
} }
@Test
void testTerminalWidth()
{
// prepare
Logger logger = mock( Logger.class );
when( logger.isInfoEnabled() ).thenReturn( true );
ExecutionEvent event = mock( ExecutionEvent.class );
MavenProject project = mock( MavenProject.class );
when( project.getGroupId() ).thenReturn( "org.apache.maven.plugins.overflow" );
when( project.getArtifactId() ).thenReturn( "maven-project-info-reports-plugin" );
when( project.getPackaging() ).thenReturn( "maven-plugin" );
when( project.getName() ).thenReturn( "Apache Maven Project Info Reports Plugin" );
when( project.getVersion() ).thenReturn( "3.0.0-SNAPSHOT" );
when( event.getProject() ).thenReturn( project );
// default width
new ExecutionEventLogger( logger, -1 ).projectStarted( event );
Mockito.verify( logger ).info( "----------------------------[ maven-plugin ]----------------------------" );
// terminal width: 30
new ExecutionEventLogger( logger, 30 ).projectStarted( event );
Mockito.verify( logger ).info( "------------------[ maven-plugin ]------------------" );
// terminal width: 70
new ExecutionEventLogger( logger, 70 ).projectStarted( event );
Mockito.verify( logger ).info( "-----------------------[ maven-plugin ]-----------------------" );
// terminal width: 110
new ExecutionEventLogger( logger, 110 ).projectStarted( event );
Mockito.verify( logger ).info( "-------------------------------------------[ maven-plugin ]-------------------------------------------" );
// terminal width: 200
new ExecutionEventLogger( logger, 200 ).projectStarted( event );
Mockito.verify( logger ).info( "-----------------------------------------------------[ maven-plugin ]-----------------------------------------------------" );
}
} }