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

后端 未结 2 671
长发绾君心
长发绾君心 2021-02-15 10:53

I want to use slf4j with logback for logging.

You can see my logback.xml below:


    

        
相关标签:
2条回答
  • 2021-02-15 11:36

    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.

    0 讨论(0)
  • 2021-02-15 11:40

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

    0 讨论(0)
提交回复
热议问题