added log dump to file

This commit is contained in:
aj 2019-05-02 10:38:06 +01:00
parent d80ba0c3ce
commit a98fa05b8f
2 changed files with 86 additions and 1 deletions

View File

@ -1,5 +1,8 @@
package sarsoo.fmframework.log; package sarsoo.fmframework.log;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import sarsoo.fmframework.log.console.Console; import sarsoo.fmframework.log.console.Console;
@ -17,6 +20,8 @@ public class Log {
private ArrayList<InfoEntry> infoList = new ArrayList<InfoEntry>(); private ArrayList<InfoEntry> infoList = new ArrayList<InfoEntry>();
private ArrayList<ErrorEntry> errorList = new ArrayList<ErrorEntry>(); private ArrayList<ErrorEntry> errorList = new ArrayList<ErrorEntry>();
private LocalDateTime createdTime = LocalDateTime.now();
public Log() { public Log() {
writeToSTDOut = true; writeToSTDOut = true;
} }
@ -65,4 +70,55 @@ public class Log {
} }
} }
public void dumpLog() {
Logger.getLog().log(new LogEntry("dumpLog"));
try {
FileWriter writer = new FileWriter(String.format("%s_Log.txt", createdTime));
for(LogEntry i: logList) {
writer.write(i.toString());
}
writer.close();
Logger.getLog().log(new InfoEntry("dumpLog").addArg("log written"));
} catch (IOException e) {
Logger.getLog().log(new ErrorEntry("dumpLog").addArg("io exception"));
e.printStackTrace();
}
}
public void dumpInfoLog() {
Logger.getLog().log(new LogEntry("dumpInfoLog"));
try {
FileWriter writer = new FileWriter(String.format("%s_InfoLog.txt", createdTime));
for(LogEntry i: infoList) {
writer.write(i.toString());
}
writer.close();
Logger.getLog().log(new InfoEntry("dumpInfoLog").addArg("log written"));
} catch (IOException e) {
Logger.getLog().log(new ErrorEntry("dumpInfoLog").addArg("io exception"));
e.printStackTrace();
}
}
public void dumpErrorLog() {
Logger.getLog().log(new LogEntry("dumpErrorLog"));
try {
FileWriter writer = new FileWriter(String.format("%s_ErrorLog.txt", createdTime));
for(LogEntry i: errorList) {
writer.write(i.toString());
}
writer.close();
Logger.getLog().log(new InfoEntry("dumpErrorLog").addArg("log written"));
} catch (IOException e) {
Logger.getLog().log(new ErrorEntry("dumpErrorLog").addArg("io exception"));
e.printStackTrace();
}
}
} }

View File

@ -0,0 +1,29 @@
package sarsoo.fmframework.log;
import static org.junit.Assert.*;
import org.junit.Test;
import sarsoo.fmframework.log.entry.ErrorEntry;
import sarsoo.fmframework.log.entry.InfoEntry;
import sarsoo.fmframework.log.entry.LogEntry;
public class LogTest {
@Test
public void testDump() {
Log log = Logger.getLog();
log.log(new LogEntry("log test"));
log.logInfo(new InfoEntry("log test"));
log.logError(new ErrorEntry("log test"));
log.dumpLog();
log.dumpInfoLog();
log.dumpErrorLog();
assertTrue(true);
}
}