设置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 @   逆火狂飙  阅读(2307)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2018-03-25 【高中数学/函数/零点】寻找函数y=2^x-x-1的零点
2018-03-25 linux CentOS6.5 yum安装mysql 5.6(转载&删改)
2018-03-25 Error: Cannot find module 'express' 之 解决方案
2018-03-25 使用Nodejs的Nodemailer通过163信箱发送邮件例程
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示