o Fixed file logger to properly log exception stack traces

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@804164 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2009-08-14 11:17:27 +00:00
parent 988246330f
commit d8b2070350
3 changed files with 155 additions and 178 deletions

View File

@ -1,4 +1,6 @@
package org.apache.maven.embedder;/*
package org.apache.maven.embedder;
/*
* 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
@ -25,84 +27,12 @@
* @author <a href="mailto:dev@avalon.codehaus.org">Avalon Development Team</a>
*/
public final class MavenEmbedderConsoleLogger
extends AbstractMavenEmbedderLogger
extends MavenEmbedderPrintStreamLogger
{
public void debug( String message,
Throwable throwable )
{
if ( isDebugEnabled() )
{
System.out.print( "[DEBUG] " );
System.out.println( message );
if ( null != throwable )
public MavenEmbedderConsoleLogger()
{
throwable.printStackTrace( System.out );
}
}
super( System.out );
}
public void info( String message,
Throwable throwable )
{
if ( isInfoEnabled() )
{
System.out.print( "[INFO] " );
System.out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void warn( String message,
Throwable throwable )
{
if ( isWarnEnabled() )
{
System.out.print( "[WARNING] " );
System.out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void error( String message,
Throwable throwable )
{
if ( isErrorEnabled() )
{
System.out.print( "[ERROR] " );
System.out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void fatalError( String message,
Throwable throwable )
{
if ( isFatalErrorEnabled() )
{
System.out.print( "[ERROR] " );
System.out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void close()
{
}
}

View File

@ -20,122 +20,32 @@
*/
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.PrintStream;
/**
* @author Jason van Zyl
* @todo document the need to call close() once successfully constructed, otherwise file handles can be leaked. Might be good to add a finalizer too, just in case.
*/
public final class MavenEmbedderFileLogger
extends AbstractMavenEmbedderLogger
extends MavenEmbedderPrintStreamLogger
{
private PrintWriter log;
public MavenEmbedderFileLogger( File logFile )
{
super( openStream( logFile ) );
}
private static PrintStream openStream( File logFile )
{
try
{
this.log = new PrintWriter( new FileWriter( logFile ) ); // platform encoding
return new PrintStream( logFile );
}
catch ( IOException e )
catch ( FileNotFoundException e )
{
// The client must make sure the file is valid.
// TODO: [BP] would just throwing the IOE be better? We can't just ignore it, since that would give misleading NPE's later
throw new RuntimeException( "The embedder was unable to write to the specified log file: " + logFile, e );
throw new IllegalArgumentException( "Cannot open specified log file " + logFile, e );
}
}
public void debug( String message,
Throwable throwable )
{
if ( isDebugEnabled() )
{
print( "[DEBUG] " );
println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void info( String message,
Throwable throwable )
{
if ( isInfoEnabled() )
{
print( "[INFO] " );
println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void warn( String message,
Throwable throwable )
{
if ( isWarnEnabled() )
{
print( "[WARNING] " );
println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void error( String message,
Throwable throwable )
{
if ( isErrorEnabled() )
{
print( "[ERROR] " );
println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
public void fatalError( String message,
Throwable throwable )
{
if ( isFatalErrorEnabled() )
{
print( "[ERROR] " );
println( message );
if ( null != throwable )
{
throwable.printStackTrace( System.out );
}
}
}
protected void print( String message )
{
log.print( message );
}
protected void println( String message )
{
log.println( message );
}
public void close()
{
log.flush();
log.close();
}
}

View File

@ -0,0 +1,137 @@
package org.apache.maven.embedder;
/*
* 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.io.PrintStream;
/**
* Logs to a user-supplied {@link PrintStream}.
*
* @author Benjamin Bentmann
*/
public class MavenEmbedderPrintStreamLogger
extends AbstractMavenEmbedderLogger
{
private final PrintStream out;
private static final String FATAL_ERROR = "[FATAL] ";
private static final String ERROR = "[ERROR] ";
private static final String WARNING = "[WARNING] ";
private static final String INFO = "[INFO] ";
private static final String DEBUG = "[DEBUG] ";
public MavenEmbedderPrintStreamLogger( PrintStream out )
{
if ( out == null )
{
throw new IllegalArgumentException( "output stream missing" );
}
this.out = out;
}
public void debug( String message, Throwable throwable )
{
if ( isDebugEnabled() )
{
out.print( DEBUG );
out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( out );
}
}
}
public void info( String message, Throwable throwable )
{
if ( isInfoEnabled() )
{
out.print( INFO );
out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( out );
}
}
}
public void warn( String message, Throwable throwable )
{
if ( isWarnEnabled() )
{
out.print( WARNING );
out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( out );
}
}
}
public void error( String message, Throwable throwable )
{
if ( isErrorEnabled() )
{
out.print( ERROR );
out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( out );
}
}
}
public void fatalError( String message, Throwable throwable )
{
if ( isFatalErrorEnabled() )
{
out.print( FATAL_ERROR );
out.println( message );
if ( null != throwable )
{
throwable.printStackTrace( out );
}
}
}
public void close()
{
if ( out == System.out || out == System.err )
{
out.flush();
}
else
{
out.close();
}
}
}