package com.yanzuoguang.log; import com.yanzuoguang.util.base.CollectionString; import com.yanzuoguang.util.thread.ThreadNext; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; import java.util.Collections; import java.util.Comparator; /** * 将请求次数日志输出 * * @author 颜佐光 */ @Component public class LogPrint implements ThreadNext.Next, InitializingBean { private final LogConfig logConfig; private final LogCountTime logCountTime; public LogPrint(LogConfig logConfig, LogCountTime logCountTime) { this.logConfig = logConfig; this.logCountTime = logCountTime; } @Override public void afterPropertiesSet() { ThreadNext.start(this, "aspectLogTime"); } @Override public boolean next() { LogCountResult todayResult = logCountTime.getTodayResult(); if (todayResult.getList().isEmpty()) { return true; } // 按照使用总时间升序排序 todayResult.getList().sort(Comparator.comparingLong(LogUrlCountVo::getTotalTime)); // 逆转排序 Collections.reverse(todayResult.getList()); // 打印日志 String tag = "从" + todayResult.getTodayTime() + "时执行次数:"; System.out.println(CollectionString.getCollectionString(tag, todayResult.getList())); // 下次继续执行 return true; } @Override public int getNextTime() { return logConfig.getLogCountTime(); } }