一个动态指定logback日志路径的方法

基于版本:

<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.3.14</version>
</dependency>

logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration >
  <statusListener class="ch.qos.logback.core.status.NopStatusListener"/>
  <!-- 日志级别从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL,如果设置为WARN,则低于WARN的信息都不会输出 -->

  <contextName>logback</contextName>
  <define name="LOG_PATH" class="fk.LogPathProperty" />
  <property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%logger{50}] - %msg%n"/>
  <property name="Console_Pattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%logger{50}] - %msg%n"/>
  <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <Pattern>${Console_Pattern}</Pattern>
      <charset>UTF-8</charset>
    </encoder>
  </appender>


  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOG_PATH}/total.log</file>
    <encoder>
      <pattern>${FILE_LOG_PATTERN}</pattern>
      <charset>UTF-8</charset>
    </encoder>
    <!-- 日志记录器的滚动策略,按日期,按大小记录 -->
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <!-- 每天日志归档路径以及格式 -->
      <fileNamePattern>${LOG_PATH}/total.log.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
      <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <maxFileSize>${LOG_FILE_MAX_SIZE:-2000MB}</maxFileSize>
        <maxHistory>${LOG_FILE_MAX_HISTORY:-180}</maxHistory>
      </timeBasedFileNamingAndTriggeringPolicy>
      <maxHistory>90</maxHistory>
    </rollingPolicy>
  </appender>


  <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
      <level>ERROR</level>
      <onMatch>ACCEPT</onMatch>
      <onMismatch>DENY</onMismatch>
    </filter>
    <encoder>
      <pattern>${FILE_LOG_PATTERN}</pattern>
    </encoder>
    <file>${LOG_PATH}/error.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
      <fileNamePattern>${LOG_PATH}/error.log.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
      <maxFileSize>${LOG_FILE_MAX_SIZE:-2000MB}</maxFileSize>
      <maxHistory>${LOG_FILE_MAX_HISTORY:-180}</maxHistory>
    </rollingPolicy>
    <maxHistory>90</maxHistory>
  </appender>


  <!-- 从低到高为:All < Trace < Debug < Info < Warn < Error < Fatal < OFF-->
  <root level="Info">
    <appender-ref ref="Console"/>
    <appender-ref ref="FILE"/>
    <appender-ref ref="ERROR_FILE"/>
  </root>


</configuration>

LogPathProperty.java:

public class LogPathProperty extends  PropertyDefinerBase {

  @Getter
  private static final String LOG_PATH ="logs";


  private static Module module = Module.COMMON;

  @SneakyThrows
  public static void setModule(Module module) {
    LogPathProperty.module = module;

    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    lc.reset();
    Method method = LoggerFactory.class.getDeclaredMethod("reset");
    method.setAccessible(true);
    method.invoke(null);
    LoggerFactory.getLogger(LogPathProperty.class);
  }

  @Override
  public String getPropertyValue() {
    return Paths.get(LOG_PATH,module.name().toLowerCase()).toString();
  }

}

在需要更改路径后写日志时,调用以下方法:

LogPathProperty.setModule(MODULE);
Logger log = LoggerFactory.getLogger(Main.class);
posted @ 2024-05-21 17:28  Jackie_JK  阅读(127)  评论(0编辑  收藏  举报