v1.2 Fix + Logger tests

This commit is contained in:
Klemek
2018-08-30 13:59:39 +01:00
parent c8f05bad09
commit 0d1111852b
5 changed files with 134 additions and 16 deletions
+3 -3
View File
@@ -1,11 +1,11 @@
# Simple Logger
A simple but useful Java logger to use everywhere.
Current version v1.1
Current version v1.2
## Download
* [simple-logger-1.1.jar](../../raw/master/download/simple-logger-1.1.jar)
* [simple-logger-1.2.jar](../../raw/master/download/simple-logger-1.2.jar)
## How to use
@@ -85,7 +85,7 @@ You can use this project as a maven dependency with this :
<dependency>
<groupId>fr.klemek</groupId>
<artifactId>simple-logger</artifactId>
<version>1.1</version>
<version>1.2</version>
</dependency>
</dependencies>
```
Binary file not shown.
+1 -2
View File
@@ -1,6 +1,5 @@
package fr.klemek.logger;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
@@ -89,7 +88,7 @@ public final class Logger {
if(outputFile != null){
Handler handler = new FileHandler(outputFile, true);
handler.setFormatter(new SimpleFormatter());
appLogger.addHandler(new FileHandler(outputFile, true));
appLogger.addHandler(handler);
}
} catch (IOException e) {
+129 -10
View File
@@ -1,25 +1,144 @@
package fr.klemek.logger;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class LoggerTest {
private static File output = new File("output.log");
@Test
public void test() {
Logger.log("message");
}
private static Path output = new File("output.log").toPath();
@Before
public void setUp() {
public void setUp() throws IOException {
if (Files.exists(output)) {
PrintWriter writer = new PrintWriter(output.toFile());
writer.print("");
writer.close();
}
Logger.init("logging.properties");
if(output.exists())
output.delete();
}
@Test
public void testLog() {
Logger.log("message");
verifyOutput(new String[]{
"[INFO][Test-LoggerTest] message"
});
}
@Test
public void testLogParams() {
Logger.log("message {2} {0}", "abc", 123, 456f);
verifyOutput(new String[]{
"[INFO][Test-LoggerTest] message 456 abc"
});
}
@Test
public void testLogCustomLevel() {
Logger.log(Level.WARNING, "message");
verifyOutput(new String[]{
"[WARNING][Test-LoggerTest] message"
});
}
@Test
public void testLogCustomLevelParams() {
Logger.log(Level.WARNING, "message {2} {0}", "abc", 123, 456f);
verifyOutput(new String[]{
"[WARNING][Test-LoggerTest] message 456 abc"
});
}
@Test
public void testLogException() {
Logger.log(new Exception("custom exception"));
verifyOutput(new String[]{
"[SEVERE][Test-LoggerTest] java.lang.Exception: custom exception",
"[SEVERE][Test-LoggerTest] \t fr.klemek.logger.LoggerTest.testLogException(LoggerTest.java:66)",
"[SEVERE][Test-LoggerTest] \t sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)"
});
}
@Test
public void testLogExceptionMessage() {
Logger.log(new Exception("custom exception"), "custom message");
verifyOutput(new String[]{
"[SEVERE][Test-LoggerTest] custom message : java.lang.Exception: custom exception",
"[SEVERE][Test-LoggerTest] \t fr.klemek.logger.LoggerTest.testLogExceptionMessage(LoggerTest.java:76)",
"[SEVERE][Test-LoggerTest] \t sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)"
});
}
@Test
public void testLogExceptionCustomLevel() {
Logger.log(Level.WARNING, new Exception("custom exception"));
verifyOutput(new String[]{
"[WARNING][Test-LoggerTest] java.lang.Exception: custom exception"
});
}
@Test
public void testLogExceptionCustomLevelCustomMessage() {
Logger.log(Level.WARNING, new Exception("custom exception"), "custom message");
verifyOutput(new String[]{
"[WARNING][Test-LoggerTest] custom message : java.lang.Exception: custom exception"
});
}
@Test
public void testSetLevel() {
assertEquals(Level.INFO, Logger.getLevel());
Logger.log(Level.INFO, "message1");
Logger.log(Level.WARNING, "message2");
Logger.setLevel(Level.WARNING);
assertEquals(Level.WARNING, Logger.getLevel());
Logger.log(Level.INFO, "message3");
Logger.log(Level.WARNING, "message4");
verifyOutput(new String[]{
"[INFO][Test-LoggerTest] message1",
"[WARNING][Test-LoggerTest] message2",
"[WARNING][Test-LoggerTest] message4",
});
}
private void verifyOutput(String[] expected) {
try (FileReader freader = new FileReader(output.toFile())) {
try (BufferedReader reader = new BufferedReader(freader)) {
int i = 0;
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.length() > 0) {
if (i >= expected.length) {
fail("Too much lines in output file");
return;
}
assertEquals(expected[i++], line);
}
}
if (i < expected.length)
fail("Not enough lines in output file");
}
} catch (IOException e) {
e.printStackTrace();
fail("error when accessing output file");
}
}
}
+1 -1
View File
@@ -4,7 +4,7 @@ handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
#Customize date/time shown with this line
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT][%4$s]%5$s %n
java.util.logging.SimpleFormatter.format=[%4$s]%5$s %n
#Specify your app name here
app_name=Test