7 changed files with 259 additions and 30 deletions
-
57src/main/java/com/gaotao/common/log/Utf8RollingFileAppender.java
-
63src/main/java/com/gaotao/modules/schedule/CreateSplitRollTiming.java
-
75src/main/java/com/gaotao/modules/schedule/controller/ScheduleController.java
-
56src/main/java/com/gaotao/modules/schedule/service/impl/ScheduleServiceImpl.java
-
9src/main/java/com/gaotao/modules/schedule/service/impl/SchedulingServiceImpl.java
-
15src/main/resources/logback.xml
-
14src/main/resources/mapper/schedule/SchedulingMapper.xml
@ -0,0 +1,57 @@ |
|||||
|
package com.gaotao.common.log; |
||||
|
|
||||
|
import ch.qos.logback.classic.spi.ILoggingEvent; |
||||
|
import ch.qos.logback.core.rolling.RollingFileAppender; |
||||
|
|
||||
|
import java.io.File; |
||||
|
import java.io.FileInputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.io.OutputStream; |
||||
|
|
||||
|
/** |
||||
|
* 日志文件按 UTF-8 写,并在新文件开头加 BOM,记事本才能认出中文。 |
||||
|
* 没有 BOM 的旧文件(例如按 GBK 写的)先挪开,避免两种编码接在同一个文件里。 |
||||
|
*/ |
||||
|
public class Utf8RollingFileAppender extends RollingFileAppender<ILoggingEvent> { |
||||
|
|
||||
|
private static final byte[] UTF8_BOM = new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; |
||||
|
|
||||
|
@Override |
||||
|
public void openFile(String fileName) throws IOException { |
||||
|
moveFileWithoutBomAside(fileName); |
||||
|
super.openFile(fileName); |
||||
|
File file = new File(fileName); |
||||
|
if (!file.exists() || file.length() == 0) { |
||||
|
OutputStream os = getOutputStream(); |
||||
|
if (os != null) { |
||||
|
os.write(UTF8_BOM); |
||||
|
os.flush(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void moveFileWithoutBomAside(String fileName) { |
||||
|
File file = new File(fileName); |
||||
|
if (!file.exists() || file.length() < 3 || startsWithBom(file)) { |
||||
|
return; |
||||
|
} |
||||
|
File rotated = new File(file.getParentFile(), file.getName() + ".gbk"); |
||||
|
if (rotated.exists()) { |
||||
|
rotated = new File(file.getParentFile(), file.getName() + "." + System.currentTimeMillis() + ".gbk"); |
||||
|
} |
||||
|
if (!file.renameTo(rotated)) { |
||||
|
addWarn("Failed to move non-UTF-8 log aside: " + file.getAbsolutePath()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private boolean startsWithBom(File file) { |
||||
|
try (FileInputStream in = new FileInputStream(file)) { |
||||
|
byte[] head = new byte[3]; |
||||
|
int read = in.read(head); |
||||
|
return read == 3 && head[0] == UTF8_BOM[0] && head[1] == UTF8_BOM[1] && head[2] == UTF8_BOM[2]; |
||||
|
} catch (IOException e) { |
||||
|
addWarn("Failed to check log file header: " + file.getAbsolutePath(), e); |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
package com.gaotao.modules.schedule; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* Collects create-split step timings on the current request thread and prints one summary. |
||||
|
*/ |
||||
|
public final class CreateSplitRollTiming { |
||||
|
|
||||
|
private static final ThreadLocal<Session> SESSION = new ThreadLocal<>(); |
||||
|
|
||||
|
private CreateSplitRollTiming() { |
||||
|
} |
||||
|
|
||||
|
public static void begin() { |
||||
|
SESSION.set(new Session()); |
||||
|
} |
||||
|
|
||||
|
public static void record(String step, long elapsedMs) { |
||||
|
Session session = SESSION.get(); |
||||
|
if (session == null || step == null) { |
||||
|
return; |
||||
|
} |
||||
|
long cost = Math.max(elapsedMs, 0); |
||||
|
session.steps.add(step + "=" + cost + "毫秒"); |
||||
|
session.stepTotal += cost; |
||||
|
} |
||||
|
|
||||
|
public static String summary(String api, long totalMs, String context, String failedMessage) { |
||||
|
Session session = SESSION.get(); |
||||
|
long cost = Math.max(totalMs, 0); |
||||
|
StringBuilder sb = new StringBuilder(); |
||||
|
sb.append("[创建分卷] 汇总 接口=").append(api) |
||||
|
.append(" 总耗时=").append(cost).append("毫秒"); |
||||
|
if (context != null && !context.isEmpty()) { |
||||
|
sb.append(" ").append(context); |
||||
|
} |
||||
|
if (session != null && !session.steps.isEmpty()) { |
||||
|
long otherMs = cost - session.stepTotal; |
||||
|
if (otherMs < 0) { |
||||
|
otherMs = 0; |
||||
|
} |
||||
|
sb.append(" 提交及间隙=").append(otherMs).append("毫秒"); |
||||
|
for (String step : session.steps) { |
||||
|
sb.append(" | ").append(step); |
||||
|
} |
||||
|
} |
||||
|
if (failedMessage != null && !failedMessage.isEmpty()) { |
||||
|
sb.append(" | 失败=").append(failedMessage); |
||||
|
} |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static void clear() { |
||||
|
SESSION.remove(); |
||||
|
} |
||||
|
|
||||
|
private static final class Session { |
||||
|
private final List<String> steps = new ArrayList<>(); |
||||
|
private long stepTotal; |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue