Warm tip: This article is reproduced from stackoverflow.com, please click
java logback logging tomcat slf4j

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

发布于 2020-04-13 10:05:13

I want to use slf4j with logback for logging.

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>

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

How can I do that?

Questioner
Sergey
Viewed
89
Sergey 2015-04-03 21:18

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:

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

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.

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?


UPDATE

Another solution:

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