Is there a convenient way to have different logging for dev vs prod in tomcat?

32 Views Asked by At

I want my tomcat web app to use one logging configuration file during development, but a different logging configuration file for deployment.

Is there any way to accomplish this? If I put a logging.properties file in the root of my class loader for deployment, that seems to take priority over everything else, so I don’t see a way to add a runtime jvm flag or anything like that to override the configuration during development.

1

There are 1 best solutions below

1
UDID On

You can use -D command-line option in Tomcat. Create two properties file for development and deployment.

Add something like this in development let say log.properites

# Development logging configuration 
logging.level.root=DEBUG 
logging.level.org.apache.tomcat.util.log=DEBUG  
logging.level.org.apache.tomcat.http.impl.connector.ClassLoader=DEBUG          
logging.level.org.apache.tomcat.util.log=DEBUG 
logging.level.org.apache.tomcat.util.log=DEBUG 
logging.level.org.apache.catalina.connector.ClassLoader=DEBUG 
logging.level.org.apache.catalina.connector.ClassLoader=DEBUG 
logging.level.org.apache.catalina.core.Controller=DEBUG 
logging.level.org.apache.catalina.core.Controller=DEBUG 
logging.level.org.apache.catalina.connector=DEBUG 
logging.level.org.apache.catalina.core.Controller=DEBUG

In deployment log.properties add

properties # Deployment logging configuration 
logging.level.root=ERROR

Now use -D command-line option

java -Dlogging.config=logging.properties -jar mywar.war

What will happen internally is -Dlogging.config=log.properties option overrides the development logging configuration in the log.properties file, and the application will use the deployment logging configuration in the log.properties file.

note : I have tested in my system.