4

I have a Google Web Toolkit (GWT) - Application which I run in three different modes:

  1. Deployed on Tomcat
  2. Hosted Mode
  3. Junit Test

How can I configure log4j.properties so that each of those modes logs correctly?

If I use ${catalina.base} I cannot use it in hosted mode and in Junit tests and if I just use a relative logs/myapplication.log, it won't work with Tomcat because then I get:

java.io.FileNotFoundException: log/myapplication.log (Keine Berechtigung)

I wouldn't mind having the logs in webapps/myapplication/logs if the log directory could be specified relatively to the application path but it would also be ok if the log files were just in /var/log/tomcat7/... or some other log folder.

At the moment my log4j.properties contains the following entries for file logging:

log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=${catalina.base}/logs/myapplication.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%c %C %d{ABSOLUTE} %-5p %C{1}: %m%n
log4j.appender.file.threshold=DEBUG
log4j.appender.file.Append=false
Konrad Höffner
  • 11,100
  • 16
  • 60
  • 118
  • 1
    I'm not sure if it will work for all of your situations, but you could try logging to stdout. Tomcat will capture that in a log file in it's logs directory. You don't say what the server is in hosted mode, but that may work there as well. I believe that JUnit will capture stdout as well. – GreyBeardedGeek May 10 '12 at 13:01
  • Yeah I already log to stdout additionally but I don't like to get all these exceptions when I start my program. – Konrad Höffner May 10 '12 at 13:13
  • 1
    what are 'all these exceptions' ? – GreyBeardedGeek May 11 '12 at 02:41
  • @GreyBeardedGeek Exeptions like `log4j:ERROR setFile(null,true) call failed. java.io.FileNotFoundException: /logs/user_sessions.html (Datei oder Verzeichnis nicht gefunden) at java.io.FileOutputStream.open(Native Method) at java.io.FileOutputStream.`... (one for each Appender) – Konrad Höffner May 11 '12 at 12:31
  • 1
    Don't you just need to find a way to set the catalina.base system property for the hosted and junit modes? I don't know much about GWT, but i'm sure this is possible. – Chris White May 11 '12 at 18:01
  • The problem is that I'm on Arch Linux and I don't even know if it has system properties like Windows. I mean I can do stuff like `echo $PATH` but if I do `echo ${catalina.base}` I get no answer, so I guess it is only defined by the Java virtual machine. – Konrad Höffner May 14 '12 at 09:37
  • Maybe you could try the method outlined here: http://stackoverflow.com/questions/216781/log4j-configuring-a-web-app-to-use-a-relative-path – jasop Aug 08 '12 at 10:30

1 Answers1

3

1. Deployed on Tomcat

I log to ${catalina.home}, but your configuration looks good still. I run my configuration locally on Windows and don't see any errors when I start the server about the fact that this folder cannot be found. If you want to point to somewhere on Windows, try adding the following to your arguments in your run configuration:

-Dcatalina.base="C:\somefolder\" -Dcatalina.home="C:\somefolder\"

2. Hosted Mode

In this mode you are running it locally in an IDE. So you don't need logging to a file. In this case, add a stdout appender to your log4j.properties

log4j.rootLogger=DEBUG, stdout, A

With this you will see the log messages in your console in your IDE

3. JUnit Test

Use a separate log configuration file for your testing (this is standard practice). This allows you to control the log level better, without affecting your production log levels.

jasop
  • 892
  • 11
  • 13