I posted a question yesterday in the Other Open Source forum but didn't get any responses, so I'm reposting here with additional information because this now appears to be a Tomcat issue rather than a log4j issue. I don't like to post in more than one forum, but replying to myself in the other forum doesn't seem productive, especially since I think I've pretty well determined that this is a Tomcat configuration issue. This is for application logging, not Tomcat logging. For testing, I've created a simple project with a servlet like so:
Here is the log4j.properties file which is located in the applications WEB-INF/classes directory.
The console appender is working as those messages appear in catalina.out, but the file for the file appender is never getting created. The only clue I've been able to find after a lot of searching indicates that I may need to add an entry to the catalina.policy file in order to allow the application to create the log file, but in all the guides to log4j that I've read, that is the only place where this issue of policies has been mentioned. Could that be the problem? If so, could someone expand a bit on how to modify that policy file? I'm not at all familiar with Tomcat security and this policy file.
I should also mention that this app DOES log to the file when I run it on my local test server (with the path changed accordingly), which is Tomcat running on Windows. The failure occurs when it's deployed to the RedHat server.
Any tips are appreciated. I'm now into my second day of trying to get log4j working and I'm getting a little frustrated. In the meantime, I'm going to try creating the log file manually and see if it gets appended to, and I'll do some more reading about this catalina.policy file to see if I can understand how it works.
"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
My #1 suspicion has to do with file access rights. If Tomcat can't write to /var/tmp/mylog.log, it's Game Over. And because in general, Log4j won't actually crash the app, you'd be able to run, but with reduced (if any) logging abilities.
Incidentally, the logical place to put logs in Red Hat is /var/log and in cases where logfiles are complex, I'd recommend a subdirectory to hold them:
I tend towards multi-generation logfiles, so an app-specific log directory is pretty much essential, but it can also reduce problems for single logfiles, since the app doesn't have to worry about violating the constraints of the /var/log directory.
An IDE is no substitute for an Intelligent Developer.
That was my thought, so I've tried several different locations, including the tomcat/log dir and my home dir. I'm currently testing in /var/tmp simply because it has 777 rights, and the file I pre-created has 777 rights, but I still can't write to it. It's very puzzling which is why I'm wondering if I need an entry in catalina.policy for either the application that's logging or for log4j itself. Oddly, it's not throwing any IOExceptions or FileNotFoundExceptions, so I haven't got a clue as to where the problem lies.
I'll try creating an application specific folder with the command you posted.
Next week, we have some system downtime, so I can restart Tomcat with impunity to test this. So I can turn on debugging for the Security Manager and add policy entries. Maybe that will give me a clue.
Thanks for responding. When I get this conquered I'll post the solution so others that find this thread won't have to pull their hair out like I've been doing for the last 4 days.
Try creating and writing a simple text file from your webapp. If the application can do so, so can log4j, since the same rules apply. If it can't, at least you can inspect the File context and maybe get a hint.
If that works, double-check the log4j configuration file. Also, if this is a complex app using things like Hibernate, be aware that other components may be mucking around with logging, adding other logging systems than log4j. In fact, for Hibernate in particular, I have to include slf4j in order to ensure that all the loggers co-operate.
You do not have to configure anything in Tomcat itself to make application logging work. The Tomcat logger is completely independent of the loggers in the various webapps.