logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Moorer <tcm...@yahoo.com>
Subject Re: Default Initialization Servlet: Determine Hostname?
Date Mon, 11 May 2009 17:39:52 GMT
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 <Douglas_E_Wegscheid@whirlpool.com>
To: Log4J Users List <log4j-user@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."



      
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message