Skip to content

Commit

Permalink
replace ThreadLocal<DateFormat> with DateTimeFormatter (#3353)
Browse files Browse the repository at this point in the history
  • Loading branch information
shichaoyuan authored Mar 21, 2024
1 parent ffe0d1e commit 24c93c8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
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

0 comments on commit 24c93c8

Please sign in to comment.