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

12,495

Solution 1

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

Solution 2

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

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

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

Share:
12,495

Related videos on Youtube

Sergey
Author by

Sergey

Just another yet Progremmer =)

Updated on September 21, 2022

Comments

  • Sergey
    Sergey over 1 year

    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?

  • Luis
    Luis almost 4 years
    Works nicely when running spring-boot from maven!