Hello,
I don't have access to my computer right now, but here's roughly what I do to
use log4J within my Servlet 2.3 API compliant webapps.
1. Create a properties file called log4j.properties
2. Place this in WEB-INF folder.
3. Write a ContextListener and in the contectInitialized method, access the
servlet context. Make sure you place this Listener implementation along with
your other classes and that you add an entry for it in web.xml
4. Ask the servlet context to get the following resource as a stream
e.g. InputStream is =
servletContext.getResourceAsStream("/WEB-INF/log4j.properties");
5. Create a properties file that'll initialize with this property file.
Properties properties = new Properties();
properites.load(is);
is.close();
6. Now pass this newly initialized properties object to the
PropertyConfigurator...
That's it.
Sriram
--- James Adams <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I am having the problem of Tomcat initializing/creating my Log4j log
> files in whatever directory that Tomcat is started from. In other words
> if I am currently at directory /kung/foo when I issue the command
> /etc/init.d/tomcat start, then my log files will be created under
> /kung/foo instead of under the /path/to/webapp directory, which is where
> I thought that they should be created.
>
> I have the following WEB-INF/classes/log4j.properties file:
>
> --------------------
> # Set root category priority to DEBUG and its only appender to A1.
> log4j.rootCategory=DEBUG, A1
>
> # A1 is set to be a RollingFileAppender.
> log4j.appender.A1=org.apache.log4j.RollingFileAppender
>
> log4j.appender.A1.File=LogMessages-trajview.txt
> log4j.appender.A1.MaxFileSize=500KB
>
> # Keep one backup file
> log4j.appender.A1.MaxBackupIndex=2
>
> # A1 uses PatternLayout.
> log4j.appender.A1.layout=org.apache.log4j.PatternLayout
> log4j.appender.A1.layout.ConversionPattern=%5p %d (%F:%L) - %m%n
> ---------------------
>
>
> The log4j stuff is being initialized/configured by a servlet which has
> the following init() method:
>
> -------------------
> public void init()
> {
> // gets the base directory to use, from the web application's
> "context"
> String prefix = getServletContext().getRealPath("/");
>
> // this looks in the web.xml for the initialization file
> parameter
> // which points to log4j.properties
> String file = getInitParameter("log4j-init-file");
>
> // if the log4j-init-file is not set, then no point in trying
> if (file != null)
> {
> // apply the configuration specified in the initialization
> file
> PropertyConfigurator.configure(prefix + file);
> }
> }
> ------------------
>
>
> The above mentioned Log4j initialization servlet is being called at
> loadup time by virtue of the following entry in my web.xml:
>
> -----------------
> <servlet>
> <servlet-name>log4j-init</servlet-name>
> <servlet-class>myapp.servlet.Log4jInit</servlet-class>
> <init-param>
> <param-name>log4j-init-file</param-name>
> <param-value>WEB-INF/classes/log4j.properties</param-value>
> </init-param>
> <load-on-startup>1</load-on-startup>
> </servlet>
> -----------------
>
>
> Because I have three separate webapps which should all have their own
> log files I would like to have the log files created in the separate
> webapp directories, or perhaps better yet in a logs subdirectory under
> each webapp directory. For example if a webapp is under
> /var/tomcat/webappps/myapp then I would like for the log file to be
> created as /var/tomcat/webapps/myapp/LogMessages.txt or as
> /var/tomcat/webapps/myapp/logs/LogMessages.txt. How can I get this to
> happen ? Should I make a change to a log4j.properties entry, or perhaps
> I'm making an error in my log4j initialization servlet's init() method
> by referencing the wrong path in the getRealPath() call ?
>
> Any suggestions will be appreciated. Thanks in advance.
>
>
> -James
>
>
> --
> To unsubscribe: <mailto:[EMAIL PROTECTED]>
> For additional commands: <mailto:[EMAIL PROTECTED]>
> Troubles with the list: <mailto:[EMAIL PROTECTED]>
>
__________________________________________________
Do You Yahoo!?
Yahoo! Sports - live college hoops coverage
http://sports.yahoo.com/
--
To unsubscribe: <mailto:[EMAIL PROTECTED]>
For additional commands: <mailto:[EMAIL PROTECTED]>
Troubles with the list: <mailto:[EMAIL PROTECTED]>