Tomcat log4j log regarding application path?

I have a Google Web Toolkit (GWT) - an application that I run in three different modes:

  • Deployed on Tomcat
  • Hosting
  • Junit test

How to configure log4j.properties so that each of these modes works correctly?

If I use ${catalina.base} , I cannot use it in host mode and in Junit tests, and if I just use relative logs/myapplication.log , it will not work with Tomcat, because then I get:

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

I would not mind having logs in the webapps / myapplication / logs files if the log directory could be specified relative to the path to the program, but it would also be nice if the log files were only in / var / log / tomcat 7 / ... or any other log folder.

Currently my log4j.properties contains the following entries for registering files:

 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 
+4
source share
1 answer

1. Deployed on Tomcat

I will write in $ {catalina.home}, but your configuration looks good. I run my configuration locally on Windows and do not see any errors when starting the server that this folder was not found. If you want to specify somewhere on Windows, try adding the following arguments to your startup configuration:

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

2. Hosting

In this mode, you run it locally in the IDE. Therefore, you do not need to log to a file. In this case add app stdout to your log4j.properties

 log4j.rootLogger=DEBUG, stdout, A 

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

3. JUnit test

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

+2
source

Source: https://habr.com/ru/post/1411746/


All Articles