From 3b57a2b55aea487ae5da91f4351fafbe28e189a9 Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Fri, 2 Oct 2009 14:40:27 +0000 Subject: [PATCH] [MNG-3183] Allow a user to specify logging to a text file o Restored feature git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@821036 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/maven/cli/MavenCli.java | 13 +- .../apache/maven/cli/MavenLoggerManager.java | 162 ++++++++++++++++++ .../apache/maven/cli/PrintStreamLogger.java | 69 +------- 3 files changed, 178 insertions(+), 66 deletions(-) create mode 100644 maven-embedder/src/main/java/org/apache/maven/cli/MavenLoggerManager.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 95e99ab54c..172f0d780d 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 @@ -153,10 +153,19 @@ public class MavenCli .setName( "embedder" ); container = new DefaultPlexusContainer( cc ); - + logger = container.getLogger(); + + if ( commandLine.hasOption( CLIManager.LOG_FILE ) ) + { + File logFile = new File( commandLine.getOptionValue( CLIManager.LOG_FILE ) ).getAbsoluteFile(); + + logger = new FileLogger( logFile ); + + container.setLoggerManager( new MavenLoggerManager( logger ) ); + } - maven = container.lookup( Maven.class ); + maven = container.lookup( Maven.class ); } catch ( PlexusContainerException e ) { diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/MavenLoggerManager.java b/maven-embedder/src/main/java/org/apache/maven/cli/MavenLoggerManager.java new file mode 100644 index 0000000000..0d512aee12 --- /dev/null +++ b/maven-embedder/src/main/java/org/apache/maven/cli/MavenLoggerManager.java @@ -0,0 +1,162 @@ +package org.apache.maven.cli; + +/* + * 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.util.Locale; + +import org.codehaus.plexus.logging.AbstractLoggerManager; +import org.codehaus.plexus.logging.Logger; +import org.codehaus.plexus.logging.LoggerManager; +import org.codehaus.plexus.logging.console.ConsoleLogger; +import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable; + +/** + * This is a simple logger manager that will only write the logging statements to the console. + *

+ * Sample configuration: + *

+ * 
+ *   org.codehaus.plexus.logging.ConsoleLoggerManager
+ *   
+ *     DEBUG
+ *   
+ * 
+ * 
+ * + * @author Jason van Zyl + * @author Trygve Laugstøl + */ +public class MavenLoggerManager + extends AbstractLoggerManager + implements LoggerManager, Initializable +{ + /** + * Message of this level or higher will be logged. + *

+ * This field is set by the plexus container thus the name is 'threshold'. The field + * currentThreshold contains the current setting of the threshold. + */ + private String threshold = "info"; + + private int currentThreshold; + + private Logger logger; + + public MavenLoggerManager( Logger logger ) + { + this.logger = logger; + } + + public void initialize() + { + debug( "Initializing ConsoleLoggerManager: " + this.hashCode() + "." ); + + currentThreshold = parseThreshold( threshold ); + + if ( currentThreshold == -1 ) + { + debug( "Could not parse the threshold level: '" + threshold + "', setting to debug." ); + currentThreshold = Logger.LEVEL_DEBUG; + } + } + + public void setThreshold( int currentThreshold ) + { + this.currentThreshold = currentThreshold; + } + + public void setThresholds( int currentThreshold ) + { + this.currentThreshold = currentThreshold; + + logger.setThreshold( currentThreshold ); + } + + /** @return Returns the threshold. */ + public int getThreshold() + { + return currentThreshold; + } + + public void setThreshold( String role, + String roleHint, + int threshold ) + { + } + + public int getThreshold( String role, + String roleHint ) + { + return currentThreshold; + } + + public Logger getLoggerForComponent( String role, + String roleHint ) + { + return logger; + } + + public void returnComponentLogger( String role, + String roleHint ) + { + } + + public int getActiveLoggerCount() + { + return 1; + } + + private int parseThreshold( String text ) + { + text = text.trim().toLowerCase( Locale.ENGLISH ); + + if ( text.equals( "debug" ) ) + { + return ConsoleLogger.LEVEL_DEBUG; + } + else if ( text.equals( "info" ) ) + { + return ConsoleLogger.LEVEL_INFO; + } + else if ( text.equals( "warn" ) ) + { + return ConsoleLogger.LEVEL_WARN; + } + else if ( text.equals( "error" ) ) + { + return ConsoleLogger.LEVEL_ERROR; + } + else if ( text.equals( "fatal" ) ) + { + return ConsoleLogger.LEVEL_FATAL; + } + + return -1; + } + + /** + * Remove this method and all references when this code is verified. + * + * @param msg + */ + private void debug( String msg ) + { + } +} diff --git a/maven-embedder/src/main/java/org/apache/maven/cli/PrintStreamLogger.java b/maven-embedder/src/main/java/org/apache/maven/cli/PrintStreamLogger.java index 054467bd0e..2b26b31510 100644 --- a/maven-embedder/src/main/java/org/apache/maven/cli/PrintStreamLogger.java +++ b/maven-embedder/src/main/java/org/apache/maven/cli/PrintStreamLogger.java @@ -22,6 +22,7 @@ package org.apache.maven.cli; import java.io.PrintStream; import org.apache.maven.Maven; +import org.codehaus.plexus.logging.AbstractLogger; import org.codehaus.plexus.logging.Logger; /** @@ -30,8 +31,9 @@ import org.codehaus.plexus.logging.Logger; * @author Benjamin Bentmann */ public class PrintStreamLogger - implements Logger + extends AbstractLogger { + private final PrintStream out; private static final String FATAL_ERROR = "[FATAL] "; @@ -46,6 +48,8 @@ public class PrintStreamLogger public PrintStreamLogger( PrintStream out ) { + super( Logger.LEVEL_INFO, Maven.class.getName() ); + if ( out == null ) { throw new IllegalArgumentException( "output stream missing" ); @@ -54,11 +58,6 @@ public class PrintStreamLogger this.out = out; } - public void debug( String message ) - { - debug( message, null ); - } - public void debug( String message, Throwable throwable ) { if ( isDebugEnabled() ) @@ -73,11 +72,6 @@ public class PrintStreamLogger } } - public void info( String message ) - { - info( message, null ); - } - public void info( String message, Throwable throwable ) { if ( isInfoEnabled() ) @@ -92,11 +86,6 @@ public class PrintStreamLogger } } - public void warn( String message ) - { - warn( message, null ); - } - public void warn( String message, Throwable throwable ) { if ( isWarnEnabled() ) @@ -111,11 +100,6 @@ public class PrintStreamLogger } } - public void error( String message ) - { - error( message, null ); - } - public void error( String message, Throwable throwable ) { if ( isErrorEnabled() ) @@ -130,11 +114,6 @@ public class PrintStreamLogger } } - public void fatalError( String message ) - { - fatalError( message, null ); - } - public void fatalError( String message, Throwable throwable ) { if ( isFatalErrorEnabled() ) @@ -166,42 +145,4 @@ public class PrintStreamLogger return this; } - public String getName() - { - return Maven.class.getName(); - } - - public int getThreshold() - { - return 0; - } - - public boolean isDebugEnabled() - { - return false; - } - - public boolean isErrorEnabled() - { - return false; - } - - public boolean isFatalErrorEnabled() - { - return false; - } - - public boolean isInfoEnabled() - { - return false; - } - - public boolean isWarnEnabled() - { - return false; - } - - public void setThreshold( int arg0 ) - { - } }