From a12706e8e9d5e69d7ed645649b50578b2aec0e0c Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Tue, 29 Nov 2022 09:49:54 +0100 Subject: [PATCH] Disable Jetty debug in Maven verbose mode Jetty produces many debug logs, it can make mess in verbose mode. Disabled by default, can be enabled by debugServer parameter. --- .../mojo/mrm/plugin/AbstractStartMojo.java | 14 ++- .../mojo/mrm/plugin/FileSystemServer.java | 25 ++++- .../mojo/mrm/plugin/ServerLogger.java | 96 +++++++++++++++++++ 3 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/ServerLogger.java diff --git a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/AbstractStartMojo.java b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/AbstractStartMojo.java index 3c5ddf2f..95b67987 100644 --- a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/AbstractStartMojo.java +++ b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/AbstractStartMojo.java @@ -63,6 +63,16 @@ public abstract class AbstractStartMojo @Parameter private ArtifactStoreFactory[] repositories; + /** + * Indicate if Jetty server should produce logs in debug level. + *

+ * Notice: 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. * @@ -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} */ diff --git a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/FileSystemServer.java b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/FileSystemServer.java index 8d99b5f7..264ef554 100644 --- a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/FileSystemServer.java +++ b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/FileSystemServer.java @@ -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; @@ -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 0 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 0 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; } /** @@ -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); @@ -307,7 +322,7 @@ public void run() synchronized (lock) { problem = e; } - e.printStackTrace(); + serverLogger.warn( e ); throw e; } synchronized (lock) { diff --git a/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/ServerLogger.java b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/ServerLogger.java new file mode 100644 index 00000000..e81f36de --- /dev/null +++ b/mrm-maven-plugin/src/main/java/org/codehaus/mojo/mrm/plugin/ServerLogger.java @@ -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 ); + } +}