Skip to content

Commit 67fbb15

Browse files
committed
Change Logger#log to catch IOException and print to injected PrintStream when it fails to log
1 parent 111a275 commit 67fbb15

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

src/main/java/httpserver/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void main(String[] args) throws IOException {
1616
PathExaminer pathExaminer = new PathExaminer();
1717
Path root = pathExaminer.getPath(fileDirectory);
1818
Path logPath = pathExaminer.concatenate(root,"logs");
19-
AppConfig appConfig = new AppConfig(root, new Logger(logPath, new FileOperator()));
19+
AppConfig appConfig = new AppConfig(root, new Logger(logPath, new FileOperator(), System.out));
2020

2121
ServerFactory serverFactory = new ServerFactory(new ServerSocketFactory(), appConfig);
2222
SocketHandlerFactory socketHandlerFactory = new SocketHandlerFactory();

src/main/java/httpserver/Logger.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,28 @@
33
import httpserver.file.FileOperator;
44

55
import java.io.IOException;
6+
import java.io.PrintStream;
67
import java.nio.file.Path;
78

89
public class Logger {
910
private final FileOperator fileOperator;
1011
private final Path logPath;
12+
private final PrintStream printStream;
1113

12-
public Logger(Path logPath, FileOperator fileOperator) throws IOException {
14+
public Logger(Path logPath, FileOperator fileOperator, PrintStream printStream) throws IOException {
1315
this.fileOperator = fileOperator;
1416
this.logPath = logPath;
17+
this.printStream = printStream;
1518
createFileIfDoesntExist(logPath);
1619
}
1720

18-
public void log(String logString) throws IOException {
21+
public void log(String logString) {
1922
byte[] logBytes = (logString + "\r\n").getBytes();
20-
fileOperator.appendToFile(logPath, logBytes);
23+
try {
24+
fileOperator.appendToFile(logPath, logBytes);
25+
} catch (IOException e) {
26+
printStream.print(e);
27+
}
2128
}
2229

2330
private void createFileIfDoesntExist(Path path) {

src/main/java/httpserver/SocketHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ private void closeStream() {
5050
try {
5151
inputStream.close();
5252
} catch (IOException e) {
53-
try {
54-
appConfig.getLogger().log(e.toString());
55-
} catch (IOException e1) {
56-
e1.printStackTrace();
57-
}
53+
appConfig.getLogger().log(e.toString());
5854
}
5955
}
6056
}

src/test/java/httpserver/LoggerTest.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.Test;
55

66
import java.io.IOException;
7+
import java.io.PrintStream;
78
import java.nio.file.Path;
89
import java.nio.file.Paths;
910
import java.nio.file.StandardOpenOption;
@@ -13,25 +14,27 @@
1314
import static java.nio.file.Files.write;
1415
import static java.nio.file.Files.exists;
1516
import static org.junit.Assert.*;
17+
import static org.mockito.Mockito.*;
1618

1719
public class LoggerTest {
1820

1921
private final Path logPath;
2022
private final Logger logger;
23+
private final PrintStream printStreamMock;
2124

2225
public LoggerTest() throws IOException {
2326
logPath = Paths.get(tempDir().toString(), "logs");
24-
logger = new Logger(logPath, new FileOperator());
27+
printStreamMock = mock(PrintStream.class);
28+
logger = new Logger(logPath, new FileOperator(), printStreamMock);
2529
}
30+
2631
@Test
2732
public void createsLogFileOnConstructDoesntOverwrite() throws Exception {
2833
assertTrue(exists(logPath));
2934

3035
write(logPath, "fake log line\r\n".getBytes(),
3136
StandardOpenOption.APPEND);
3237

33-
new Logger(logPath, new FileOperator());
34-
3538
assertEquals("fake log line\r\n", new String(readAllBytes(logPath)));
3639
}
3740

@@ -44,4 +47,15 @@ public void writesToLogAndReadsFromLog() throws Exception {
4447

4548
assertEquals("POST example\r\nHEAD example\r\n", log);
4649
}
50+
51+
@Test
52+
public void printsErrorToStdoutIfCantWriteToLog() throws Exception {
53+
FileOperator fileOperatorMock = mock(FileOperator.class);
54+
doThrow(new IOException()).when(fileOperatorMock).appendToFile(any(), any());
55+
Logger logger = new Logger(logPath, fileOperatorMock, printStreamMock);
56+
57+
logger.log("");
58+
59+
verify(printStreamMock).print(any(Exception.class));
60+
}
4761
}

src/test/java/httpserver/SocketHandlerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class SocketHandlerTest {
3535

3636
public SocketHandlerTest() throws IOException {
3737
root = tempDir();
38-
logger = new Logger(root.resolve("logs"), new FileOperator());
38+
logger = new Logger(root.resolve("logs"), new FileOperator(), System.out);
3939
appConfig = new AppConfig(root, logger);
4040
relativePath1 = root.relativize(tempFileOptions(root, "aaa", "temp"));
4141
relativePath2 = root.relativize(tempFileOptions(root, "bbb", "temp"));

0 commit comments

Comments
 (0)