java Logback:如何将日志目录从“tomcat/bin”更改为与应用程序相关的目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29430249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:15:53  来源:igfitidea点击:

Logback: how to change log directory from "tomcat/bin" to application related?

javatomcatloggingslf4jlogback

提问by Sergey

I want to use slf4jwith logbackfor logging.

我想使用带有logback 的slf4j进行日志记录。

You can see my logback.xml below:

你可以在下面看到我的 logback.xml:

<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>

The problem is: when I deploy my application to Tomcat, log file is stored in tomcat/binfolder, and I want to store it in myapp folder (tomcat/webapp/myapp).

问题是:当我将应用程序部署到 Tomcat 时,日志文件存储在tomcat/bin文件夹中,我想将其存储在 myapp 文件夹(tomcat/webapp/myapp)中。

How can I do that?

我怎样才能做到这一点?

回答by Sergey

Well, I solved my problem but it is not very good solution (by my opinion).

好吧,我解决了我的问题,但这不是很好的解决方案(我认为)。

First of all I put absolute path to log file in .property file. For example:

首先,我将日志文件的绝对路径放在 .property 文件中。例如:

logback.log.location=d:\Tomcat\tomcat_8.0.0-RC5\webapps\module\logs

Then I use that property in my logback.xml:

然后我在 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.

您可以在此处查看更多详细信息。这是我使用的一个例子

But in solution above we have environment specific absolute path to the logs. This is ugly. Of course, we can use system variable CATALINA_HOMEto 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.

但是在上面的解决方案中,我们有环境特定的日志绝对路径。这是丑陋的。当然,我们可以使用系统变量CATALINA_HOME来避免绝对路径。但是,据我所知,CATALINA_HOME 可以是未定义的。或者,我们可以使用另一个不在 CATALINA_HOME 中的 tomcat 实例。

Maybe someone have more nice solution that will be environment independent?

也许有人有更好的解决方案,将与环境无关?



UPDATE

更新

Another solution:

另一种解决方案:

Just use relative (to tomcat\bin) path instead absolute in logback.xml:

只需在 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>

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 thisand thisarticles confused me.

这是我尝试实施的第一个想法。我不知道,为什么它以前不起作用。也许还有其他问题。而且文章让我感到困惑。

But now this solution work fine. This is exactly that I am looking for =)

但现在这个解决方案工作正常。这正是我正在寻找的 =)

回答by Praval Sharma

If you do not want to save your logs in Tomcat's bin folder then use ${catalina.base}as file path prefix in logback.xmlfile.

如果您不想将日志保存在 Tomcat 的 bin 文件夹中,请${catalina.base}logback.xml文件中用作文件路径前缀。

<file>${catalina.base}/logs/log.log</file>

Here log file will be saved in existing logs folder of Tomcat instead of bin.

这里的日志文件将保存在 Tomcat 现有的日志文件夹中,而不是保存在 bin 中。