1 package com.x.x.x.listener; 2 3 import ch.qos.logback.classic.Level; 4 import ch.qos.logback.classic.Logger; 5 import ch.qos.logback.classic.LoggerContext; 6 import ch.qos.logback.classic.spi.LoggerContextListener; 7 import ch.qos.logback.core.Context; 8 import ch.qos.logback.core.spi.ContextAwareBase; 9 import ch.qos.logback.core.spi.LifeCycle; 10 11 public class CustomLogContextListener extends ContextAwareBase implements LoggerContextListener, LifeCycle { 12 13 /** 存储日志路径标识 */ 14 public static final String LOG_PAHT_KEY = "LOG_PATH"; 15 16 @Override 17 public boolean isResetResistant() { 18 return false; 19 } 20 21 @Override 22 public void onStart(LoggerContext loggerContext) { 23 } 24 @Override 25 public void onReset(LoggerContext loggerContext) { 26 } 27 @Override 28 public void onStop(LoggerContext loggerContext) { 29 } 30 @Override 31 public void onLevelChange(Logger logger, Level level) { 32 } 33 34 @Override 35 public void start() { 36 String s = System.getProperty("user.dir") + "/logs/"; 37 System.setProperty(LOG_PAHT_KEY, s); 38 Context context = getContext(); 39 context.putProperty(LOG_PAHT_KEY, s); 40 } 41 @Override 42 public void stop() { 43 } 44 @Override 45 public boolean isStarted() { 46 return false; 47 } 48 }
在日志配置文件中引入“CustomLogContextListener”(<contextListener class="com.x.x.x.listener.CustomLogContextListener" />)类,
就可以直接通过声明的变量名称使用路径(${LOG_PATH}),LOG_PATH就是在CustomLogContextListener类中指定的变量名称
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <configuration scan="true" scanPeriod="20 seconds" debug="false"> 3 <contextListener class="com.x.x.x.listener.CustomLogContextListener" /> 4 <property name="PROJECT_NAME" value="xxxx" /> 5 <!-- 控制台输出日志 --> 6 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 7 <layout class="ch.qos.logback.classic.PatternLayout"> 8 <pattern>%d{yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX} ${PROJECT_NAME} [%thread] %-5level %logger{80}[%line] %msg%n</pattern> 9 </layout> 10 </appender> 11 12 13 <!-- 文件输出日志 (文件大小策略进行文件输出,超过指定大小对文件备份)--> 14 <appender name="FILE-debug" class="ch.qos.logback.core.rolling.RollingFileAppender"> 15 <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> 16 <level>DEBUG</level> 17 </filter> 18 <File>${LOG_PATH}/${PROJECT_NAME}_debug.log</File> 19 <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> 20 <FileNamePattern>${LOG_PATH}/${PROJECT_NAME}_debug.%i.log 21 </FileNamePattern> 22 <MinIndex>1</MinIndex> 23 <MaxIndex>10</MaxIndex> 24 </rollingPolicy> 25 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> 26 <MaxFileSize>10MB</MaxFileSize> 27 </triggeringPolicy> 28 <encoder> 29 <pattern>%d{yyyy-MM-dd‘T‘HH:mm:ss.SSSXXX} ${PROJECT_NAME} [%thread] %-5level %logger{80}[%line] %msg%n</pattern> 30 </encoder> 31 </appender> 32 <root level="INFO"> 33 <appender-ref ref="STDOUT"/> 34 <appender-ref ref="FILE-debug" /> 35 <appender-ref ref="FILE-error" /> 36 </root> 37 </configuration>
原文:https://www.cnblogs.com/donfaquir/p/12198570.html