-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.java
More file actions
39 lines (32 loc) · 1.03 KB
/
Logger.java
File metadata and controls
39 lines (32 loc) · 1.03 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
package httpserver;
import httpserver.file.FileOperator;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
public class Logger {
private final FileOperator fileOperator;
private final Path logPath;
private final PrintStream errorStream;
public Logger(Path logPath, FileOperator fileOperator, PrintStream errorStream) {
this.fileOperator = fileOperator;
this.logPath = logPath;
this.errorStream = errorStream;
createFileIfDoesntExist(logPath);
}
public void log(String logString) {
byte[] logBytes = (logString + "\r\n").getBytes();
try {
fileOperator.appendToFile(logPath, logBytes);
} catch (IOException e) {
errorStream.print(e);
}
}
private void createFileIfDoesntExist(Path path) {
try {
fileOperator.createFileAtPath(path);
} catch (IOException e) { }
}
public byte[] readLog() throws IOException {
return fileOperator.readContents(logPath);
}
}