From 605e3536b5a6a1c45e594bc0c7dcdfa02436c771 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 24 Aug 2017 10:41:24 +0300 Subject: [PATCH] Context logging --- .../src/main/java/inr/numass/NumassIO.java | 85 ++++++++++--------- .../numass/tasks/NumassTableFilterTask.java | 2 +- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/numass-main/src/main/java/inr/numass/NumassIO.java b/numass-main/src/main/java/inr/numass/NumassIO.java index e834504c..4346b211 100644 --- a/numass-main/src/main/java/inr/numass/NumassIO.java +++ b/numass-main/src/main/java/inr/numass/NumassIO.java @@ -15,61 +15,64 @@ */ 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.names.Name; +import hep.dataforge.utils.ReferenceRegistry; import org.apache.commons.io.output.TeeOutputStream; +import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.OutputStream; +import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** - * * @author Darksnake */ public class NumassIO extends BasicIOManager { public static final String NUMASS_OUTPUT_CONTEXT_KEY = "numass.outputDir"; -// public static RawNMFile readAsDat(Binary source, Meta config) throws IOException { -// return new LegacyDataReader(source, config).read(); -// } + ReferenceRegistry registry = new ReferenceRegistry<>(); +// FileAppender 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 { -// return new NumassPawReader().readPaw(source, config.getString(FileDataFactory.FILE_NAME_KEY)); -// } + @Override + public void attach(Context context) { + super.attach(context); + } -// public static RawNMFile getNumassData(Binary binary, Meta config) { -// try { -// RawNMFile dataFile; -// String extension = FilenameUtils.getExtension(config.getString(FileDataFactory.FILE_NAME_KEY)).toLowerCase(); -// 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); -// } -// } + @Override + public Appender createLoggerAppender() { + LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); + PatternLayoutEncoder ple = new PatternLayoutEncoder(); + ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n"); + ple.setContext(lc); + ple.start(); + FileAppender 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 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())); } String dirName = String.join(File.separator, tokens); - String fileName = name.removeNameSpace().toString() + ".out"; - return buildOut(getWorkDirectory(), dirName, fileName); + String fileName = name.removeNameSpace().toString(); + 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; if (!parentDir.exists()) { diff --git a/numass-main/src/main/java/inr/numass/tasks/NumassTableFilterTask.java b/numass-main/src/main/java/inr/numass/tasks/NumassTableFilterTask.java index e08bfb4a..e985fc5a 100644 --- a/numass-main/src/main/java/inr/numass/tasks/NumassTableFilterTask.java +++ b/numass-main/src/main/java/inr/numass/tasks/NumassTableFilterTask.java @@ -56,7 +56,7 @@ public class NumassTableFilterTask extends SingleActionTask { if (inputMeta.hasValue("from") || inputMeta.hasValue("to")) { double uLo = inputMeta.getDouble("from", 0); 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); } else if (inputMeta.hasValue("condition")) { Predicate predicate = (dp) -> ExpressionUtils.condition(inputMeta.getString("condition"), unbox(dp));