-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClientLogger.java
32 lines (27 loc) · 1 KB
/
ClientLogger.java
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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.time.LocalDateTime;
public class ClientLogger {
private final Path logPath;
public ClientLogger(Path logPath) {
this.logPath = logPath;
}
public void write(String message) throws IOException {
String full = LocalDateTime.now().toString().concat(" ").concat(message).concat("\n");
if (!Files.exists(logPath)) {
Files.createFile(logPath);
}
Files.write(logPath, full.getBytes(), StandardOpenOption.APPEND);
}
public void writeError(String message) {
try {
String full = LocalDateTime.now().toString().concat(": ERROR - ").concat(message).concat("\n");
Files.write(logPath, full.getBytes(), StandardOpenOption.APPEND);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new RuntimeException("Failure when writing to logs.");
}
}
}