I wanted to put my log4j.xml file in WEB-INF/conf directory where I have many other configuration files. And I wanted the web application to read log4.xml from there.
Not sure what you really wanna do....
if your log4j.xml is in the classpath, when you start your app-server, it should be loaded automatically.
Check the console when you start your server and you should see your log4j info.
You can also put debug=true:
in your xml. you will be able to see a lot of info about your config.
Now if you want to access the appenders you configured in the log4j.xml, all you have to do is:
Logger mylogger = Logger.getLogger("MyAppenderName");
Ok, i think you wanna load a custom log4j config file! In a web app context, you will need 2 things:
create a contextListnerServlet; modify your web.xml
ServletListner:
public class StartupListener implements ServletContextListener
{
@Override
public void contextDestroyed(ServletContextEvent arg0)
{
// Cleanup code goes here
}
@Override
public void contextInitialized(ServletContextEvent sce)
{
Logger logger = null;
String log4jFile = sce.getServletContext().getInitParameter("log4jFileName");
DOMConfigurator.configure(sce.getServletContext().getRealPath(log4jFile));
logger = LogManager.getLogger(StartupListener.class.getName());
logger.debug("Loaded: " + log4jFile);
}
web.xml:
log4jFileName
WEB-INF/config/log4j-my.xml
com.yourpackage.StartupListener
Hope it helps