You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Laurent Duperval <ld...@yahoo.com> on 2004/10/06 16:18:22 UTC

Tracing action dispatching

Hi,

Is there a flag I can set so that Struts tells me what it's doing? I'm 
having a problem with one of my actions: when I click on it, I get a blank 
page and I don't understand why. I'd like to be able to see what decisions 
Sruts is taking in order to load my pages, to determine where I made a 
mistake in my config.

Thanks,

L


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Tracing action dispatching

Posted by Michael McGrady <mi...@michaelmcgrady.com>.
Laurent Duperval wrote:

> Hi,
>
> Is there a flag I can set so that Struts tells me what it's doing? I'm 
> having a problem with one of my actions: when I click on it, I get a 
> blank page and I don't understand why. I'd like to be able to see what 
> decisions Sruts is taking in order to load my pages, to determine 
> where I made a mistake in my config.
>
> Thanks,
>
> L 


You are better off setting up a logging facility as other suggest.  
However, if you just want a temporary solution prior to doing that, you 
can use something like the following class, which you ahve to configure 
to send a log to where you want it.

public class StdOut {
  private static final String slash       = File.separator;
  private static final String environment = Classpath.CLASSES;
  private static final String path        = "com" + slash + 
"crackwillow" + slash + "log" + slash+ "store" + slash;
  private static final String fileName    = "log.crackwillow";
  private static final String logPath     = environment + path;

  public static void log(Object input) {
    try {
      SaveOutput.start(environment + path + fileName);
      System.out.println(input.toString());
      new Exception();
    } catch (Exception e) {
      log("log.error",e.getMessage());
    } finally {
      SaveOutput.stop();
    }
  }

  public static void log(String fileName,
                         Object input) {
    fileName = logPath + fileName;
    try {
      SaveOutput.start(fileName);
      System.out.println(input.toString());
      new Exception();
    } catch (Exception e) {
      log("log.error",e.getMessage());
    } finally {
      SaveOutput.stop();
    }
  }

  public static void log(String input) {
    try {
      SaveOutput.start(environment + path + fileName);
      System.out.println(input);
      new Exception();
    } catch (Exception e) {
      log("log.error",e.getMessage());
    } finally {
      SaveOutput.stop();
    }
  }

  public static void log(String fileName,
                              String input) {
    fileName = logPath + fileName;
    try {
      SaveOutput.start(fileName);
      System.out.println(input);
      new Exception();
    } catch (Exception e) {
      log("log.error",e.getMessage());
    } finally {
      SaveOutput.stop();
    }
  }

  public static void log(boolean input) {
    try {
      SaveOutput.start(environment + path + fileName);
      System.out.println("" + input);
      new Exception();
    } catch (Exception e) {
      log("log.error",e.getMessage());
    } finally {
      SaveOutput.stop();
    }
  }

  public static void log(String fileName,
                              boolean input) {
    fileName = logPath + fileName;
    try {
      SaveOutput.start(fileName);
      System.out.println("" + input);
      new Exception();
    } catch (Exception e) {
      log("log.error",e.getMessage());
    } finally {
      SaveOutput.stop();
    }
  }
}

class SaveOutput
    extends PrintStream {

  static OutputStream logFile;
  static PrintWriter printer;
  static PrintStream oldStdOut;
  static PrintStream oldStdErr;

  /****************************************************
   * The print streams are out and err objects of the *
   * System class.  Println here overrides these.     *
   ****************************************************/
  SaveOutput(PrintStream ps) {
    super(ps);
  }

  // Starts copying stdout and
  // stderr to file
  public static void start(String file) {
    // Save the old settings
    oldStdOut = System.out;
    oldStdErr = System.err;
    // Create and open a log file
    try {
      logFile = new PrintStream(new BufferedOutputStream(new 
FileOutputStream(file, true)));
    } catch (FileNotFoundException fnfe) {
    }
    // Start redirecting the output
    System.setOut(new SaveOutput(System.out));
    System.setErr(new SaveOutput(System.err));
  }

  // Restores the original settings of
  // stdout and stderr
  public static void stop() {
    System.setOut(oldStdOut);
    System.setErr(oldStdErr);
    try {
      logFile.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void println(String input) {
    ((PrintStream)logFile).println(input);
  }

  private class Classpath {
  public static final String HERE         = 
Classpath.class.getClassLoader().getResource("com" + File.separator + 
"crackwillow" + File.separator + "classpath" + File.separator + 
"Classpath.class").getFile();
  public static final String CLASSES      = HERE.substring(0, 
HERE.lastIndexOf("com"));
  }
 
}


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Tracing action dispatching

Posted by Tuncay Baskan <tb...@gmail.com>.
On Wed, 06 Oct 2004 10:18:22 -0400, Laurent Duperval
<ld...@yahoo.com> wrote:
> Hi,
> 
> Is there a flag I can set so that Struts tells me what it's doing? I'm
> having a problem with one of my actions: when I click on it, I get a blank
> page and I don't understand why. I'd like to be able to see what decisions
> Sruts is taking in order to load my pages, to determine where I made a
> mistake in my config.
> 
> Thanks,
> 
> L
> 

Hello Laurent,

Using log4j with commons-logging is a good combination. Put log4j jar
file in your application's WEB-INF/lib directory and a
log4j.properties file in your WEB-INF/classes directory.

Below is a sample content for log4j.properties file:
----------
log4j.rootLogger=DEBUG, stdout

log4j.additivity.tr.com.intertech=false
log4j.additivity.org.apache.struts.action=false

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %C %M%n[%-5p] %m%n

log4j.logger.org.apache.struts=WARN, stdout
log4j.logger.org.apache.struts.action=DEBUG, stdout
log4j.logger.org.apache.commons=WARN, stdout
----------
I generally use Eclipse or WSAD, thus by directing all log4j output to
console you can watch what's going on in Eclipse's "console view".

You can change the debug output level for individual packages. Last
three lines define that I only want to see debug output from classes
in "org.apache.struts.action". (Since rootLogger is defined to be
DEBUG)

I'm not a log4j expert. For detailed information on log4j check 
http://logging.apache.org/log4j/docs/

And, here is a good quick start:
http://logging.apache.org/log4j/docs/manual.html

Regards,
/tb.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org