Skip to content

Commit

Permalink
Disable Jetty debug in Maven verbose mode
Browse files Browse the repository at this point in the history
Jetty produces many debug logs, it can make mess in verbose mode.
Disabled by default, can be enabled by debugServer parameter.
  • Loading branch information
slawekjaranowski committed Nov 29, 2022
1 parent 7e2a2e5 commit 4cd5a33
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public abstract class AbstractStartMojo
@Parameter
private ArtifactStoreFactory[] repositories;

/**
* Indicate if Jetty server should produce logs in debug level.
* <p>
* <b>Notice:</b> It is taken into account only when Maven is started in verbose mode.
*
* @since 1.5.0
*/
@Parameter( property = "mrm.debugServer", defaultValue = "false" )
private boolean debugServer;

/**
* Creates a file system server from an artifact store.
*
Expand All @@ -75,11 +85,11 @@ protected FileSystemServer createFileSystemServer( ArtifactStore artifactStore )
Math.max( 0, Math.min( port, 65535 ) ),
basePath,
new AutoDigestFileSystem( new ArtifactStoreFileSystem( artifactStore ) ),
getSettingsServletPath() );
getSettingsServletPath(), debugServer );
}

/**
* When set, this points to the to the location from where the settings file can be downloaded.
* When set, this points to the location from where the settings file can be downloaded.
*
* @return the servlet path to the settings file of {@code null}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

import java.net.InetAddress;
import java.net.UnknownHostException;
Expand Down Expand Up @@ -106,20 +108,28 @@ public class FileSystemServer
*/
private final String settingsServletPath;

/**
* Indicate debug level by Jetty server
*/
private final boolean debugServer;

/**
* Creates a new file system server that will serve a {@link FileSystem} over HTTP on the specified port.
*
* @param name The name of the file system server thread.
* @param port The port to server on or <code>0</code> to pick a random, but available, port.
* @param fileSystem the file system to serve.
* @param name The name of the file system server thread.
* @param port The port to server on or <code>0</code> to pick a random, but available, port.
* @param fileSystem the file system to serve.
* @param debugServer the server debug mode
*/
public FileSystemServer( String name, int port, String contextPath, FileSystem fileSystem, String settingsServletPath )
public FileSystemServer( String name, int port, String contextPath, FileSystem fileSystem, String settingsServletPath,
boolean debugServer )
{
this.name = name;
this.fileSystem = fileSystem;
this.requestedPort = port;
this.contextPath = sanitizeContextPath(contextPath);
this.settingsServletPath = settingsServletPath;
this.debugServer = debugServer;
}

/**
Expand Down Expand Up @@ -290,7 +300,12 @@ private final class Worker
public void run()
{
try {
Logger serverLogger = new ServerLogger( debugServer );
Log.setLog( serverLogger );
Log.initialized();

Server server = new Server(requestedPort);

try {
ServletContextHandler context = new ServletContextHandler();
context.setContextPath(contextPath);
Expand All @@ -307,7 +322,7 @@ public void run()
synchronized (lock) {
problem = e;
}
e.printStackTrace();
serverLogger.warn( e );
throw e;
}
synchronized (lock) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package org.codehaus.mojo.mrm.plugin;

/*
* Copyright MojoHaus and Contributors
*
* Licensed 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 org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.log.Slf4jLog;

/**
* Jetty SLF4J logger with debug level configuration.
*
* @author Slawomir Jaranowski
*/
class ServerLogger extends Slf4jLog
{
private final boolean debugEnabled;

public ServerLogger( boolean debugEnabled )
{
this( ServerLogger.class.getName(), debugEnabled );
}

public ServerLogger( String name, boolean debugEnabled )
{
super( name );
this.debugEnabled = debugEnabled;
}

@Override
public void debug( String msg, Object... args )
{
if ( isDebugEnabled() )
{
super.debug( msg, args );
}
}

@Override
public void debug( String msg, long arg )
{
if ( isDebugEnabled() )
{
super.debug( msg, arg );
}
}

@Override
public void debug( Throwable thrown )
{
if ( isDebugEnabled() )
{
super.debug( thrown );
}
}

@Override
public void debug( String msg, Throwable thrown )
{
if ( isDebugEnabled() )
{
super.debug( msg, thrown );
}
}

@Override
public boolean isDebugEnabled()
{
return debugEnabled;
}

@Override
public void setDebugEnabled( boolean enabled )
{
// do nothing
}

@Override
protected Logger newLogger( String fullname )
{
return new ServerLogger( fullname, debugEnabled );
}
}

0 comments on commit 4cd5a33

Please sign in to comment.