You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Thomas Moorer <tc...@yahoo.com> on 2009/05/11 17:33:02 UTC

Default Initialization Servlet: Determine Hostname?

Hi All,

I
need help with this one. What I am trying to do is set up a default
initialization servlet as explained in the manual http://logging.apache.org/log4j/1.2/manual.html (see below)

What
I would like to do is determine in the Log4jInit servlet's init method
the hostname of the server and load the property file accordingly. For
example if the hostname is localhost I would load the property file
that writes to a console and if the hostname is my production url load
the property file that writes to a lof file....

I have been searching for a couple of days on this now without luck. Anyone have any ideas on how I can do this?



Initialization servlet
It is also possible to use a special servlet for log4j
initialization. Here is an example,
package com.foo;

import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;

public class Log4jInit extends HttpServlet {

  public
  void init() {
    String prefix =  getServletContext().getRealPath("/");
    String file = getInitParameter("log4j-init-file");
    // if the log4j-init-file is not set, then no point in trying
    if(file != null) {
      PropertyConfigurator.configure(prefix+file);
    }
  }

  public
  void doGet(HttpServletRequest req, HttpServletResponse res) {
  }
}
 
Define the following servlet in the web.xml file for your web-application.
  <servlet>
    <servlet-name>log4j-init</servlet-name>
    <servlet-class>com.foo.Log4jInit</servlet-class>

    <init-param>
      <param-name>log4j-init-file</param-name>
      <param-value>WEB-INF/classes/log4j.lcf</param-value>
    </init-param>

<load-on-startup>1</load-on-startup>
  </servlet>
 
Writing an initialization servlet is the most flexible way for
initializing log4j. There are no constraints on the code you can place
in the init() method of the servlet.
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
email: tcm527@yahoo.com



      

Re: Default Initialization Servlet: Determine Hostname?

Posted by Thomas Moorer <tc...@yahoo.com>.
Brilliant!  I knew it would be something pretty straightforward...

I case anyone else is interested here's what I came up with:

web.xml

  <servlet>
    <description></description>
    <display-name>Log4jInit</display-name>
    <servlet-name>Log4jInit</servlet-name>
    <servlet-class>core.app.controllers.Log4jInit</servlet-class>
    <init-param>
      <param-name>log4j-dev</param-name>
      <param-value>WEB-INF/classes/log4j-dev.properties</param-value>
    </init-param>
    <init-param>
      <param-name>log4j-prd</param-name>
      <param-value>WEB-INF/classes/log4j-prd.properties</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Log4jInit</servlet-name>
    <url-pattern>/Log4jInit</url-pattern>
  </servlet-mapping>

Log4jInit servlet

    public void init(ServletConfig config) throws ServletException {
        try {
            InetAddress localhost = InetAddress.getLocalHost();
            String hostname = localhost.getHostName();
            String parmName = "log4j-dev";
            if (hostname.contains("...dev") || hostname.contains("...prd")) {
                parmName = "log4j-prd";
            }
            String propFile = config.getServletContext().getRealPath("/"+config.getInitParameter(parmName));
            //System.out.println(this.getClass()+": hostname='"+hostname+"'");
            //System.out.println(this.getClass()+": propFile='"+propFile+"'");
            if(propFile!= null) {
                PropertyConfigurator.configure(propFile);
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
email: tcm527@yahoo.com




________________________________
From: Douglas E Wegscheid <Do...@whirlpool.com>
To: Log4J Users List <lo...@logging.apache.org>
Cc: log4j-user@logging.apache.org
Sent: Monday, May 11, 2009 12:59:14 PM
Subject: Re: Default Initialization Servlet: Determine Hostname?

this should be pretty straightforward: you can determine hostname with 
InetAddress.getLocalHost(). after that, it's just code.

you probably won't get an address of "localhost" back, though. localhost 
usually (always?) maps to 127.0.0.1...

Douglas E Wegscheid
Lead Technical Analyst, Whirlpool Corporation
(269)-923-5278

"A wrong note played hesitatingly is a wrong note. A wrong note played 
with conviction is interpretation."



      

Re: Default Initialization Servlet: Determine Hostname?

Posted by Douglas E Wegscheid <Do...@whirlpool.com>.
this should be pretty straightforward: you can determine hostname with 
InetAddress.getLocalHost(). after that, it's just code.

you probably won't get an address of "localhost" back, though. localhost 
usually (always?) maps to 127.0.0.1...

Douglas E Wegscheid
Lead Technical Analyst, Whirlpool Corporation
(269)-923-5278

"A wrong note played hesitatingly is a wrong note. A wrong note played 
with conviction is interpretation."