Re[2]: addEventlistener question
Jacob Kjome <[email protected]>
| Newsgroups | gmane.comp.java.enhydra.barracuda.general |
|---|---|
| Organization | Springfield Nuclear Plant |
| Message-ID | <[email protected]> |
Hello David,
Well, not exactly. The <Parameter> provides a way to override a
<context-param> entry in the web.xml (or create one that doesn't exist in
web.xml). This value is read by InitContextListener. That value is set
as a system property specifically for your context. If your context
path is "mycontext", then the system property created will be
"mycontext.log.home". You can then look it up as...
System.getProperties().getProperty("mycontext.log.home");
It is important that these names are unique since system properties
are global to the JVM.
You know what. I misled you before. The issue is not finding the log
home. That isn't container-specific. The issue is dynamically
creating the system variable based upon the name of context. Here's
the problem...
There is no way to get the name of the context at runtime. You can
call getServletContextName(), but that only returns the value one sets
in the web.xml for <display-name> which may or may not be there nor,
if it is there, guaranteed to be unique or predictable. You also
can't use getRealPath() and then take the name of the directory that
your context lives in. The directory name isn't guaranteed to be
unique and isn't guarateed to match the name of the actual runtime
servlet context path ( eg... "/myapp" in directory "c:\mycoolapp" ).
The only way I was able to make work was to grab the value of the
returned value of the "javax.servlet.context.tempdir" returned by the context...
private void setFileAppenderSystemProperty(String logHome, ServletContext context) {
File logHomeDir = new File(logHome);
if (logHomeDir.exists() || logHomeDir.mkdirs()) {
String tempdir =
"" + context.getAttribute("javax.servlet.context.tempdir");
int lastSlash = tempdir.lastIndexOf(File.separator);
if ((tempdir.length() - 1) > lastSlash) {
String logHomePropertyName =
tempdir.substring(lastSlash + 1) + ".log.home";
context.log(
"Setting system property [ " + logHomePropertyName + " ] to [ "
+ logHome + " ]");
System.setProperty(logHomePropertyName, logHome);
}
}
}
In Tomcat, the name of the directory created in the tmpdir for a given
context happens to match that of its context path. This makes it so
one can predict the name of the system variable without actually
hardcoding anything about what the name should be. The only place
where you have to reference it is in logj4.xml. This is a convenience
that, apparently (and I did suspect as much before, but was lazy about
testing), isn't guaranteed to be portable across
containers.
Can you do me a favor and find out what the value of "tmpdir" is
under Enhydra?...
String tempdir = "" + context.getAttribute("javax.servlet.context.tempdir");
Maybe there is a pattern that I can still use to keep the convenience?
Otherwise, maybe I need to require another <context-param> stating the
name of the context? Any other suggestions?
I'm really sorry I confused you. I guess I must have been
sleeping-at-the-wheel when I replied previously.
Jake
Thursday, March 06, 2003, 1:42:45 PM, you wrote:
DRR> If I use
DRR> <Context path="/myapp" docBase="myapp">
DRR> <Parameter name="log4j-log-home" override="true"
DRR> value="C:/java/logs/tomcat"/>
DRR> </Context>
DRR> in a context configuration file could I reference the parameter from
DRR> inside a servlet? For example,
DRR> System.getProperties().getProperty("log4j-log-home); ?
DRR> David R Robison
DRR> Open Roads Consulting, Inc.
DRR> http://www.openroadsconsulting.com
DRR> -----Original Message-----
DRR> From: [email protected]
DRR> [mailto:[email protected]] On Behalf Of Jacob Kjome
DRR> Sent: Tuesday, March 04, 2003 4:20 PM
DRR> To: [email protected]
DRR> Subject: RE: [Barracuda] addEventlistener question
DRR> I see.
DRR> I suppose I should have stated that the InitContextListener has only
DRR> been tested to work with the autodiscovery of the location of the
DRR> WEB-INF/logs directory of the current running application in
DRR> Tomcat-4.1.x. You should be able to fix that by specifying your own
DRR> log4j-log-home context parameter...
DRR> <context-param>
DRR> <param-name>log4j-log-home</param-name>
DRR> <param-value>/usr/local/logs/tomcat</param-value>
DRR> </context-param>
DRR> Note that it is commented out in all examples in Barracuda and
DRR> BarracudaDiscRack's web.xml files. If provided, the InitContextListener
DRR> will use that path instead of dynamically figuring it out for itself.
DRR> Make sure to use forward slashes on Windows platforms such as...
DRR> C:/java/logs/tomcat
DRR> You, actually, don't have to edit web.xml if you don't want to. Most
DRR> any platform should provide proprietary server configuration to override
DRR> context parameters set in the web.xml. In Tomcat, you'd do...
DRR> <Context path="/myapp" docBase="myapp">
DRR> <Parameter name="log4j-log-home" override="true"
DRR> value="C:/java/logs/tomcat"/>
DRR> </Context>
DRR> Note you can put that in a Context configuration file instead of having
DRR> to edit server.xml.
DRR> Now, I'm not so familiar with configuration for Enhydra, but I imagine
DRR> it provides similar capabilities.
DRR> hope that helps!
DRR> Jake
DRR> At 03:54 PM 3/4/2003 -0500, you wrote:
DRR> This problem seems to be related to running from Enhydra. When I run
DRR> with Tomcat, the logging works normally.
DRR> David R Robison
DRR> Open Roads Consulting, Inc.
DRR> http://www.openroadsconsulting.com
DRR> -----Original Message-----
DRR> From: [email protected]
DRR> [mailto:[email protected]] On Behalf Of Jacob Kjome
DRR> Sent: Tuesday, March 04, 2003 11:46 AM
DRR> To: [email protected]
DRR> Subject: RE: [Barracuda] addEventlistener question
DRR> At 10:11 AM 3/4/2003 -0500, you wrote:
DRR> 4) I'm having trouble getting log4j to find any appenders. I used the
DRR> web.xml
DRR> and log4j.xml from the latest BarracudaDiscRack as an example. I tried
DRR> to
DRR> sepecify logging of debug message, but I got nothing conclusive.
DRR> Make sure you modify the log4j.xml. Note that the FileAppender is set
DRR> up with a dynamic variable: "${BarracudaDiscRack.log.home}". Change
DRR> "BarracudaDiscRack" to the name of your context. For instance, if your
DRR> context path is "/myapp", then it would be "${myapp.log.home}". Look at
DRR> sample.log4j.xml in BarracudaDiscRack and notice that this value is
DRR> dynamically generated by the build using a filter copy of
DRR> sample.log4j.xml to log4j.xml.
DRR> Now, in the web.xml, make sure that you have....
DRR> <listener>
DRR> <listener-class>
DRR> org.apache.log4j.servlet.InitContextListener
DRR> </listener-class>
DRR> </listener>
DRR> That listener class will determine the location of the [context
DRR> name].log.home variable dynamically. Output will go to file if you
DRR> haven't changed the <appender-ref ref="A2"/> in the <root> logger. Make
DRR> sure to add your other loggers to the log4j.xml if you want things
DRR> logged. You can also change this at runtime since confgureAndWatch is
DRR> done by default. Additionally, you can put the barracuda-config.jar in
DRR> your app and add the following to your event-gateway.xml file...
DRR> <event-gateway class="org.enhydra.barracuda.config.BarracudaConfig" />
DRR> Then you can go to http://localhost:8080/myapp/GetBConfig.event
DRR> You can now turn on and off various loggers and set different levels.
DRR> Let me know if you still have problems logging.
DRR> Jake
--
Best regards,
Jacob mailto:[email protected]