-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.java
More file actions
51 lines (44 loc) · 1.39 KB
/
Logger.java
File metadata and controls
51 lines (44 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class Logger {
private static BufferedWriter bw;
private static FileWriter fw;
private Logger(){
// Do not allow to create objects
}
public static void initializeLogger(int peerId){
try {
fw = new FileWriter("log_peer_" +peerId + ".log");
bw = new BufferedWriter(fw);
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public static void terminateLogger(){
try {
if( bw != null){
bw.close();
}
if( fw != null){
fw.close();
}
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
public static synchronized void logInfo(String log){
StringBuilder data = new StringBuilder();
LocalDateTime now = LocalDateTime.now();
data.append('[').append(now.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM))).append(']');
data.append(" ").append(log).append("\n");
try {
bw.write(data.toString());
bw.flush();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}