Tuesday, December 14, 2010
Transport To Anthem Outlet
Hibernate uses log4j but it does so through another API called slf4j (Simple Light Facade for Java).
From previous experiments, I found that the default use the hibernate jar slf-simple in practice only allows the output to video, not on file, so I replaced that jar with the following:
log4j12-slf4j slf4j-api-1.5.10.jar
-1.5.10.jar
Without the replacement of course remains to configure log4j in our application, in particular, I added in a web.xml context-param with the path to go to read the file properties .
Finally, upload the whole, in my case within a context listener: String
pathLog4J event.getServletContext = (). GetRealPath ("/"); String
fileLog4J event.getServletContext = (). GetInitParameter ( "log4jConfigPath");
PropertyConfigurator.configure (pathLog4J fileLog4J +);
In this way the settings in the log4j.properties file become active.
As an alternative to the path to the configuration file, I found this great way around that using the reflection API of the classes allows you to load the log4j.properties example from within one of our package of classes:
public void reloadLog4jProperties () throws Throwable {try {
ClassLoader cl = getClass().getClassLoader();
Class logManagerClass = cl.loadClass("org.apache.log4j.LogManager");
Method resetConfiguration = logManagerClass.getMethod("resetConfiguration", new Class[]{});
resetConfiguration.invoke(null, new Object[]{});
URL log4jprops = cl.getResource("log4j.properties");
if (log4jprops != null) {
Class propertyConfiguratorClass = cl.loadClass("org.apache.log4j.PropertyConfigurator");
Method configure = propertyConfiguratorClass.getMethod("configure", new Class[]{URL.class});
configure.invoke(null, new Object[]{log4jprops});
}} catch (InvocationTargetException e) {throw
e.getTargetException ();}
}
In this way the load is disconnected from the log4j file system in which to install the application.
Subscribe to:
Posts (Atom)