Commit 7c137805 authored by Loris Croce's avatar Loris Croce
Browse files

log files with headers (thanks...

log files with headers (thanks https://stackoverflow.com/questions/34359166/java-logging-using-gethead-with-append)
parent 45689ef2
Pipeline #38567 passed with stages
in 4 minutes and 51 seconds
package fr.inrae.agriterix.simulator.dynamics;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.io.File;
import java.util.logging.*;
public class DynamicsLogFormatter extends Formatter {
private String header;
public DynamicsLogFormatter(String header) {
this.header = header;
}
@Override
public String format(LogRecord record) {
StringBuffer sb = new StringBuffer();
......@@ -12,4 +18,37 @@ public class DynamicsLogFormatter extends Formatter {
return sb.toString();
}
@Override
public String getHead(Handler h) {
boolean writeHeader = true;
try {
if (h instanceof FileHandler) {
writeHeader = lengthOpen((FileHandler) h).longValue() == 0L;
}
} catch (SecurityException ignore) {
}
if (writeHeader) {
return header + "\n"; //TODO: Insert your CSV headers.
} else {
return super.getHead(h); //Skip headers.
}
}
private Number lengthOpen(Handler h) {
if (h instanceof FileHandler) {
String p = h.getClass().getName();
LogManager manager = LogManager.getLogManager();
p = manager.getProperty(p.concat(".pattern"));
//TODO: Deal with FileHandler patterns.
if (p != null) {
File f = new File(p);
//TODO: Implement file listing and filtering.
return f.length();
}
}
return 0L;
}
}
......@@ -19,7 +19,7 @@ public class DynamicsLogger {
private String separator = ", ";
private String path = "data/out";
public DynamicsLogger(String className, String timeStamp) {
public DynamicsLogger(String className, String timeStamp, String header) {
try {
Files.createDirectories(Paths.get("data/out/" + timeStamp + "/logs/"));
fileHandler = new FileHandler("data/out/" + timeStamp + "/logs/" + className + ".csv", false);
......@@ -29,7 +29,12 @@ public class DynamicsLogger {
logger = Logger.getLogger(className);
logger.setUseParentHandlers(false);
logger.addHandler(fileHandler);
fileHandler.setFormatter(new DynamicsLogFormatter());
fileHandler.setFormatter(new DynamicsLogFormatter(header));
}
public void setHeader(String header) {
logger.info(header);
}
public void log(int timestep, String message) {
......
......@@ -22,7 +22,8 @@ public class FlowDynamics extends Dynamics {
@Override
public void initLogger() {
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp);
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp, "TIMESTAMP, CATEGORY, FLOW_LABEL, " +
"PRODUCT, VALUE");
}
@Override
......
......@@ -30,7 +30,8 @@ public class MarketDynamics extends Dynamics {
@Override
public void initLogger() {
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp);
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp, "TIMESTAMP, GEOAREA, PRODUCT, " +
"AMOUNT, LABEL");
}
@Override
......
......@@ -106,7 +106,7 @@ public class PopulationDynamics extends Dynamics {
@Override
public void initLogger() {
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp);
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp, "HEADER");
}
/**
......
......@@ -63,7 +63,7 @@ public class PopulationDynamicsByAgeClass extends Dynamics {
@Override
public void initLogger() {
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp);
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp, "HEADER");
}
@Override
......
......@@ -36,7 +36,8 @@ public class ProductionDynamics extends Dynamics {
@Override
public void initLogger() {
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp);
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp, "TIMESTAMP, GEOAREA, PRODUCT, " +
"AMOUNT, LABEL");
}
@Override
......
......@@ -22,7 +22,7 @@ public class StateDynamics extends Dynamics {
@Override
public void initLogger() {
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp);
logger = new DynamicsLogger(this.getClass().getSimpleName(), super.timeStamp, "HEADER");
}
@Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment