1

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?

Spyros K
  • 2,480
  • 1
  • 20
  • 37
Jack
  • 119
  • 1
  • 6
  • Possible duplicate: http://stackoverflow.com/questions/201188/using-system-environment-variables-in-log4j-xml-configuration – Mohit Nov 17 '15 at 04:52
  • Nice remark, but I think it is relevant, not a duplicate. This is for logback the other for log4. – Spyros K Nov 18 '15 at 09:18

1 Answers1

1

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.

Community
  • 1
  • 1
Spyros K
  • 2,480
  • 1
  • 20
  • 37
  • Basically I am wondering about life cycle, when logback.xml is loading, are environment entries from web.xml applied before to context and visible? – Jack Nov 18 '15 at 15:08
  • I would expect yes, as the application server would first consider the web.xml before loading the servlets which reference logback. Perhaps there is a way to change this (could be also server dependent) but it should not be probable. – Spyros K Nov 23 '15 at 09:30