Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 896
Sergey Avatar asked Apr 03 '15 10:04

Sergey


People also ask

How do I view Tomcat application logs?

The main Apache Tomcat configuration file is at /opt/bitnami/tomcat/conf/server. xml. Once Apache Tomcat starts, it will create several log files in the /opt/bitnami/tomcat/logs directory.

Where should Logback xml be stored?

In a Spring Boot application, you can put the Logback. xml file in the resources folder. If your Logback. xml file is outside the classpath, you need to point to its location using the Logback.


1 Answers

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

like image 125
Sergey Avatar answered Oct 06 '22 20:10

Sergey