Authored by Gino Zhang

支持动态调整日志级别

@@ -16,6 +16,8 @@ import java.util.concurrent.TimeUnit; @@ -16,6 +16,8 @@ import java.util.concurrent.TimeUnit;
16 */ 16 */
17 public final class LogUtils { 17 public final class LogUtils {
18 18
  19 + private static final Logger ROOT_LOGGER = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
  20 +
19 private static final Map<String, Level> sourceLevelMap = new ConcurrentHashMap<>(); 21 private static final Map<String, Level> sourceLevelMap = new ConcurrentHashMap<>();
20 22
21 // 最大支持打开五分钟 23 // 最大支持打开五分钟
@@ -38,8 +40,8 @@ public final class LogUtils { @@ -38,8 +40,8 @@ public final class LogUtils {
38 return "The logger [" + loggerName + "] is not found."; 40 return "The logger [" + loggerName + "] is not found.";
39 } 41 }
40 42
41 - final Level srcLevel = logger.getLevel();  
42 - if (srcLevel == null || level == srcLevel) { 43 + final Level srcLevel = logger.getLevel() != null ? logger.getLevel() : ROOT_LOGGER.getLevel();
  44 + if (level == srcLevel) {
43 return "The logger [" + loggerName + "] is [" + levelStr + "] already."; 45 return "The logger [" + loggerName + "] is [" + levelStr + "] already.";
44 } 46 }
45 47
@@ -177,11 +177,21 @@ public class ToolsController { @@ -177,11 +177,21 @@ public class ToolsController {
177 } 177 }
178 178
179 179
  180 + /**
  181 + * 用于动态地变更日记级别。
  182 + * @param loggerName 日志名称,有三类:
  183 + * 第一种是ROOT,对所有日志都起作用,
  184 + * 第二种是具体的日志名称,如CACHE_MATCH_REQUEST,
  185 + * 第三种就是全限定类名,如com.yoho.search.service.servicenew.impl.ProductListServiceImpl。
  186 + * @param level 调整目标级别,支持ALL/TRACE/DEBUG/INFO/WARN/ERROR/OFF这些取值
  187 + * @param seconds 调整日志级别的有效时间,默认为60秒,最多只能为300秒,到了时间后变更为原来的日志级别。
  188 + * @return
  189 + */
180 @RequestMapping(method = RequestMethod.GET, value = "/changeLogLevel") 190 @RequestMapping(method = RequestMethod.GET, value = "/changeLogLevel")
181 @ResponseBody 191 @ResponseBody
182 public SearchApiResult changeLogLevel(@RequestParam(defaultValue = "ProductListServiceImpl") String loggerName, 192 public SearchApiResult changeLogLevel(@RequestParam(defaultValue = "ProductListServiceImpl") String loggerName,
183 @RequestParam(defaultValue = "DEBUG") String level, 193 @RequestParam(defaultValue = "DEBUG") String level,
184 - @RequestParam(defaultValue = "60") int seconds) throws Exception { 194 + @RequestParam(defaultValue = "60") int seconds) {
185 try { 195 try {
186 return new SearchApiResult().setData(LogUtils.changeRootLogLevel(loggerName, level, seconds)); 196 return new SearchApiResult().setData(LogUtils.changeRootLogLevel(loggerName, level, seconds));
187 } catch (Exception e) { 197 } catch (Exception e) {
@@ -21,6 +21,23 @@ @@ -21,6 +21,23 @@
21 </encoder> 21 </encoder>
22 </appender> 22 </appender>
23 23
  24 + <!-- DEBUG级别日志 appender -->
  25 + <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
  26 + <!-- 过滤器,过滤掉 TRACE 和 DEBUG 和 INFO 级别的日志 -->
  27 + <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  28 + <!-- 按天回滚 daily -->
  29 + <fileNamePattern>${catalina.home}/logs/%d{yyyy-MM-dd}/debug.log</fileNamePattern>
  30 + <!-- 日志最大的保存天数 -->
  31 + <maxHistory>${maxHistory}</maxHistory>
  32 + </rollingPolicy>
  33 + <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
  34 + <maxFileSize>${maxFileSize}</maxFileSize>
  35 + </triggeringPolicy>
  36 + <encoder>
  37 + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
  38 + </encoder>
  39 + </appender>
  40 +
24 <!-- WARN级别日志 appender --> 41 <!-- WARN级别日志 appender -->
25 <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> 42 <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
26 <!-- 过滤器,过滤掉 TRACE 和 DEBUG 和 INFO 级别的日志 --> 43 <!-- 过滤器,过滤掉 TRACE 和 DEBUG 和 INFO 级别的日志 -->
@@ -191,6 +208,8 @@ @@ -191,6 +208,8 @@
191 <appender-ref ref="STDOUT" /> 208 <appender-ref ref="STDOUT" />
192 <!-- 文件输出 --> 209 <!-- 文件输出 -->
193 <appender-ref ref="WARN" /> 210 <appender-ref ref="WARN" />
  211 + <!-- 文件输出 -->
  212 + <appender-ref ref="DEBUG" />
194 </root> 213 </root>
195 214
196 <!-- http请求处理超时日志 --> 215 <!-- http请求处理超时日志 -->