RE: Location of log files with Tomcat
Howdy, >initialization servlet that goes through the log4j.properties file >making all the file locations be relative to my web app directory. The >code is listed below. Consider putting this code in a ServletContextListener's contextInitialized() method, instead of a servlet's init() method, as the latter can get called multiple times at the container's behest. >My questions are: >- Is this a reasonable way of doing things? >- Is there a better way? >public void init() { >String prefix = getServletContext().getRealPath("/"); Keep in mind prefix will be null if you're running out of a packed .war file. > >for ( enum = props.propertyNames(); enum.hasMoreElements(); ) { >pName = (String ) enum.nextElement(); >if ( pName.startsWith("log4j.appender.") && > pName.endsWith(".File")) { Instead of doing this whole property replacement scheme, why not just specify a system-property based file name in your log4j configuration file, e.g. log4j.MyAppender.File=${CATALINA_HOME}/logs/myLog.txt The above strategy and similar ones have been discussed on this list in the past. You can search the archives for more information and examples. Yoav Shapira This e-mail, including any attachments, is a confidential business communication, and may contain information that is confidential, proprietary and/or privileged. This e-mail is intended only for the individual(s) to whom it is addressed, and may not be saved, copied, printed, disclosed or used by anyone else. If you are not the(an) intended recipient, please immediately delete this e-mail from your computer system and notify the sender. Thank you. - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Location of log files with Tomcat
I think there is an easier way. You can use log4j's variable substitution mechanism: All option values admit variable substitution. The syntax of variable substitution is similar to that of Unix shells. The string between an opening "${" and closing "}" is interpreted as a key. The value of the substituted variable can be defined as a system property or in the configuration file itself. The value of the key is first searched in the system properties, and if not found there, it is then searched in the configuration file being parsed. The corresponding value replaces the ${variableName} sequence. For example, if java.home system property is set to /home/xyz, then every occurrence of the sequence ${java.home} will be interpreted as /home/xyz. HTH, At 09:20 AM 8/25/2003 +0100, Louise Pryor wrote: My web site is on a shared host, so I don't have access to the $CATALINA_HOME/logs directory. In fact, I don't even know where the host thinks the directory of my web site is (I see it as being in ~/public_html ). I'm using log4j as my logging mechanism (directly rather than through commons logging) and have some code in an initialization servlet that goes through the log4j.properties file making all the file locations be relative to my web app directory. The code is listed below. My questions are: - Is this a reasonable way of doing things? - Is there a better way? Thanks for any help on this Louise -- Louise Pryor http://www.louisepryor.com public void init() { String prefix = getServletContext().getRealPath("/"); String file = getInitParameter("log4j-init-file"); Properties props = new Properties(); Enumeration enum; String pName, pVal; if (file != null) { try { FileInputStream istream = new FileInputStream(prefix+file); props.load(istream); istream.close(); } catch (IOException e) { LogLog.error("Could not read configuration file ["+ prefix+file+"].", e); LogLog.error("Ignoring configuration file [" + prefix+file+"]."); return; } // go through the properties looking for appender files. // add the prefix onto them for ( enum = props.propertyNames(); enum.hasMoreElements(); ) { pName = (String ) enum.nextElement(); if ( pName.startsWith("log4j.appender.") && pName.endsWith(".File")) { pVal = props.getProperty(pName); props.setProperty(pName, prefix+pVal); } // end of if () } // end of for () PropertyConfigurator.configure(props); } else { BasicConfigurator.configure(); } } -- Ceki For log4j documentation consider "The complete log4j manual" ISBN: 2970036908 http://www.qos.ch/shop/products/clm_t.jsp - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Location of log files with Tomcat
On Monday, August 25, 2003 at 2:50:25 PM, Shapira, Yoav wrote: >>initialization servlet that goes through the log4j.properties file >>making all the file locations be relative to my web app directory. The >>code is listed below. SY> Consider putting this code in a ServletContextListener's SY> contextInitialized() method, instead of a servlet's init() method, as SY> the latter can get called multiple times at the container's behest. OK. I haven't come across a ServletContextListener before, so I'll look it up and work out what it does. SY> Keep in mind prefix will be null if you're running out of a packed .war SY> file. This just won't happen, as I can't deploy using a war. I have to unpack the files by hand into my public_html directory. SY> Instead of doing this whole property replacement scheme, why not just SY> specify a system-property based file name in your log4j configuration SY> file, e.g. SY> log4j.MyAppender.File=${CATALINA_HOME}/logs/myLog.txt As I said, I don't have any access at all to the ${CATALINA_HOME} directory. When I do a getRealPath("/") in my webapp the result is /home/myName/public_html/. For obvious reasons I don't want to hard code that in anywhere. I am not even sure that say ${CATALINA_HOME}/webapps/myName/ would work, given the getRealPath behaviour. What I would really like is a foolproof way of specifying a file in my webapp directory structure in the log4j configuration file, which would work whatever weird configuration my hosting provider has used for Tomcat. Louise -- Louise Pryor http://www.louisepryor.com - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]