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:[EMAIL PROTECTED]]
> Sent: Friday, April 13, 2001 9:17 AM
> To: [EMAIL PROTECTED]
> 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
>
>
>

log4j_conf.properties

Log4jServlet.java

Keys.java

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>

  <context-param>
    <param-name>weblogic.jsp.keepgenerated</param-name>
    <param-value>true</param-value>
  </context-param>

  <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>wlpi_log4j_conf</param-name>
      <param-value>/WEB-INF/wlpi_log4j_conf.properties</param-value>
    </init-param>
    <init-param>
      <param-name>base_category</param-name>
      <param-value>com.infogain.DOG</param-value>
    </init-param>
    <init-param>
      <param-name>base_priority</param-name>
      <param-value>info</param-value>
    </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Standard Action Servlet Configuration (with debugging) -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>application</param-name>
      <param-value>ApplicationResources</param-value>
    </init-param>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>validate</param-name>
      <param-value>true</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>


  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>


  <!-- The Usual Welcome File List -->
  <welcome-file-list>
    <welcome-file>SnoopServlet.jsp, index.html</welcome-file>
  </welcome-file-list>

  <!-- Example Application Tag Library Descriptor
  <taglib>
    <taglib-uri>/WEB-INF/app.tld</taglib-uri>
    <taglib-location>/WEB-INF/app.tld</taglib-location>
  </taglib>
  -->

  <!-- Struts Tag Library Descriptors -->
  <taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>

  <taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>

  <security-constraint>
    <web-resource-collection>
       <web-resource-name>
         wlpiTest 
       </web-resource-name>
       <url-pattern>
          /*
       </url-pattern> 
       <http-method>POST</http-method>
       <http-method>GET</http-method>
    </web-resource-collection>
 	 <auth-constraint>
 
		 <role-name>
          wlpiUsers
       </role-name>
		 <role-name>
          wlpiAdministrators
       </role-name>
    </auth-constraint>
	 <user-data-constraint>
            <description>SSL not required</description>
            <transport-guarantee>NONE</transport-guarantee>
    </user-data-constraint>
  </security-constraint>

  <login-config>
    <auth-method>
       FORM
    </auth-method>
    <form-login-config>
      <form-login-page>
       /logonWebApp.jsp
      </form-login-page>
      <form-error-page>
       /invalidLogon.html
      </form-error-page>
    </form-login-config>
  </login-config>

</web-app>

Reply via email to