好吧,我解决了我的问题,但这不是很好的解决方案(根据我的观点)。
首先,我将日志文件的绝对路径放入 .property 文件中。例如:
logback.log.location=d:\Tomcat\tomcat_8.0.0-RC5\webapps\module\logs
然后我在 logback.xml 中使用该属性:
<configuration>
<property file="src\main\resources\system_config.properties" />
<appender name="FILE-MODULE" class="ch.qos.logback.core.FileAppender">
<file>${logback.log.location}\module.log</file>
<encoder>
<pattern>
%date %level [%thread] %logger{10} [%file:%line] %msg%n
</pattern>
</encoder>
</appender>
<logger name="module" level="debug" additivity="false">
<appender-ref ref="FILE-MODULE" />
</logger>
</configuration>
更多详情您可以查看here http://logback.qos.ch/manual/configuration.html#variableSubstitution。这是一example https://github.com/qos-ch/logback/blob/master/logback-examples/src/main/java/chapters/configuration/variableSubstitution3.xml,我用的。
但在上面的解决方案中,我们有特定于环境的日志绝对路径。这太丑了。当然,我们可以使用系统变量卡塔琳娜_HOME以避免绝对路径。但是,据我所知,CATALINA_HOME 可能是未定义的。或者,我们可以使用不在 CATALINA_HOME 中的另一个 tomcat 实例。
也许有人有更好的独立于环境的解决方案?
UPDATE
另一个解决方案:
只需在 logback.xml 中使用相对(到 tomcat\bin)路径而不是绝对路径:
<configuration>
<appender name="FILE-MODULE" class="ch.qos.logback.core.FileAppender">
<file>..\webapps\module\module.log</file>
<encoder>
<pattern>
%date %level [%thread] %logger{10} [%file:%line] %msg%n
</pattern>
</encoder>
</appender>
<logger name="module" level="debug" additivity="false">
<appender-ref ref="FILE-MODULE" />
</logger>
</configuration>
这是我尝试实施的第一个想法。我不知道,为什么之前不起作用。也许还有其他问题。而且this http://java.dzone.com/tips/do-not-use-relative-path and this http://baptiste-wicht.com/posts/2010/08/do-not-use-relative-path-with-logback.html文章让我很困惑。
但现在这个解决方案工作得很好。这正是我正在寻找的 =)