Context logging

This commit is contained in:
Alexander Nozik 2017-08-24 10:41:24 +03:00
parent c04a1780e8
commit 605e3536b5
2 changed files with 46 additions and 41 deletions

View File

@ -15,61 +15,64 @@
*/ */
package inr.numass; package inr.numass;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.FileAppender;
import hep.dataforge.context.Context;
import hep.dataforge.io.BasicIOManager; import hep.dataforge.io.BasicIOManager;
import hep.dataforge.names.Name; import hep.dataforge.names.Name;
import hep.dataforge.utils.ReferenceRegistry;
import org.apache.commons.io.output.TeeOutputStream; import org.apache.commons.io.output.TeeOutputStream;
import org.slf4j.LoggerFactory;
import java.io.File; import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
*
* @author Darksnake * @author Darksnake
*/ */
public class NumassIO extends BasicIOManager { public class NumassIO extends BasicIOManager {
public static final String NUMASS_OUTPUT_CONTEXT_KEY = "numass.outputDir"; public static final String NUMASS_OUTPUT_CONTEXT_KEY = "numass.outputDir";
// public static RawNMFile readAsDat(Binary source, Meta config) throws IOException { ReferenceRegistry<OutputStream> registry = new ReferenceRegistry<>();
// return new LegacyDataReader(source, config).read(); // FileAppender<ILoggingEvent> appender;
// }
// private File getOutputDir() {
// String outputDirPath = getContext().getString(NUMASS_OUTPUT_CONTEXT_KEY, ".");
// File res = new File(getRootDirectory(), outputDirPath);
// if (!res.exists()) {
// res.mkdir();
// }
// return res;
//
// }
// public static RawNMFile readAsPaw(Binary source, Meta config) throws IOException { @Override
// return new NumassPawReader().readPaw(source, config.getString(FileDataFactory.FILE_NAME_KEY)); public void attach(Context context) {
// } super.attach(context);
}
// public static RawNMFile getNumassData(Binary binary, Meta config) { @Override
// try { public Appender<ILoggingEvent> createLoggerAppender() {
// RawNMFile dataFile; LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
// String extension = FilenameUtils.getExtension(config.getString(FileDataFactory.FILE_NAME_KEY)).toLowerCase(); PatternLayoutEncoder ple = new PatternLayoutEncoder();
// switch (extension) {
// case "dat":
// dataFile = readAsDat(binary, config);
// break;
// default:
// throw new RuntimeException("Wrong file format");
// }
// return dataFile;
// } catch (IOException ex) {
// throw new RuntimeException(ex);
// }
// }
ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
ple.setContext(lc);
ple.start();
FileAppender<ILoggingEvent> appender = new FileAppender<>();
appender.setFile(new File(getWorkDirectory(), meta().getString("logFileName", "numass.log")).toString());
appender.setEncoder(ple);
return appender;
}
@Override
public void detach() {
super.detach();
registry.forEach(it -> {
try {
it.close();
} catch (IOException e) {
LoggerFactory.getLogger(getClass()).error("Failed to close output", e);
}
});
}
@Override @Override
public OutputStream out(Name stage, Name name) { public OutputStream out(Name stage, Name name) {
@ -83,16 +86,18 @@ public class NumassIO extends BasicIOManager {
} }
} }
if (stage != null) { if (stage != null && stage.length() != 0) {
tokens.addAll(Arrays.asList(stage.asArray())); tokens.addAll(Arrays.asList(stage.asArray()));
} }
String dirName = String.join(File.separator, tokens); String dirName = String.join(File.separator, tokens);
String fileName = name.removeNameSpace().toString() + ".out"; String fileName = name.removeNameSpace().toString();
return buildOut(getWorkDirectory(), dirName, fileName); OutputStream out = buildOut(getWorkDirectory(), dirName, fileName);
registry.add(out);
return out;
} }
protected OutputStream buildOut(File parentDir, String dirName, String fileName) { private OutputStream buildOut(File parentDir, String dirName, String fileName) {
File outputFile; File outputFile;
if (!parentDir.exists()) { if (!parentDir.exists()) {

View File

@ -56,7 +56,7 @@ public class NumassTableFilterTask extends SingleActionTask<Table, Table> {
if (inputMeta.hasValue("from") || inputMeta.hasValue("to")) { if (inputMeta.hasValue("from") || inputMeta.hasValue("to")) {
double uLo = inputMeta.getDouble("from", 0); double uLo = inputMeta.getDouble("from", 0);
double uHi = inputMeta.getDouble("to", Double.POSITIVE_INFINITY); double uHi = inputMeta.getDouble("to", Double.POSITIVE_INFINITY);
getLogger(inputMeta).debug("Filtering finished"); getLogger(context,inputMeta).debug("Filtering finished");
return TableTransform.filter(input, "Uset", uLo, uHi); return TableTransform.filter(input, "Uset", uLo, uHi);
} else if (inputMeta.hasValue("condition")) { } else if (inputMeta.hasValue("condition")) {
Predicate<Values> predicate = (dp) -> ExpressionUtils.condition(inputMeta.getString("condition"), unbox(dp)); Predicate<Values> predicate = (dp) -> ExpressionUtils.condition(inputMeta.getString("condition"), unbox(dp));