Feat: add log file on disk
This commit is contained in:
@@ -19,6 +19,11 @@
|
||||
|
||||
package com.sheepit.client;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
@@ -36,8 +41,11 @@ public class Log {
|
||||
|
||||
private boolean printStdOut;
|
||||
|
||||
private Log(boolean print_) {
|
||||
private String logFile;
|
||||
|
||||
private Log(boolean print_, String logDirectory_) {
|
||||
this.printStdOut = print_;
|
||||
this.logFile = logDirectory_ + File.separator + "sheepit.log";
|
||||
this.lastCheckPoint = 0;
|
||||
this.checkpoints.put(this.lastCheckPoint, new ArrayList<String>());
|
||||
this.dateFormat = new SimpleDateFormat("dd-MM HH:mm:ss");
|
||||
@@ -81,6 +89,15 @@ public class Log {
|
||||
if (this.printStdOut) {
|
||||
System.out.println(line);
|
||||
}
|
||||
|
||||
if (this.logFile != null && this.logFile.isEmpty() == false) {
|
||||
try {
|
||||
Files.write(Paths.get(this.logFile), (line + "\n").getBytes(), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
|
||||
}
|
||||
catch (IOException e) {
|
||||
// :( catch-22, we can't really write the exception on the log file
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
@@ -109,11 +126,14 @@ public class Log {
|
||||
|
||||
public static synchronized Log getInstance(Configuration config) {
|
||||
if (instance == null) {
|
||||
String path = null;
|
||||
boolean print = false;
|
||||
if (config != null) {
|
||||
print = config.isPrintLog();
|
||||
path = config.getLogDirectory() == null ? null : config.getLogDirectory();
|
||||
}
|
||||
instance = new Log(print);
|
||||
|
||||
instance = new Log(print, path);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user