Skip to content

Instantly share code, notes, and snippets.

@alainpham
Created March 16, 2024 00:20
Show Gist options
  • Select an option

  • Save alainpham/55001997ca9287912807b4012a0dddc1 to your computer and use it in GitHub Desktop.

Select an option

Save alainpham/55001997ca9287912807b4012a0dddc1 to your computer and use it in GitHub Desktop.
visual cron logs replayer simulator
package demo;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.time.ZoneId;
import java.util.TimeZone;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Application {
private static final String TZ = "UTC";
private static Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) throws InterruptedException, IOException {
String filePath = System.getenv("LOG_FILE_PATH"); // export LOG_FILE_PATH=/mnt/c/Users/alain/Downloads/log_server20240312.txt
String content = simulateFile(filePath);
String contentLines[] = content.split("\n");
while (true) {
Thread.sleep(15000);
logger.debug("Starting from scratch ...");
for (String line : contentLines) {
String[] elements=line.split("\t");
String timestamp = elements[0];
String level = elements[1];
String message = elements[2];
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone(TZ));
Date parsedDate;
try {
parsedDate = dateFormat.parse(timestamp);
long secondsOfDay = parsedDate.toInstant().atZone(ZoneId.of(TZ)).toLocalTime().toSecondOfDay();
long currentSecondsOfDay = (new Date()).toInstant().atZone(ZoneId.of(TZ)).toLocalTime()
.toSecondOfDay();
logger.debug("timestamp: " + timestamp + " // " + parsedDate);
logger.debug("Log Seconds of Today: " + secondsOfDay + "// Current Seconds of Today: "
+ currentSecondsOfDay);
while (currentSecondsOfDay < secondsOfDay) {
logger.debug("Log Seconds of Today: " + secondsOfDay);
logger.debug("Current Seconds of Today: " + currentSecondsOfDay);
Thread.sleep(2000);
currentSecondsOfDay = (new Date()).toInstant().atZone(ZoneId.of(TZ)).toLocalTime()
.toSecondOfDay();
}
logger.debug("Processing line: " + line);
if (level.equals("Err")) {
logger.error(message);
} else if (level.equals("Debug")) {
logger.debug(message);
} else{
logger.info(message);
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
}
public static String simulateFile(String filePath) throws IOException {
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
int i = 0;
while ((line = reader.readLine()) != null) {
String pattern = "\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}:\\d{2}";
Pattern regex = Pattern.compile(pattern);
Matcher matcher = regex.matcher(line);
if (matcher.find()) {
if (i > 0) {
content.append("\n");
}
content.append(line);
} else {
content.append(" " + line);
}
i++;
}
}
return content.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment