0

I want to be able to log at different levels depending on what server I deploy my Java web application on. For example, on my test server I would like to log at the DEBUG level and on my production server I would like to log at the WARN level. Currently my log4.xml file is located inside of my WAR file. I want to be able to configure the logging level of my web app using Tomcat context parameters. Is this possible?

I have seen that environment variables and system properties can be used. For example, ${catalina.home} gets interpreted. Is this same functionality available for substituting in Tomcat context parameter values?

For example, this would be the log4j.xml snippet:

<level value="${log.level}" 

Where log.level is defined in %TOMCAT_HOME%/conf/context.xml:

<Parameter name="log.level" value="DEBUG" />

I have tried the above and it does not seem to work. Google has not turned up anything for me related to using context parameters in log4j configuration files. I found this SO question doing something similar using system properties, but it does not use Tomcat context parameters so it is not quite what I want. Is a context parameter value substitution capability available in log4j configuration? Do I need to explore other options such using an external log4j.xml file?

Community
  • 1
  • 1
BennyMcBenBen
  • 1,438
  • 2
  • 20
  • 37

2 Answers2

1

I don't think that Log4J does this natively. I recall that the Spring Web Framework came with a listener configured via web.xml that might do something a little similar. If you really wanted to, I think you could could grab the code for that and use it to create something that does exactly want you want.

Robert
  • 2,441
  • 21
  • 12
  • We ended up using an external log configuration file using [Log4jWebConfigurer](http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/util/Log4jWebConfigurer.html) and the log4jConfigLocation property with a file value such as file:/path/to/log4j.xml. Later on we decided to switch to Logback and we used Spring Extensions for Logback, which has a similar property called logbackConfigLocation. This allows us to use different config files for the different environments we deploy to. – BennyMcBenBen Sep 28 '12 at 02:39
0

One method I've been using recently for extremely fine-grained control over logging is the log4 appender. It's a little more work than editing your config file, but gives you an incredible amount of flexibility. See my answer here for a quick example on how one of these would be configured.

Community
  • 1
  • 1
josh-cain
  • 4,997
  • 7
  • 35
  • 55