You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by al...@locus.apache.org on 2000/06/23 01:10:25 UTC

cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/logging TomcatLogger.java

alex        00/06/22 16:10:25

  Modified:    src/share/org/apache/tomcat/logging TomcatLogger.java
  Log:
  Made throwableToString public and static, and made it print the nested
  "root cause" of ServletException when available.  Intended to be used
  by any code that needs a stack trace (todo: replace all
  printStackTrace calls in other files)
  
  Fixed bug: failed to print timestamp correctly in "custom" logs
  
  Custom logs and non-custom logs output is now consistent, except for
  bracketing tag in non-custom
  
  Does anyone actually use non-custom logs?
  
  Revision  Changes    Path
  1.7       +57 -25    jakarta-tomcat/src/share/org/apache/tomcat/logging/TomcatLogger.java
  
  Index: TomcatLogger.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/logging/TomcatLogger.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TomcatLogger.java	2000/04/05 02:52:15	1.6
  +++ TomcatLogger.java	2000/06/22 23:10:25	1.7
  @@ -61,6 +61,7 @@
   import java.io.PrintWriter;
   
   import java.util.Date;
  +import javax.servlet.ServletException;
   
   import org.apache.tomcat.util.Queue;
   
  @@ -73,6 +74,36 @@
   public class TomcatLogger extends Logger {
   
       /**
  +     * Converts a Throwable to a printable stack trace, including the
  +     * nested root cause for a ServletException if applicable
  +     * 
  +     * @param t any Throwable, or ServletException, or null
  +     **/
  +    public static String throwableToString( Throwable t ) {
  +	StringWriter sw = new StringWriter();
  +	PrintWriter w = new PrintWriter(sw);
  +	
  +	if (t != null) {
  +	    t.printStackTrace(w);
  +	    if (t instanceof ServletException) {
  +		Throwable cause = ((ServletException)t).getRootCause();
  +		if (cause != null) {
  +		    // we could use a StringManager here to get the
  +		    // localized translation of "Root cause" , but
  +		    // since it's going into a log, no user will see
  +		    // it, and it's desirable that the log file is
  +		    // predictable, so just use English
  +		    String rootmsg = "Root cause:";
  +		    w.println(rootmsg);
  +		    cause.printStackTrace(w);
  +		}
  +	    }
  +	}
  +	w.flush();	// couldn't hurt...
  +	return sw.toString();
  +    }
  +    
  +    /**
        * This is an entry that is created in response to every
        * Logger.log(...) call.
        */
  @@ -98,41 +129,42 @@
   	    return TomcatLogger.this.sink;
   	}
   
  -	private String throwableToString( Throwable t ) {
  -	    StringWriter sw = new StringWriter();
  -	    PrintWriter w = new PrintWriter(sw);
  -
  -	    if (t != null)
  -		t.printStackTrace(w);
  -	    return sw.toString();
  -	}
  -	
   	/**
   	 * Format the log message nicely into a string.
   	 */
   	public String toString() {
  -
  -	    // custom output - do nothing
  -	    if( TomcatLogger.this.custom )
  -		return (t==null) ? message : message + " " + throwableToString( t );
   	    
  -	    StringWriter sw = new StringWriter();
  -	    PrintWriter w = new PrintWriter(sw);
  +	    StringBuffer val = new StringBuffer();
   
  -	    w.print("<"+TomcatLogger.this.getName()+"> ");
  -	    if (TomcatLogger.this.timeStamp)
  -		w.print(new Date(date).toString());
  -	    w.print(' ');
  +	    // custom output, now with timestamp
  +	    // does anyone actually use non-custom logs?
   
  +	    if( !TomcatLogger.this.custom ) {
  +		val.append("<");
  +		val.append(TomcatLogger.this.getName());
  +		val.append("> ");
  +	    }
  +
  +	    if (TomcatLogger.this.timeStamp) {
  +		val.append(new Date(date).toString());
  +		val.append(" ");
  +	    }
  +
   	    if (message != null)
  -		w.print(message);
  -	    
  -	    if (t != null)
  -		t.printStackTrace(w);
  +		val.append(message);
   
  -	    w.println("</"+TomcatLogger.this.getName()+"> ");
  +	    if (t != null) {
  +		val.append(" ");
  +		val.append(throwableToString( t ));
  +	    }
  +
  +	    if( !TomcatLogger.this.custom ) {
  +		val.append("</");
  +		val.append(TomcatLogger.this.getName());
  +		val.append("> ");
  +	    }
   
  -	    return sw.toString();
  +	    return val.toString();	    
   	}
       }
   
  
  
  

Re: cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/logging TomcatLogger.java

Posted by Costin Manolache <co...@eng.sun.com>.
>   Fixed bug: failed to print timestamp correctly in "custom" logs
>
>   Custom logs and non-custom logs output is now consistent, except for
>   bracketing tag in non-custom
>
>   Does anyone actually use non-custom logs?

Please make sure you have a way to turn timestamp off - or at least output it as
a long, not as a formated date. Formating dates is one of the slowest operations, and
a big hotspot.

If you have time - it would be great to have few additional log methods. Some time ago
Sam Ruby proposed a solution - to have multiple overloaded log(), that will allow
to do things like log( msgType, param1, param2, param3).
The log method will then output the message - either as a text ( using ResourceBundle
syntax to allow insertion of param inside the internationalized string ) or as
XML ( based on config ).

Costin