Logback:如何从"tomcat/bin"更改日志目录?与应用相关? [英] Logback: how to change log directory from "tomcat/bin" to application related?
问题描述
我想使用 slf4j 和 logback 进行日志记录.
I want to use slf4j with logback for logging.
您可以在下面看到我的logback.xml:
You can see my logback.xml below:
<configuration>
<appender name="FILE-MODULE" class="ch.qos.logback.core.FileAppender">
<file>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>
问题是:当我将应用程序部署到Tomcat时,日志文件存储在 tomcat/bin 文件夹中,而我想将其存储在myapp文件夹中( tomcat/webapp/myapp ).
The problem is: when I deploy my application to Tomcat, log file is stored in tomcat/bin folder, and I want to store it in myapp folder (tomcat/webapp/myapp).
我该怎么做?
推荐答案
好吧,我解决了我的问题,但这并不是一个很好的解决方案(以我的观点).
Well, I solved my problem but it is not very good solution (by my opinion).
首先,我将绝对路径放置到.property文件中的日志文件中.例如:
First of all I put absolute path to log file in .property file. For example:
logback.log.location=d:\Tomcat\tomcat_8.0.0-RC5\webapps\module\logs
然后我在logback.xml中使用该属性:
Then I use that property in my 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>
More details you can see here. This is an example, that I use.
但是在上面的解决方案中,我们有特定于环境的日志绝对路径.这很丑.当然,我们可以使用系统变量 CATALINA_HOME 来避免绝对路径.但是,据我所知,CATALINA_HOME可以是未定义的.或者,我们可以使用另一个Tomcat实例,而不是CATALINA_HOME中的实例.
But in solution above we have environment specific absolute path to the logs. This is ugly. Of course, we can use system variable CATALINA_HOME to avoid absolute path. But, as I know, CATALINA_HOME can be undefined. Or, we can use another instance of tomcat, that is not in CATALINA_HOME.
也许有人有更好的解决方案,并且与环境无关?
Maybe someone have more nice solution that will be environment independent?
更新
另一种解决方案:
仅在logback.xml中使用相对(到tomcat \ bin)路径而不是绝对路径:
Just use relative (to tomcat\bin) path instead absolute in logback.xml:
<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>
这是我尝试实现的第一个想法.我不知道,为什么以前不起作用.也许还有其他问题.此外,此和
It was the first idea, that I try to implement. I don't know, why it didn't work before. Maybe there were other problems. Moreover this and this articles confused me.
但是现在此解决方案可以正常工作.这正是我在寻找=)
But now this solution work fine. This is exactly that I am looking for =)
这篇关于Logback:如何从"tomcat/bin"更改日志目录?与应用相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!