Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

替换ThreadLocal<SimpleDateFormat>以适配Java21虚拟线程 #3353

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,15 +142,12 @@ public static StringBuilder appendLog(String str, StringBuilder appender, char d
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

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;

Expand All @@ -25,18 +25,14 @@
*/
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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

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;
Expand All @@ -33,12 +35,9 @@

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,
Expand Down Expand Up @@ -120,8 +119,7 @@ public void setFormatter(Formatter newFormatter) {

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";
Expand All @@ -139,8 +137,7 @@ private void rotateDate() {
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.
Expand Down
Loading