replace ThreadLocal<DateFormat> with DateTimeFormatter (#3353)

This commit is contained in:
袁世超 2024-03-21 13:41:11 +08:00 committed by GitHub
parent ffe0d1ec81
commit 24c93c8a1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 29 deletions

View File

@ -15,6 +15,9 @@
*/
package com.alibaba.csp.sentinel.eagleeye;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
@ -139,15 +142,12 @@ final class EagleEyeCoreUtils {
return appender;
}
private static final ThreadLocal<FastDateFormat> dateFmt = new ThreadLocal<FastDateFormat>() {
@Override
protected FastDateFormat initialValue() {
return new FastDateFormat();
}
};
private static final DateTimeFormatter dateFmt = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
.withZone(ZoneId.systemDefault());
public static String formatTime(long timestamp) {
return dateFmt.get().format(timestamp);
return dateFmt.format(Instant.ofEpochMilli(timestamp));
}
public static String getSystemProperty(String key) {

View File

@ -14,9 +14,9 @@ package com.alibaba.csp.sentinel.log.jul;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
@ -25,18 +25,14 @@ import java.util.logging.LogRecord;
*/
class CspFormatter extends Formatter {
private final ThreadLocal<SimpleDateFormat> dateFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
@Override
public SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
}
};
private final DateTimeFormatter dateFormat = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
.withZone(ZoneId.systemDefault());
@Override
public String format(LogRecord record) {
final DateFormat df = dateFormatThreadLocal.get();
StringBuilder builder = new StringBuilder(1000);
builder.append(df.format(new Date(record.getMillis()))).append(" ");
builder.append(dateFormat.format(Instant.ofEpochMilli(record.getMillis()))).append(" ");
builder.append(record.getLevel().getName()).append(" ");
builder.append(formatMessage(record));

View File

@ -19,7 +19,9 @@ import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
@ -33,12 +35,9 @@ import java.util.logging.LogRecord;
class DateFileLogHandler extends Handler {
private final ThreadLocal<SimpleDateFormat> dateFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
@Override
public SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
private final DateTimeFormatter dateFormat = DateTimeFormatter
.ofPattern("yyyy-MM-dd")
.withZone(ZoneId.systemDefault());
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(
1,
@ -120,8 +119,7 @@ class DateFileLogHandler extends Handler {
private boolean logFileExits() {
try {
SimpleDateFormat format = dateFormatThreadLocal.get();
String fileName = pattern.replace("%d", format.format(new Date()));
String fileName = pattern.replace("%d", dateFormat.format(Instant.now()));
// When file count is not 1, the first log file name will end with ".0"
if (count != 1) {
fileName += ".0";
@ -139,8 +137,7 @@ class DateFileLogHandler extends Handler {
if (handler != null) {
handler.close();
}
SimpleDateFormat format = dateFormatThreadLocal.get();
String newPattern = pattern.replace("%d", format.format(new Date()));
String newPattern = pattern.replace("%d", dateFormat.format(Instant.now()));
// Get current date.
Calendar next = Calendar.getInstance();
// Begin of next date.