You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by "Natra, Uday" <UN...@cooksys.com> on 2001/04/13 17:32:26 UTC

Logging Mechanism

Hi All,
The Logging Mechanism provided in the Struts framework is accessible to the
Java Components that have access to ActionServlet instance. Struts does not
define the Logging mechanism for the Components that are Isolated from the
Struts framework but are part of the application being developed using
struts framework. Can anybody tell me how they solved this problem or how
they implemented the Logging Mechanism ??

Thanks,
Uday.

RE: Logging Mechanism

Posted by Abraham Kang <ab...@infogain.com>.
Here is some code examples and instalation instructions
to help people get going with Log4j.

1.  Download the log4j binary distribution
2.  Extract the log4j.jar file and put it in the /WEB-INF/lib directory
    If you are using WebLogic add the log4j.jar to the WEBLOGIC_CLASSPATH
3.  Add this Log4jServlet to your classes directory (Also attached as
Log4jServlet).

package com.infogain.DOG.logging.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.log4j.*;
import com.infogain.DOG.logging.*;

public class Log4jServlet extends HttpServlet {

   static Category c = null;

  	public void init(ServletConfig config) throws ServletException {
     	super.init(config);

     	ServletContext context = config.getServletContext();

		log("#$#$ Context is " + context);

		String baseCategory = config.getInitParameter( Keys.BASE_CATEGORY );
		String basePriority = config.getInitParameter( Keys.BASE_PRIORITY );
      String initConfig = config.getInitParameter( Keys.WLPI_LOG4J_CONF );

      log( "WLPI_LOG4J_CONF = " + initConfig );

      if( initConfig == null )
         throw new ServletException( "Need an init-config provided by
parameter " + Keys.WLPI_LOG4J_CONF );

      Properties props = new Properties();
      try {
			log("getting log4j properties file");
			InputStream is = context.getResourceAsStream(initConfig);
         log("got log4j properties file");
			props.load(is);
			log("loaded config file into properties object");
         is.close();

		/******
         		// manipulate what needs to be manipulated :-)
			props.setProperty("log4j.appender.stdlog.File",
			getServletContext().getRealPath(messages.getMessage("Log.Qwickrate")) );
			props.setProperty("log4j.appender.ddrlog.File",
			getServletContext().getRealPath(messages.getMessage("Log.DDR"))  );
		*******/

			// configure the logging device
      	PropertyConfigurator.configure(props);

			if (baseCategory != null && basePriority != null) {
   			c = Category.getInstance(baseCategory);
				if (basePriority.equalsIgnoreCase("debug") )
   				c.setPriority(Priority.DEBUG);
				else if (basePriority.equalsIgnoreCase("info") )
   				c.setPriority(Priority.INFO);
				else if (basePriority.equalsIgnoreCase("warn") )
   				c.setPriority(Priority.WARN);
				else if (basePriority.equalsIgnoreCase("error") )
   				c.setPriority(Priority.ERROR);
				else if (basePriority.equalsIgnoreCase("fatal") )
   				c.setPriority(Priority.FATAL);
				else
					c.setPriority(Priority.DEBUG);

				if(c.isDebugEnabled())
					c.debug("Log4jServlet inited to priority: " + c.getPriority() );
			}
      } catch (IOException io) {
          System.out.println(io.getMessage());
      }
  	}


  /**
   * A very simple implementation of the service method, in
   * which we output the contents of a static html page
   */
  	public void service(HttpServletRequest req, HttpServletResponse res)
       throws IOException {
    	// Must set the content type first
    	res.setContentType("text/html");
    	// Now we can obtain a PrintWriter
    	PrintWriter out = res.getWriter();

    	out.println("<html><head><title>Log4j Servlet</title></head>");
    	out.println("<body><h1>The Log4j Servlet is up and
running</h1></body></html>");
  }
}

4.)  Drop the log4j.conf file in /WEB-INF (attached as
log4j_conf.properties).  This tells log4j to log to Std out and a file.


log4j.rootCategory=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.FileAppender
log4j.appender.stdout.File=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

5.)  Edit your web.xml file by adding the following:

  <servlet>
    <servlet-name>log4j</servlet-name>

<servlet-class>com.infogain.DOG.logging.servlet.Log4jServlet</servlet-class>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>log4j_conf</param-name>
      <param-value>/WEB-INF/log4j_conf.properties</param-value>
    </init-param>
    <init-param>
      <param-name>base_category</param-name>
	<!-- You will need to change this for your base Category -->
      <param-value>com.infogain.DOG</param-value>
    </init-param>
    <init-param>
      <param-name>base_priority</param-name>
      <param-value>debug</param-value>
    </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>

Enjoy log4j,
Abraham

> -----Original Message-----
> From: Craig R. McClanahan [mailto:craigmcc@apache.org]
> Sent: Friday, April 13, 2001 9:17 AM
> To: struts-dev@jakarta.apache.org
> Subject: Re: Logging Mechanism
>
>
>
>
> On Fri, 13 Apr 2001, Natra, Uday wrote:
>
> > Hi All,
> > The Logging Mechanism provided in the Struts framework is
> accessible to the
> > Java Components that have access to ActionServlet instance.
> Struts does not
> > define the Logging mechanism for the Components that are
> Isolated from the
> > Struts framework but are part of the application being developed using
> > struts framework. Can anybody tell me how they solved this
> problem or how
> > they implemented the Logging Mechanism ??
> >
> > Thanks,
> > Uday.
> >
>
> One approach to this you should investigate is the Log4J framework, which
> is also available at the Jakarta web site
> (http://jakarta.apache.org/log4j).
>
> I'm a little hesitant to mandate a particular logging technology in the
> framework itself (at least until the upcoming Java standard for logging
> APIs is completed -- religious wars are nothing compared to the emotions
> that logging technologies can raise :-).
>
> Craig
>
>
>

Re: Logging Mechanism

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Fri, 13 Apr 2001, Natra, Uday wrote:

> Hi All,
> The Logging Mechanism provided in the Struts framework is accessible to the
> Java Components that have access to ActionServlet instance. Struts does not
> define the Logging mechanism for the Components that are Isolated from the
> Struts framework but are part of the application being developed using
> struts framework. Can anybody tell me how they solved this problem or how
> they implemented the Logging Mechanism ??
> 
> Thanks,
> Uday.
> 

One approach to this you should investigate is the Log4J framework, which
is also available at the Jakarta web site
(http://jakarta.apache.org/log4j).

I'm a little hesitant to mandate a particular logging technology in the
framework itself (at least until the upcoming Java standard for logging
APIs is completed -- religious wars are nothing compared to the emotions
that logging technologies can raise :-).

Craig



Re: Logging Mechanism

Posted by Rajan Gupta <rg...@yahoo.com>.
I am using LOG4J instead of using the servlet default logging mechanism. I
wish Struts could also use LOG4J too.
--- "Natra, Uday" <UN...@cooksys.com> wrote:
> Hi All,
> The Logging Mechanism provided in the Struts framework is accessible to
> the
> Java Components that have access to ActionServlet instance. Struts does
> not
> define the Logging mechanism for the Components that are Isolated from
> the
> Struts framework but are part of the application being developed using
> struts framework. Can anybody tell me how they solved this problem or
> how
> they implemented the Logging Mechanism ??
> 
> Thanks,
> Uday.


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/