[slf4j-user] Per Web App logging
Amir Mistric
amistric at nemours.org
Tue Nov 21 14:11:32 CET 2006
Hi Ceki
Here is the whole class
-------------------------------------------------------------------
public final class Log4JContextualRepositorySelector
implements RepositorySelector {
private static boolean initialized = false;
private static Object guard =
LogManager.getRootLogger();
private static Map repositories = new HashMap();
private static LoggerRepository defaultRepository;
/**
* Register your web-app with this repository selector.
* @param log4jConf
* either an .xml or .properties file (e.g. 'log4j.xml')
*/
public static synchronized void addToRepository(final String log4jConf)
{
// set the global RepositorySelector
if (!initialized) {
defaultRepository = LogManager.getLoggerRepository();
RepositorySelector theSelector = new
Log4JContextualRepositorySelector();
LogManager.setRepositorySelector(theSelector, guard);
initialized = true;
}
Hierarchy hierarchy = new Hierarchy(new RootLogger(Level.DEBUG));
configureLog4J(log4jConf, hierarchy);
ClassLoader loader = Thread.currentThread().getContextClassLoader();
repositories.put(loader, hierarchy);
}
/**
* Unregister your web-app with this repository selector.
*/
public static synchronized void removeFromRepository() {
repositories.remove(Thread.currentThread().getContextClassLoader());
}
// ovverriden
public LoggerRepository getLoggerRepository() {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
LoggerRepository repository = (LoggerRepository) repositories
.get(loader);
if (repository == null) {
return defaultRepository;
} else {
return repository;
}
}
// private constructor to prevent instantiation
private Log4JContextualRepositorySelector() {
}
// configure Log4J using XML or Properties file
private static void configureLog4J(String log4jConf, Hierarchy
hierarchy) {
try {
if (log4jConf.endsWith(".properties")) {
PropertyConfigurator propConf = new PropertyConfigurator();
propConf.doConfigure(log4jConf, hierarchy);
System.out.println("Log4J PropertyConfigurator:" +
log4jConf);
} else if (log4jConf.endsWith(".xml")) {
DOMConfigurator conf = new DOMConfigurator();
conf.doConfigure(log4jConf, hierarchy);
System.out.println("Log4J DOMConfigurator:" + log4jConf);
} else {
throw new Exception("Please use either '.properties' or
'.xml'"
+ "file extensions for Log4J configuration files");
}
} catch (IOException e) {
System.out.println("I/O exception occured while accessing"
+ log4jConf);
e.printStackTrace();
} catch (SAXException e) {
System.out.println("SAX parsing exception occured while
accessing"
+ log4jConf);
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
-------------------------------------------------------------------
-----Original Message-----
From: user-bounces at slf4j.org [mailto:user-bounces at slf4j.org] On Behalf Of
Ceki Gülcü
Sent: Monday, November 20, 2006 5:21 PM
To: User list for the slf4j project
Subject: Re: [slf4j-user] Per Web App logging
In the MyWebAppContextListener code you supplied, one can read:
Log4JContextualRepositorySelector.addToRepository(pConfigFilePath);
How is the addToRepository method defined?
At 11:14 PM 11/20/2006, you wrote:
>Yes it is and all we did was to rename the MyRepositorySelector class that
>JBoss Wiki has. The code is the same as on
>http://wiki.jboss.org/wiki/Wiki.jsp?page=Log4jRepositorySelector
>
>Amir
>
>-----Original Message-----
>From: user-bounces at slf4j.org [mailto:user-bounces at slf4j.org] On Behalf Of
>Ceki Gülcü
>Sent: Monday, November 20, 2006 4:51 PM
>To: User list for the slf4j project
>Subject: Re: [slf4j-user] Per Web App logging
>
>
>Hi Amir,
>
>Is the Log4JContextualRepositorySelector class yours?
>
>At 09:55 PM 11/20/2006, you wrote:
> >Hi Ceki
> >
> >Thanks for the quick reply.
> >Here are my answers:
> >
> > >If you use the JNDI-based repository selector, you can place a single
> > >log4j.jar in the containers shared class loader. I am a little
surprised
> > >that you need to copy log4j.jar in each web-app. Can you please
>elaborate?
> >
> >Here is the URL we used to configure the RepositorySelector.
> >http://wiki.jboss.org/wiki/Wiki.jsp?page=Log4jRepositorySelector
> >We have about 10 web apps running on JBoss AS and the above solution
works.
> >Each web app has its own WEB-INF/lib/log4j.jar and WEB-INF/log4j.xml...
> >Also, each application has a class that implements
ServletContextListener.
> >
> >public final class MyWebAppContextListener implements
>ServletContextListener
> >{
> >
> > private ServletContext context = null;
> > // we cannot use Logger.getLogger until we initialize it...
> > private static Logger log;
> >
> >...
> >...
> >...
> >
> >In this class we call (in contextInitialized() method):
> >
> > this.context.log("Initializing Log4J...");
> > // Before using Log4J logging system, we must initialize it by
> >adding
> > // the webapp specific configuration to the repository.
> >
>Log4JContextualRepositorySelector.addToRepository(pConfigFilePath);
> > // note that we can't call Logger.getLogger() until
> > // Log4JContextualRepositorySelector.addToRepository() is
called.
> > // For all other classes in the webapp, you can call
> >Logger.getLogger()
> > // at any time.
> > log = Logger.getLogger(FindADocContextListener.class);
> >
> >That is it....
> >However this fails if we remove log4j.jar from WEB-INF/lib...
> >
> >
> >I do apologize if this question is gettting off topic
> >
> >Amir
> >
> >
> >
> >
> >
> >
> >
> >-----Original Message-----
> >From: user-bounces at slf4j.org [mailto:user-bounces at slf4j.org] On Behalf Of
> >Ceki Gülcü
> >Sent: Monday, November 20, 2006 3:43 PM
> >To: User list for the slf4j project
> >Subject: Re: [slf4j-user] Per Web App logging
> >
> >Hi Amir,
> >
> >At 09:32 PM 11/20/2006, Amir Mistric wrote:
> > >Hello Everyone
> > >
> > >My company would like to try SLF4J as a replacement for Log4J.
> > >Currently we use JBoss AS 4.x and the problem we had was that we could
>not
> > >configure logging "per web application".
> > >We ended up using RepositorySelector trick in order to be able to have
a
> > >separate log4j.xml in every web application deployed.
> > >This approach, however, requires each web app to have a copy of
log4j.jar
> > >in its WEB-INF/lib......
> >
> >If you use the JNDI-based repository selector, you can place a single
> >log4j.jar in the containers shared class loader. I am a little surprised
> >that you need to copy log4j.jar in each web-app. Can you please
elaborate?
> >
> > >Recently we decided to create a shared library repository for all web
>apps
> > >we have.
> > >While this approach has reduced the "baggage" each web app had in its
> > >WEB-INF/lib we still have to use log4j.jar ....
> > >
> > >Our goal is simple. Have every web application running on the same app
> > >server have its own logging config file but share the single JAR (or
>jars).
> >
> >Not yet, but it's core feature planned in one of the next releases of
> >logback.
> >
> > >Can SLF4J accomplish this (using built in logback classic) for us?
> >
> >No, SLF4J is just a front for logback/jul/log4j/jcl. While SLF4J cannot
> >accomplish what you are asking, log4j can do so, and logback will in the
> >near future.
> >
> > >Thanks
> > >Amir
> >
> >--
> >Ceki Gülcü
> >Logback: The reliable, generic, fast and flexible logging framework for
> >Java.
> >http://logback.qos.ch
> >
> >_______________________________________________
> >user mailing list
> >user at slf4j.org
> >http://www.slf4j.org/mailman/listinfo/user
> >
> >_______________________________________________
> >user mailing list
> >user at slf4j.org
> >http://www.slf4j.org/mailman/listinfo/user
>
>--
>Ceki Gülcü
>Logback: The reliable, generic, fast and flexible logging framework for
>Java.
>http://logback.qos.ch
>
>_______________________________________________
>user mailing list
>user at slf4j.org
>http://www.slf4j.org/mailman/listinfo/user
>
>_______________________________________________
>user mailing list
>user at slf4j.org
>http://www.slf4j.org/mailman/listinfo/user
--
Ceki Gülcü
Logback: The reliable, generic, fast and flexible logging framework for
Java.
http://logback.qos.ch
_______________________________________________
user mailing list
user at slf4j.org
http://www.slf4j.org/mailman/listinfo/user
More information about the slf4j-user
mailing list