I am trying to add logging to an application for collaborating such that the user's actions (like chat messages and UI clicks) are logged to a local text file. Later I will synchronize the log text files from different users (using different computers) with the goal of having the logs in chronological order by timestamp. I am wondering how I should record the timestamp to avoid relying on the system clock since that will likely cause inaccurate syncing of the log files.
I am currently using the java Instant class (import java.time.Instant;), but I am not sure if this is the way to go.
String timestamp = Instant.now().toString();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("actionLogs.txt", true));
writer.write("\n" + timestamp + "\t" + username + "\t" + chatContents);
writer.close();
} catch (IOException ex) {
System.out.println("caught an IOException");
ex.printStackTrace(System.out);
}
Thanks for your suggestions!