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 2003/11/07 15:21:31 UTC
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");
>
>}
>
>
> >>> hoju@visi.com 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) {
>}
> >
> >}
> >
> >
> > >>> hoju@visi.com 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
> > >
> > >
> > >
> > > >>> hoju@visi.com 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
> >initialize
> > > >Log4j automatically, the log4j.properties file will have to be
> >placed
> > >in
> > > >/WEB-INF/classes.
> > > >
> > > >How could I put the log4j.properties file in
> >/WEB-INF/classes/config
> > > >directory? What do I have to do?
> > > >
> > > >Thank you.
> > >
> > >If you are going to create a "config" dir, why not do it directly
> >under
> > >
> > >WEB-INF rather than where classes are put? You can load up an
>input
> > >stream
> > >using
>context.getResourceAsStream("WEB-INF/config/log4j.properties")
> > >and
> > >feed that info a Properties object, then send that into
> > >PropertyConfigurator.configure(Properties) or load up a URL using
> > >context.getResource("WEB-INF/config/log4j.properties") and send
>that
> > >info
> > >PropertyConfigurator.configure(URL).
> > >
> > >Of course you can still do this even if you leave the "config"
> > >directory
> > >under "WEB-INF/classes" and use the same technique above. Whatever
> >you
> > >
> > >feel comfortable with. I just like not cluttering my classpath
>with
> > >non-classes.
> > >
> > >Jake
> > >
> > >
> >
> >---------------------------------------------------------------------
> > >To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
> > >For additional commands, e-mail: log4j-user-help@jakarta.apache.org
>
> > >
> > >
> > >
> > >
> > >IMPORTANT -
> > >
> > >This email and any attachments are confidential and may be
>privileged
> >in
> > >which case neither is intended to be waived. If you have received
>this
> >
> > >message in error, please notify us and remove it from your system.
>It
> >is
> > >your responsibility to check any attachments for viruses and
>defects
> > >before opening or sending them on. Where applicable, liability is
> >limited
> > >by the Solicitors Scheme approved under the Professional Standards
>Act
> >
> > >1994 (NSW). Minter Ellison collects personal information to provide
> >and
> > >market our services. For more information about use, disclosure and
> > >access, see our privacy policy at www.minterellison.com.
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail: log4j-user-help@jakarta.apache.org
> >
> >
> >
> >
> >IMPORTANT -
> >
> >This email and any attachments are confidential and may be privileged
>in
> >which case neither is intended to be waived. If you have received this
>
> >message in error, please notify us and remove it from your system. It
>is
> >your responsibility to check any attachments for viruses and defects
> >before opening or sending them on. Where applicable, liability is
>limited
> >by the Solicitors Scheme approved under the Professional Standards Act
>
> >1994 (NSW). Minter Ellison collects personal information to provide
>and
> >market our services. For more information about use, disclosure and
> >access, see our privacy policy at www.minterellison.com.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: log4j-user-help@jakarta.apache.org
>
>
>
>
>IMPORTANT -
>
>This email and any attachments are confidential and may be privileged in
>which case neither is intended to be waived. If you have received this
>message in error, please notify us and remove it from your system. It is
>your responsibility to check any attachments for viruses and defects
>before opening or sending them on. Where applicable, liability is limited
>by the Solicitors Scheme approved under the Professional Standards Act
>1994 (NSW). Minter Ellison collects personal information to provide and
>market our services. For more information about use, disclosure and
>access, see our privacy policy at www.minterellison.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-user-help@jakarta.apache.org