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 Jacob Kjome <ho...@visi.com> on 2007/05/25 19:56:05 UTC

Re: Log4j repository selector

I take it you are using Log4j-1.3 alpha?  If so, look here...

http://logging.apache.org/log4j/docs/api-1.3/org/apache/log4j/selector/ContextJNDISelector.html


Jake

Quoting Sandip Prashar <sp...@humbersys.com>:

> Could you please provide sample, how to configure in RepositorySelector in
> log4jinit class, I am using the ContextJNDISelector.
>
>
>
>
>
> Thanks,
>
>
>
> Sandip Prashar
>
>
>
> Principal Consultant
>
> -----------------------------------------------------------
>
> HumberSys Consultancy Services (HCS)
>
> Tel:           647-500-7376
>
> Email:         <ma...@humbersys.com> sprashar@humbersys.com
>
> Website:     <http://www.humbersys.com/> www.humbersys.com
>
> ------------------------------------------------
>
>
>
> "Unlocking Information Technology"
>
>
>
>




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


RE: Log4j repository selector

Posted by Jacob Kjome <ho...@visi.com>.
I don't think I wrote JNDRS.  Where did you get it?  It was probably 
from Ceki's original example.  In any case, I see at least 3 issues....

1.  I'm pretty sure PropertyConfigurator.configure(String) expects a 
File path.  What you are passing to it isn't one.  You should use the 
servlet context or the classloader to load the resource and use that 
URL to pass to PropertyConfigurator.configure(URL).

2.  You are setting the selector after you perform 
configuration.  So, the configuration will have been set on the 
default logger repository, assuming some other webapp didn't already 
set the selector.  And there's no point in holding onto the 
guard.  Once the selector is set once, there's no point in having the 
ability to set it again.  You should also keep the configuration 
try/catch separate from the repository setting try/catch.  As I 
mentiond previously, if the repository has already been set by 
another application (or by the server itself), your attempt will 
fail. If you put the configuration code in the same try/catch, both 
will fail.  Failing to set the repository selector because it's 
already been set is perfectly fine and you can ignore the 
failure.  But you still want to perform configuration on your own 
logger repository no matter what.

3.  You appear to be using the commons-logging API to obtain 
loggers.  This is a problem because of how commons-logging works 
internally.   The same issue occurs with SLF4J adapters.  The only 
SLF4J adapter that works with repository selectors is the 
Logback.  Try using Log4j loggers directly.

BTW, if you could send these emails to the log4j-user list, that 
would be better than sending to me directly.

thanks,

Jake

At 01:49 PM 5/25/2007, you wrote:
 >I am using log4j1.2.11, and this is my setting in web.xml:
 >
 >
 ><servlet>
 >               <servlet-name>log4j-init</servlet-name>
 >               <servlet-class>com.abc.def.config.Log4jInit</servlet-class>
 >               <init-param>
 >                       <param-name>log4j-init-file</param-name>
 >
 ><param-value>/WEB-INF/classes/log4j.properties</param-value>
 >               </init-param>
 >               <load-on-startup>2</load-on-startup>
 >       </servlet>
 >
 >
 >
 >Env-entry:
 >
 ><env-entry>
 >        <description>JNDI logging context for this app</description>
 >        <env-entry-name>log4j/logging-context</env-entry-name>
 >        <env-entry-value>msuid</env-entry-value>
 >        <env-entry-type>java.lang.String</env-entry-type>
 >    </env-entry>
 >
 >
 >
 >
 >I used your JDIRS class:
 >
 >
 >/** JNDI based Repository selector */
 >public class JNDIRS implements RepositorySelector {
 >
 >  // key: name of logging context,
 >  // value: Hierarchy instance
 >  private Hashtable ht;
 >  private Hierarchy defaultHierarchy;
 >
 >  public JNDIRS() {
 >   ht = new Hashtable();
 >   defaultHierarchy = new Hierarchy(new RootCategory(Level.DEBUG));
 >  }
 >
 >  // the returned value is guaranteed to be non-null
 >  public LoggerRepository getLoggerRepository() {
 >    String loggingContextName = null;
 >
 >    try {
 >      Context ctx = new InitialContext();
 >      loggingContextName = (String)
 >ctx.lookup("java:comp/env/log4j/logging-context");
 >    } catch(NamingException ne) {
 >      // we can't log here
 >       ne.printStackTrace();
 >    }
 >
 >    if(loggingContextName == null) {
 >      return defaultHierarchy;
 >    } else {
 >      Hierarchy h = (Hierarchy) ht.get(loggingContextName);
 >      if(h == null) {
 >        h = new Hierarchy(new RootCategory(Level.INFO));
 >        ht.put(loggingContextName, h);
 >      }
 >      return h;
 >    }
 >  }
 >}
 >
 >
 >
 >
 >log4j-init-class:
 >
 >
 >mport java.io.IOException;
 >
 >import javax.servlet.ServletException;
 >import javax.servlet.http.HttpServlet;
 >import javax.servlet.http.HttpServletRequest;
 >import javax.servlet.http.HttpServletResponse;
 >
 >import org.apache.log4j.LogManager;
 >import org.apache.log4j.PropertyConfigurator;
 >
 >public class Log4jInit extends HttpServlet {
 >       static final long serialVersionUID = 123L;
 >       private static Object guard = LogManager.getRootLogger();
 >       public void init() throws ServletException {
 >
 >               String file = getInitParameter("log4j-init-file");
 >               // if the log4j-init-file is not set, then no point in
 >trying
 >               try {
 >                       if (file != null) {
 >                               try {
 >
 >PropertyConfigurator.configure(file);
 >
 >
 >LogManager.setRepositorySelector(new JNDIRS(), guard);
 >
 >                               } catch (Exception fe) {
 >                                       throw new Exception(fe.getMessage()
 >                                                       + "log4j.properties
 >file not found");
 >                               }
 >
 >                       } else {
 >                               throw new IOException("log4j-init-file
 >parameter is not set");
 >                       }
 >
 >               } catch (Exception io) {
 >                       throw new ServletException(io);
 >               }
 >       }
 >
 >
 >
 >       public void doGet(HttpServletRequest req, HttpServletResponse res) {
 >       }
 >
 >}
 >
 >
 >
 >
 >Is this appropriate, do I have to change the way I log:
 >
 >protected final Log logger = LogFactory.getLog(getClass());
 >
 >
 >
 >Thanks,
 >
 >Sandip Prashar
 >
 >
 >
 >-----Original Message-----
 >From: Jacob Kjome [mailto:hoju@visi.com]
 >Sent: Friday, May 25, 2007 1:56 PM
 >To: Log4J Users List
 >Cc: Sandip Prashar
 >Subject: Re: Log4j repository selector
 >
 >
 >I take it you are using Log4j-1.3 alpha?  If so, look here...
 >
 >http://logging.apache.org/log4j/docs/api-1.3/org/apache/log4j/selector/Conte
 >xtJNDISelector.html
 >
 >
 >Jake
 >
 >Quoting Sandip Prashar <sp...@humbersys.com>:
 >
 >> Could you please provide sample, how to configure in RepositorySelector in
 >> log4jinit class, I am using the ContextJNDISelector.
 >>
 >>
 >>
 >>
 >>
 >> Thanks,
 >>
 >>
 >>
 >> Sandip Prashar
 >>
 >>
 >>
 >> Principal Consultant
 >>
 >> -----------------------------------------------------------
 >>
 >> HumberSys Consultancy Services (HCS)
 >>
 >> Tel:           647-500-7376
 >>
 >> Email:         <ma...@humbersys.com> sprashar@humbersys.com
 >>
 >> Website:     <http://www.humbersys.com/> www.humbersys.com
 >>
 >> ------------------------------------------------
 >>
 >>
 >>
 >> "Unlocking Information Technology"
 >>
 >>
 >>
 >>


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