設置logback的log文件地址為程序運行的當前目錄


這個需求雖然怪異,卻也不是無事生非,在以jar包為執行主體的程序中就會遇到。

設置方法就是指定Log_HOME為./,其在如下配置文件的第四行:

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <!-- Where are log files -->
    <property name="LOG_HOME" value="./" /> 
    
    <!-- Output to Console -->
    <appender name="STDOUT"
        class="ch.qos.logback.core.ConsoleAppender">
        <encoder
            class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--%d:date,%thread:thread,%-5level:error/debug/info... %msg:message,%n:new line -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n</pattern>
        </encoder>
    </appender>
    
    <!-- Output to File -->
    <appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/rtm32ook.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxFileSize>1000MB</maxFileSize>
            <MaxHistory>60</MaxHistory>
        </rollingPolicy>
        
        <encoder
            class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--%d:date,%thread:thread,%-5level:error/debug/info... %msg:message,%n:new line -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} -%msg%n</pattern>
        </encoder>
    </appender>

    <!-- log level TRACE, DEBUG, INFO, WARN, ERROR, ALL and OFF,default:DEBUG。-->
    <root level="INFO">
        <appender-ref ref="STDOUT" /> <!-- show log on console -->
        <appender-ref ref="FILE" /> <!-- show log in file -->
    </root>
</configuration>

--2020-03-25--


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM