You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Rob Bradlee <rb...@yahoo.com> on 2003/04/29 17:51:49 UTC

How do I call the log method in my extension of ActionServlet?

Hi,
To make a simple demonstration of extending the
ActionServlet class I'd like to override the log
function to write to my own log file.  I've
successfully opened the file in my override of the
init() function.  What do I do in my Struts app to
send a message to the log method?

Rob Bradlee


=====
Rob Bradlee
Java, C++, C, Perl, XML, OOAD, and Unix Training

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


Re: How do I call the log method in my extension of ActionServlet?

Posted by Natalie D Rassmann <na...@lmco.com>.
Why don't you just use one of the logging frameworks like log4J or JDK 1.4
logging API??

Natalie

Rob Bradlee wrote:

> Answering my own question:
>
> >From an Action subclass I did a:
>
> // Send a message to our ActionServlet log
>         ActionServlet daddy = getServlet();
>         daddy.log("This is a test");
>
> However, the message does NOT go to my override of the
> log method in my ActionServlet extension.  It DOES go
> to the Tomcat log with the app name pre-pended to it.
>
> I know my extension of ActionServlet is in play.  My
> override of init is being called and it opens my log
> file and writes to it.  So what's up?  Do I have the
> signature of log() correct?  Is it not really called?
>
> Below is my ActionServlet code:
>
> package com.mycompany.helloApp.framework;
>
> import javax.servlet.ServletException;
> import javax.servlet.UnavailableException;
> import org.apache.struts.action.ActionServlet;
> import java.io.*;
>
> /**
>  * This LoggingActionServlet overrides the default
> ActionServlet to provide private
>  * logging feature.
>  */
> public class LoggingActionServlet extends
> ActionServlet {
>
>         private FileOutputStream fileOut;
>         private DataOutputStream dataOut;
>         private BufferedWriter lineWriter;
>
>         public void init() throws ServletException {
>                 super.init();
>                 // open the file for logging
>                 try {
>                         fileOut = new FileOutputStream ("Lab2.txt");
>                         dataOut = new DataOutputStream(fileOut);
>                         lineWriter = new BufferedWriter (new
> OutputStreamWriter(dataOut));
>                         lineWriter.write("This is my log file\n");
>                         lineWriter.flush();
>                         }
>                 catch (IOException ioe)
>                         {
>                                 //should handle errors here
>                                 System.err.println("Error in restore: " +
> ioe.getMessage() + ioe);
>                         }
>
>         }
>    public void log(String message, int level) {
>            message = message + "hello from LAS";
>                 super.log(message,level);
>                 try {
>                 lineWriter.write( message  );
>                 lineWriter.flush();
>         }
>         catch (IOException ioe)
>                         {
>                                 //should handle errors here
>                                 System.err.println("Error in restore: " +
> ioe.getMessage() + ioe);
>                         }
>
>   }
> }
>
> --- Rob Bradlee <rb...@yahoo.com> wrote:
> > Hi,
> > To make a simple demonstration of extending the
> > ActionServlet class I'd like to override the log
> > function to write to my own log file.  I've
> > successfully opened the file in my override of the
> > init() function.  What do I do in my Struts app to
> > send a message to the log method?
> >
> > Rob Bradlee
> >
> >
> > =====
> > Rob Bradlee
> > Java, C++, C, Perl, XML, OOAD, and Unix Training
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> > struts-user-help@jakarta.apache.org
> >
>
> =====
> Rob Bradlee
> Java, C++, C, Perl, XML, OOAD, and Unix Training
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: How do I call the log method in my extension of ActionServlet?

Posted by Rob Bradlee <rb...@yahoo.com>.
Okay, answering my second question myself again (I
like talking to myself) - my problem - duh! - is that
I didn't call the log method correctly.  Forgot about
the second parameter.  Must be overloaded in
ActionServlet so I didn't get an error.  If I only had
a brain...

Good news is that it all works now.

--- Rob Bradlee <rb...@yahoo.com> wrote:
> Answering my own question:
> 
> From an Action subclass I did a:
> 
> // Send a message to our ActionServlet log
> 	ActionServlet daddy = getServlet();
> 	daddy.log("This is a test");
> 
> However, the message does NOT go to my override of
> the
> log method in my ActionServlet extension.  It DOES
> go
> to the Tomcat log with the app name pre-pended to
> it.
> 
> I know my extension of ActionServlet is in play.  My
> override of init is being called and it opens my log
> file and writes to it.  So what's up?  Do I have the
> signature of log() correct?  Is it not really
> called?
> 
> Below is my ActionServlet code:
> 
> package com.mycompany.helloApp.framework;
> 
> import javax.servlet.ServletException;
> import javax.servlet.UnavailableException;
> import org.apache.struts.action.ActionServlet;
> import java.io.*;
> 
> /**
>  * This LoggingActionServlet overrides the default
> ActionServlet to provide private
>  * logging feature.
>  */
> public class LoggingActionServlet extends
> ActionServlet {
> 
> 	private FileOutputStream fileOut;
> 	private DataOutputStream dataOut;
> 	private BufferedWriter lineWriter;
> 
> 	public void init() throws ServletException {
> 		super.init();
> 		// open the file for logging
> 		try {
> 			fileOut = new FileOutputStream ("Lab2.txt");
> 			dataOut = new DataOutputStream(fileOut);
> 			lineWriter = new BufferedWriter (new
> OutputStreamWriter(dataOut));
> 			lineWriter.write("This is my log file\n");
> 			lineWriter.flush();
> 			}
> 		catch (IOException ioe)
> 			{
> 				//should handle errors here
> 				System.err.println("Error in restore: " +
> ioe.getMessage() + ioe);
> 			}
> 
> 	}
>    public void log(String message, int level) {
> 	   message = message + "hello from LAS";
> 		super.log(message,level);
> 		try {
> 		lineWriter.write( message  );
> 		lineWriter.flush();
> 	}
> 	catch (IOException ioe)
> 			{
> 				//should handle errors here
> 				System.err.println("Error in restore: " +
> ioe.getMessage() + ioe);
> 			}
> 
>   }
> }
> 
> --- Rob Bradlee <rb...@yahoo.com> wrote:
> > Hi,
> > To make a simple demonstration of extending the
> > ActionServlet class I'd like to override the log
> > function to write to my own log file.  I've
> > successfully opened the file in my override of the
> > init() function.  What do I do in my Struts app to
> > send a message to the log method?
> > 
> > Rob Bradlee
> > 
> > 
> > =====
> > Rob Bradlee
> > Java, C++, C, Perl, XML, OOAD, and Unix Training
> > 
> >
>
---------------------------------------------------------------------
> > To unsubscribe, e-mail:
> > struts-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail:
> > struts-user-help@jakarta.apache.org
> > 
> 
> 
> =====
> Rob Bradlee
> Java, C++, C, Perl, XML, OOAD, and Unix Training
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> 


=====
Rob Bradlee
Java, C++, C, Perl, XML, OOAD, and Unix Training

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


Re: How do I call the log method in my extension of ActionServlet?

Posted by Rob Bradlee <rb...@yahoo.com>.
Answering my own question:

>From an Action subclass I did a:

// Send a message to our ActionServlet log
	ActionServlet daddy = getServlet();
	daddy.log("This is a test");

However, the message does NOT go to my override of the
log method in my ActionServlet extension.  It DOES go
to the Tomcat log with the app name pre-pended to it.

I know my extension of ActionServlet is in play.  My
override of init is being called and it opens my log
file and writes to it.  So what's up?  Do I have the
signature of log() correct?  Is it not really called?

Below is my ActionServlet code:

package com.mycompany.helloApp.framework;

import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import org.apache.struts.action.ActionServlet;
import java.io.*;

/**
 * This LoggingActionServlet overrides the default
ActionServlet to provide private
 * logging feature.
 */
public class LoggingActionServlet extends
ActionServlet {

	private FileOutputStream fileOut;
	private DataOutputStream dataOut;
	private BufferedWriter lineWriter;

	public void init() throws ServletException {
		super.init();
		// open the file for logging
		try {
			fileOut = new FileOutputStream ("Lab2.txt");
			dataOut = new DataOutputStream(fileOut);
			lineWriter = new BufferedWriter (new
OutputStreamWriter(dataOut));
			lineWriter.write("This is my log file\n");
			lineWriter.flush();
			}
		catch (IOException ioe)
			{
				//should handle errors here
				System.err.println("Error in restore: " +
ioe.getMessage() + ioe);
			}

	}
   public void log(String message, int level) {
	   message = message + "hello from LAS";
		super.log(message,level);
		try {
		lineWriter.write( message  );
		lineWriter.flush();
	}
	catch (IOException ioe)
			{
				//should handle errors here
				System.err.println("Error in restore: " +
ioe.getMessage() + ioe);
			}

  }
}

--- Rob Bradlee <rb...@yahoo.com> wrote:
> Hi,
> To make a simple demonstration of extending the
> ActionServlet class I'd like to override the log
> function to write to my own log file.  I've
> successfully opened the file in my override of the
> init() function.  What do I do in my Struts app to
> send a message to the log method?
> 
> Rob Bradlee
> 
> 
> =====
> Rob Bradlee
> Java, C++, C, Perl, XML, OOAD, and Unix Training
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> 


=====
Rob Bradlee
Java, C++, C, Perl, XML, OOAD, and Unix Training

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