Re: How do I do to change the properties file forlog4jintoanother directory?
getting resources from WEB-INF is specific to a servlet application. If you need to get the config file from both places, then put your config file back into WEB-INF/classes/config. At that point, the code below will work and you can change your path in the servlet example to this... context.getResource(/WEB-INF/classes/config/log4j.properties); Jake At 04:07 PM 11/7/2003 +1100, you wrote: Jake, Using your suggestion, I have able to make my Log4j servlet reading the correct properties in /WEB-INF/config dir. I know this is a bit out of track, but in another java bean file, I am using InputStream. Is it a way to change to use the URL just like the Log4j method which you have pointed out? as I am not able to get it read from /WEB-INF. Thank you. public class ConnectionManager implements HttpSessionBindingListener { private Connection connection; private Statement statement; private String driver = ; private String dbURL = ; private String login = ; private String password = ; static public void main(String[] args) { ConnectionManager cm = new ConnectionManager(); } // main public ConnectionManager() { Properties Prop = new Properties(); try { InputStream configStream = getClass().getResourceAsStream(/config/database.properties); Prop.load(configStream); configStream.close(); } catch(IOException e) { System.out.println(Error: Cannot laod configuration file );} driver =Prop.getProperty(driver); dbURL = Prop.getProperty(dbURL); login = Prop.getProperty(login); password = Prop.getProperty(password); } [EMAIL PROTECTED] 07/Nov/2003 12:19:45 pm It would help if you actually obtained your servlet context. Right now, it is null. ServletContext context = this.getServletContext() Jake At 10:47 AM 11/7/2003 +1100, you wrote: Jake, Thank you for your time. I have made the changes to the code as you have suggested. It compiled without problem. However, it does not seems to initiate the Log4j Tag Library. Have I missed out anything? TJ package com.log; import org.apache.log4j.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Log4jInit extends HttpServlet { public void init() { try { ServletContext context = null; URL url = context.getResource(/WEB-INF/config/log4j.properties); BasicConfigurator.configure(); BasicConfigurator.resetConfiguration(); PropertyConfigurator.configure(url); } catch (MalformedURLException e) { e.printStackTrace();} Logger logger = Logger.getLogger(getClass()); logger.info(Logging system initialised successfully); } public void doGet(HttpServletRequest req, HttpServletResponse res) { } } [EMAIL PROTECTED] 07/Nov/2003 10:21:59 am You need to look at my code closer. context is a ServletContext. You can use it to access stuff anywhere in the webapp. URL url = context.getResource(/WEB-INF/config/log4j.properties); PropertyConfigurator.configure(url); And, of course, you can also use the InputStream way if you want, but the URL way is a bit simpler. One thing to note. In my original code, I had forgotten to prefix WEB-INF with a /. That is fixed in the code above. Jake At 09:59 AM 11/7/2003 +1100, you wrote: Jake, Putting the properties file under /web-inf/config is what I really wanted to achieve, but I am not be able to make it work. I uses the following to initialised the log4j package. package com.log; import org.apache.log4j.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Log4jInit extends HttpServlet { public void init() { String servletContainer = getServletContext().getRealPath(/); // The exact path to the config file is in servlet mapping String file = getInitParameter(log4j); Logger logger = Logger.getLogger(getClass()); // if the log4j-init is not set, then no point in trying if (file != null) { BasicConfigurator.configure(); BasicConfigurator.resetConfiguration(); PropertyConfigurator.configure(servletContainer+file); // Sent into to system.log logger.info(Logging system initialised successfully); } } public void doGet(HttpServletRequest req, HttpServletResponse res) { } } I uses the following for my database properties which I have no luck in getting it to read /web-inf/config too: Properties Prop = new Properties(); try { InputStream configStream = getClass().getResourceAsStream(/config/database.properties); Prop.load(configStream); configStream.close(); }
Re: How do I do to change the properties file forlog4jintoanother directory?
Jake, Using your suggestion, I have able to make my Log4j servlet reading the correct properties in /WEB-INF/config dir. I know this is a bit out of track, but in another java bean file, I am using InputStream. Is it a way to change to use the URL just like the Log4j method which you have pointed out? as I am not able to get it read from /WEB-INF. Thank you. public class ConnectionManager implements HttpSessionBindingListener { private Connection connection; private Statement statement; private String driver = ; private String dbURL = ; private String login = ; private String password = ; static public void main(String[] args) { ConnectionManager cm = new ConnectionManager(); } // main public ConnectionManager() { Properties Prop = new Properties(); try { InputStream configStream = getClass().getResourceAsStream(/config/database.properties); Prop.load(configStream); configStream.close(); } catch(IOException e) { System.out.println(Error: Cannot laod configuration file );} driver =Prop.getProperty(driver); dbURL = Prop.getProperty(dbURL); login = Prop.getProperty(login); password = Prop.getProperty(password); } [EMAIL PROTECTED] 07/Nov/2003 12:19:45 pm It would help if you actually obtained your servlet context. Right now, it is null. ServletContext context = this.getServletContext() Jake At 10:47 AM 11/7/2003 +1100, you wrote: Jake, Thank you for your time. I have made the changes to the code as you have suggested. It compiled without problem. However, it does not seems to initiate the Log4j Tag Library. Have I missed out anything? TJ package com.log; import org.apache.log4j.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Log4jInit extends HttpServlet { public void init() { try { ServletContext context = null; URL url = context.getResource(/WEB-INF/config/log4j.properties); BasicConfigurator.configure(); BasicConfigurator.resetConfiguration(); PropertyConfigurator.configure(url); } catch (MalformedURLException e) { e.printStackTrace();} Logger logger = Logger.getLogger(getClass()); logger.info(Logging system initialised successfully); } public void doGet(HttpServletRequest req, HttpServletResponse res) { } } [EMAIL PROTECTED] 07/Nov/2003 10:21:59 am You need to look at my code closer. context is a ServletContext. You can use it to access stuff anywhere in the webapp. URL url = context.getResource(/WEB-INF/config/log4j.properties); PropertyConfigurator.configure(url); And, of course, you can also use the InputStream way if you want, but the URL way is a bit simpler. One thing to note. In my original code, I had forgotten to prefix WEB-INF with a /. That is fixed in the code above. Jake At 09:59 AM 11/7/2003 +1100, you wrote: Jake, Putting the properties file under /web-inf/config is what I really wanted to achieve, but I am not be able to make it work. I uses the following to initialised the log4j package. package com.log; import org.apache.log4j.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Log4jInit extends HttpServlet { public void init() { String servletContainer = getServletContext().getRealPath(/); // The exact path to the config file is in servlet mapping String file = getInitParameter(log4j); Logger logger = Logger.getLogger(getClass()); // if the log4j-init is not set, then no point in trying if (file != null) { BasicConfigurator.configure(); BasicConfigurator.resetConfiguration(); PropertyConfigurator.configure(servletContainer+file); // Sent into to system.log logger.info(Logging system initialised successfully); } } public void doGet(HttpServletRequest req, HttpServletResponse res) { } } I uses the following for my database properties which I have no luck in getting it to read /web-inf/config too: Properties Prop = new Properties(); try { InputStream configStream = getClass().getResourceAsStream(/config/database.properties); Prop.load(configStream); configStream.close(); } catch(IOException e) { System.out.println(Error: Cannot laod configuration file ); } Do you have a sample code that works? Thank you. TJ [EMAIL PROTECTED] 06/Nov/2003 04:33:59 pm At 11:29 AM 11/5/2003 +1100, you wrote: I am using the Log Tag Library 1.0 from Jakarta Project. In the Installation document, it suggest that in order to