设置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--

posted @ 2020-03-25 08:19  逆火狂飙  阅读(2240)  评论(0编辑  收藏  举报
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东