git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271823 13f79535-47bb-0310-9956-ffa450edef68master
@@ -0,0 +1,265 @@ | |||||
/* | |||||
* Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
* | |||||
* This software is published under the terms of the Apache Software License | |||||
* version 1.1, a copy of which has been included with this distribution in | |||||
* the LICENSE.txt file. | |||||
*/ | |||||
package org.apache.myrmidon.frontends; | |||||
import org.apache.avalon.framework.ExceptionUtil; | |||||
import org.apache.avalon.framework.logger.Logger; | |||||
/** | |||||
* A basic logger that just prints out messages to <code>System.out</code>. | |||||
* | |||||
* @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||||
* @version $Revision$ $Date$ | |||||
*/ | |||||
class BasicLogger | |||||
implements Logger | |||||
{ | |||||
public final static int LEVEL_DEBUG = 0; | |||||
public final static int LEVEL_INFO = 1; | |||||
public final static int LEVEL_WARN = 2; | |||||
public final static int LEVEL_ERROR = 3; | |||||
public final static int LEVEL_FATAL = 4; | |||||
/** | |||||
* The string prefixed to all log messages. | |||||
*/ | |||||
private final String m_prefix; | |||||
/** | |||||
* The level at which messages start becoming logged. | |||||
*/ | |||||
private final int m_logLevel; | |||||
/** | |||||
* Create a logger that has specified prefix and is logging | |||||
* at specified level. | |||||
*/ | |||||
public BasicLogger( final String prefix, final int logLevel ) | |||||
{ | |||||
m_prefix = prefix; | |||||
m_logLevel = logLevel; | |||||
} | |||||
/** | |||||
* Log a debug message. | |||||
* | |||||
* @param message the message | |||||
*/ | |||||
public void debug( final String message ) | |||||
{ | |||||
if( isDebugEnabled() ) | |||||
{ | |||||
output( message, null ); | |||||
} | |||||
} | |||||
/** | |||||
* Log a debug message. | |||||
* | |||||
* @param message the message | |||||
* @param throwable the throwable | |||||
*/ | |||||
public void debug( final String message, final Throwable throwable ) | |||||
{ | |||||
if( isDebugEnabled() ) | |||||
{ | |||||
output( message, throwable ); | |||||
} | |||||
} | |||||
/** | |||||
* Determine if messages of priority "debug" will be logged. | |||||
* | |||||
* @return true if "debug" messages will be logged | |||||
*/ | |||||
public boolean isDebugEnabled() | |||||
{ | |||||
return m_logLevel <= LEVEL_DEBUG; | |||||
} | |||||
/** | |||||
* Log a info message. | |||||
* | |||||
* @param message the message | |||||
*/ | |||||
public void info( final String message ) | |||||
{ | |||||
if( isInfoEnabled() ) | |||||
{ | |||||
output( message, null ); | |||||
} | |||||
} | |||||
/** | |||||
* Log a info message. | |||||
* | |||||
* @param message the message | |||||
* @param throwable the throwable | |||||
*/ | |||||
public void info( final String message, final Throwable throwable ) | |||||
{ | |||||
if( isInfoEnabled() ) | |||||
{ | |||||
output( message, throwable ); | |||||
} | |||||
} | |||||
/** | |||||
* Determine if messages of priority "info" will be logged. | |||||
* | |||||
* @return true if "info" messages will be logged | |||||
*/ | |||||
public boolean isInfoEnabled() | |||||
{ | |||||
return m_logLevel <= LEVEL_INFO; | |||||
} | |||||
/** | |||||
* Log a warn message. | |||||
* | |||||
* @param message the message | |||||
*/ | |||||
public void warn( final String message ) | |||||
{ | |||||
if( isWarnEnabled() ) | |||||
{ | |||||
output( message, null ); | |||||
} | |||||
} | |||||
/** | |||||
* Log a warn message. | |||||
* | |||||
* @param message the message | |||||
* @param throwable the throwable | |||||
*/ | |||||
public void warn( final String message, final Throwable throwable ) | |||||
{ | |||||
if( isWarnEnabled() ) | |||||
{ | |||||
output( message, throwable ); | |||||
} | |||||
} | |||||
/** | |||||
* Determine if messages of priority "warn" will be logged. | |||||
* | |||||
* @return true if "warn" messages will be logged | |||||
*/ | |||||
public boolean isWarnEnabled() | |||||
{ | |||||
return m_logLevel <= LEVEL_WARN; | |||||
} | |||||
/** | |||||
* Log a error message. | |||||
* | |||||
* @param message the message | |||||
*/ | |||||
public void error( final String message ) | |||||
{ | |||||
if( isErrorEnabled() ) | |||||
{ | |||||
output( message, null ); | |||||
} | |||||
} | |||||
/** | |||||
* Log a error message. | |||||
* | |||||
* @param message the message | |||||
* @param throwable the throwable | |||||
*/ | |||||
public void error( final String message, final Throwable throwable ) | |||||
{ | |||||
if( isErrorEnabled() ) | |||||
{ | |||||
output( message, throwable ); | |||||
} | |||||
} | |||||
/** | |||||
* Determine if messages of priority "error" will be logged. | |||||
* | |||||
* @return true if "error" messages will be logged | |||||
*/ | |||||
public boolean isErrorEnabled() | |||||
{ | |||||
return m_logLevel <= LEVEL_ERROR; | |||||
} | |||||
/** | |||||
* Log a fatalError message. | |||||
* | |||||
* @param message the message | |||||
*/ | |||||
public void fatalError( final String message ) | |||||
{ | |||||
if( isFatalErrorEnabled() ) | |||||
{ | |||||
output( message, null ); | |||||
} | |||||
} | |||||
/** | |||||
* Log a fatalError message. | |||||
* | |||||
* @param message the message | |||||
* @param throwable the throwable | |||||
*/ | |||||
public void fatalError( final String message, final Throwable throwable ) | |||||
{ | |||||
if( isFatalErrorEnabled() ) | |||||
{ | |||||
output( message, throwable ); | |||||
} | |||||
} | |||||
/** | |||||
* Determine if messages of priority "fatalError" will be logged. | |||||
* | |||||
* @return true if "fatalError" messages will be logged | |||||
*/ | |||||
public boolean isFatalErrorEnabled() | |||||
{ | |||||
return m_logLevel <= LEVEL_FATAL; | |||||
} | |||||
/** | |||||
* Create a new child logger. | |||||
* The name of the child logger is [current-loggers-name].[passed-in-name] | |||||
* | |||||
* @param name the subname of this logger | |||||
* @return the new logger | |||||
* @exception IllegalArgumentException if name has an empty element name | |||||
*/ | |||||
public Logger getChildLogger( final String name ) | |||||
{ | |||||
return new BasicLogger( m_prefix + "." + name, m_logLevel ); | |||||
} | |||||
/** | |||||
* Utility method to output messages. | |||||
*/ | |||||
private void output( final String message, final Throwable throwable ) | |||||
{ | |||||
final StringBuffer sb = new StringBuffer( m_prefix ); | |||||
if( null != message ) | |||||
{ | |||||
sb.append( message ); | |||||
} | |||||
if( null != throwable ) | |||||
{ | |||||
final String stackTrace = ExceptionUtil.printStackTrace( throwable, 8, true, true ); | |||||
sb.append( stackTrace ); | |||||
} | |||||
System.out.println( sb.toString() ); | |||||
} | |||||
} |
@@ -21,14 +21,7 @@ import org.apache.avalon.excalibur.i18n.Resources; | |||||
import org.apache.avalon.framework.CascadingException; | import org.apache.avalon.framework.CascadingException; | ||||
import org.apache.avalon.framework.ExceptionUtil; | import org.apache.avalon.framework.ExceptionUtil; | ||||
import org.apache.avalon.framework.logger.AbstractLogEnabled; | import org.apache.avalon.framework.logger.AbstractLogEnabled; | ||||
import org.apache.avalon.framework.logger.LogKitLogger; | |||||
import org.apache.avalon.framework.parameters.Parameters; | import org.apache.avalon.framework.parameters.Parameters; | ||||
import org.apache.log.Hierarchy; | |||||
import org.apache.log.LogTarget; | |||||
import org.apache.log.Logger; | |||||
import org.apache.log.Priority; | |||||
import org.apache.log.format.PatternFormatter; | |||||
import org.apache.log.output.io.StreamTarget; | |||||
import org.apache.myrmidon.Constants; | import org.apache.myrmidon.Constants; | ||||
import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
import org.apache.myrmidon.interfaces.embeddor.Embeddor; | import org.apache.myrmidon.interfaces.embeddor.Embeddor; | ||||
@@ -52,7 +45,6 @@ public class CLIMain | |||||
ResourceManager.getPackageResources( CLIMain.class ); | ResourceManager.getPackageResources( CLIMain.class ); | ||||
private final String DEFAULT_EMBEDDOR_CLASS = "org.apache.myrmidon.components.embeddor.DefaultEmbeddor"; | private final String DEFAULT_EMBEDDOR_CLASS = "org.apache.myrmidon.components.embeddor.DefaultEmbeddor"; | ||||
private final static String PATTERN = "[%8.8{category}] %{message}\\n%{throwable}"; | |||||
//defines for the Command Line options | //defines for the Command Line options | ||||
private final static int HELP_OPT = 'h'; | private final static int HELP_OPT = 'h'; | ||||
@@ -107,7 +99,8 @@ public class CLIMain | |||||
private boolean m_dryRun = false; | private boolean m_dryRun = false; | ||||
///Log level to use | ///Log level to use | ||||
private static Priority m_priority = Priority.WARN; | |||||
//private static Priority m_priority = Priority.WARN; | |||||
private static int m_priority = BasicLogger.LEVEL_WARN; | |||||
/** | /** | ||||
* Main entry point called to run standard Myrmidon. | * Main entry point called to run standard Myrmidon. | ||||
@@ -259,10 +252,10 @@ public class CLIMain | |||||
m_priority = mapLogLevel( option.getArgument() ); | m_priority = mapLogLevel( option.getArgument() ); | ||||
break; | break; | ||||
case VERBOSE_OPT: | case VERBOSE_OPT: | ||||
m_priority = Priority.INFO; | |||||
m_priority = BasicLogger.LEVEL_INFO; | |||||
break; | break; | ||||
case QUIET_OPT: | case QUIET_OPT: | ||||
m_priority = Priority.ERROR; | |||||
m_priority = BasicLogger.LEVEL_ERROR; | |||||
break; | break; | ||||
case INCREMENTAL_OPT: | case INCREMENTAL_OPT: | ||||
@@ -401,7 +394,6 @@ public class CLIMain | |||||
{ | { | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
@@ -412,7 +404,7 @@ public class CLIMain | |||||
{ | { | ||||
// Build the message | // Build the message | ||||
final String message; | final String message; | ||||
if( m_priority.isLowerOrEqual( Priority.INFO ) ) | |||||
if( m_priority <= BasicLogger.LEVEL_INFO ) | |||||
{ | { | ||||
// Verbose mode - include the stack traces | // Verbose mode - include the stack traces | ||||
message = ExceptionUtil.printStackTrace( throwable, 5, true, true ); | message = ExceptionUtil.printStackTrace( throwable, 5, true, true ); | ||||
@@ -494,7 +486,8 @@ public class CLIMain | |||||
private void prepareLogging() throws Exception | private void prepareLogging() throws Exception | ||||
{ | { | ||||
//handle logging... | //handle logging... | ||||
enableLogging( new LogKitLogger( createLogger( m_priority ) ) ); | |||||
final BasicLogger logger = new BasicLogger( "myrmidon", m_priority ); | |||||
enableLogging( logger ); | |||||
} | } | ||||
private void shutdownEmbeddor( final Embeddor embeddor ) | private void shutdownEmbeddor( final Embeddor embeddor ) | ||||
@@ -562,36 +555,30 @@ public class CLIMain | |||||
/** | /** | ||||
* Sets the log level. | * Sets the log level. | ||||
*/ | */ | ||||
private Priority mapLogLevel( final String logLevel ) throws Exception | |||||
private int mapLogLevel( final String logLevel ) | |||||
throws Exception | |||||
{ | { | ||||
final String logLevelCapitalized = logLevel.toUpperCase(); | final String logLevelCapitalized = logLevel.toUpperCase(); | ||||
final Priority priority = Priority.getPriorityForName( logLevelCapitalized ); | |||||
if( !priority.getName().equals( logLevelCapitalized ) ) | |||||
if( "DEBUG".equals( logLevelCapitalized ) ) | |||||
{ | |||||
return BasicLogger.LEVEL_DEBUG; | |||||
} | |||||
else if( "INFO".equals( logLevelCapitalized ) ) | |||||
{ | |||||
return BasicLogger.LEVEL_INFO; | |||||
} | |||||
else if( "WARN".equals( logLevelCapitalized ) ) | |||||
{ | |||||
return BasicLogger.LEVEL_WARN; | |||||
} | |||||
else if( "ERROR".equals( logLevelCapitalized ) ) | |||||
{ | |||||
return BasicLogger.LEVEL_ERROR; | |||||
} | |||||
else | |||||
{ | { | ||||
final String message = REZ.getString( "bad-loglevel.error", logLevel ); | final String message = REZ.getString( "bad-loglevel.error", logLevel ); | ||||
throw new Exception( message ); | throw new Exception( message ); | ||||
} | } | ||||
return priority; | |||||
} | |||||
/** | |||||
* Create Logger of appropriate log-level. | |||||
* | |||||
* @param priority the log-level | |||||
* @return the logger | |||||
* @exception Exception if an error occurs | |||||
*/ | |||||
private Logger createLogger( final Priority priority ) | |||||
throws Exception | |||||
{ | |||||
final Logger logger = Hierarchy.getDefaultHierarchy().getLoggerFor( "myrmidon" ); | |||||
final PatternFormatter formatter = new PatternFormatter( PATTERN ); | |||||
final StreamTarget target = new StreamTarget( System.out, formatter ); | |||||
logger.setLogTargets( new LogTarget[]{target} ); | |||||
logger.setPriority( priority ); | |||||
return logger; | |||||
} | } | ||||
} | } |