Optimize logging performance
This commit is contained in:
@@ -19,11 +19,10 @@ package org.jackhuang.hmcl.util;
|
|||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.MessageFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
|
|
||||||
@@ -93,21 +92,19 @@ public final class Logging {
|
|||||||
private static final class DefaultFormatter extends Formatter {
|
private static final class DefaultFormatter extends Formatter {
|
||||||
|
|
||||||
static final DefaultFormatter INSTANCE = new DefaultFormatter();
|
static final DefaultFormatter INSTANCE = new DefaultFormatter();
|
||||||
private final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
|
private static final MessageFormat format = new MessageFormat("[{0,date,HH:mm:ss}] [{1}.{2}/{3}] {4}\n");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String format(LogRecord record) {
|
public String format(LogRecord record) {
|
||||||
String date = format.format(new Date(record.getMillis()));
|
String log = format.format(new Object[]{
|
||||||
String log = String.format("[%s] [%s.%s/%s] %s%n",
|
new Date(record.getMillis()),
|
||||||
date, record.getSourceClassName(), record.getSourceMethodName(),
|
record.getSourceClassName(), record.getSourceMethodName(), record.getLevel().getName(),
|
||||||
record.getLevel().getName(), record.getMessage()
|
record.getMessage()
|
||||||
);
|
});
|
||||||
ByteArrayOutputStream builder = new ByteArrayOutputStream();
|
|
||||||
if (record.getThrown() != null)
|
if (record.getThrown() != null)
|
||||||
try (PrintWriter writer = new PrintWriter(builder)) {
|
log += StringUtils.getStackTrace(record.getThrown());
|
||||||
record.getThrown().printStackTrace(writer);
|
|
||||||
}
|
return log;
|
||||||
return log + builder.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user