From a98fa05b8f495c530f430dede18a20d1c6ee1344 Mon Sep 17 00:00:00 2001 From: aj Date: Thu, 2 May 2019 10:38:06 +0100 Subject: [PATCH] added log dump to file --- src/main/java/sarsoo/fmframework/log/Log.java | 58 ++++++++++++++++++- .../java/sarsoo/fmframework/log/LogTest.java | 29 ++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/test/java/sarsoo/fmframework/log/LogTest.java diff --git a/src/main/java/sarsoo/fmframework/log/Log.java b/src/main/java/sarsoo/fmframework/log/Log.java index 4899382..502a60e 100644 --- a/src/main/java/sarsoo/fmframework/log/Log.java +++ b/src/main/java/sarsoo/fmframework/log/Log.java @@ -1,5 +1,8 @@ package sarsoo.fmframework.log; +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDateTime; import java.util.ArrayList; import sarsoo.fmframework.log.console.Console; @@ -12,10 +15,12 @@ public class Log { private ArrayList consoles = new ArrayList(); private Boolean writeToSTDOut; - + private ArrayList logList = new ArrayList(); private ArrayList infoList = new ArrayList(); private ArrayList errorList = new ArrayList(); + + private LocalDateTime createdTime = LocalDateTime.now(); public Log() { 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(); + } + } + } diff --git a/src/test/java/sarsoo/fmframework/log/LogTest.java b/src/test/java/sarsoo/fmframework/log/LogTest.java new file mode 100644 index 0000000..f72c7fb --- /dev/null +++ b/src/test/java/sarsoo/fmframework/log/LogTest.java @@ -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); + } + +}