Reputation: 129
I have defined a variable in web.xml inside a war package like:
<env-entry>
<env-entry-name>LOG_DIR</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>${CATALINA_HOME}</env-entry-value>
</env-entry>
Also I have logback.xml file in classpath. And I want to use this variable there:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/logs/WebStore.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%logger{36}] [%thread] %msg%n</pattern>
</encoder>
</appender>
When I start my tomcat instance I get LOG_DIR_IS_UNDEFINED folder in tomcat bin directory.
I know I can create a property file and import it into logback.xml but I do not want to create one more file and I am interested in using variable in this way. Is it possible?
Upvotes: 1
Views: 5088
Reputation: 2605
The environment entries can be visible in the configuration files but usually the programmer has to specifically set the environment scope. For example
$${env:USER}
something like that would work in Log4j.
See here: http://logging.apache.org/log4j/2.x/manual/lookups.html
See also this answer: How to use system environment variables in log4j.properties?
A similar mechanism could exist in logback, however I could not find something by quickly searching.
If everyone finds, you are more than welcome to edit this answer and add it.
In the particular case you could directly use the ${CATALINA_HOME}
inside the logback.xml
.
Upvotes: 1