Search in sources :

Example 36 with Formatter

use of java.util.logging.Formatter in project openj9 by eclipse.

the class Session method enableConsoleLogging.

/**
 * Enables console logger for the supplied logger. Typically used to surface
 * log commands generated by internal components
 *
 * @param log the logger to enable
 */
private void enableConsoleLogging(Logger log) {
    ConsoleHandler handler = new ConsoleHandler();
    handler.setLevel(Level.FINE);
    Formatter formatter = new SimpleFormatter();
    handler.setFormatter(formatter);
    log.addHandler(handler);
    log.setLevel(Level.FINE);
    if (isVerboseEnabled) {
        out.println("Console logging is now enabled for " + log.getName());
    }
}
Also used : SimpleFormatter(java.util.logging.SimpleFormatter) Formatter(java.util.logging.Formatter) SimpleFormatter(java.util.logging.SimpleFormatter) ConsoleHandler(java.util.logging.ConsoleHandler)

Example 37 with Formatter

use of java.util.logging.Formatter in project pyramid by cheng-li.

the class AppCombSUM method main.

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        throw new IllegalArgumentException("Please specify a properties file.");
    }
    Config config = new Config(args[0]);
    Logger logger = Logger.getAnonymousLogger();
    String logFile = config.getString("output.log");
    FileHandler fileHandler = null;
    if (!logFile.isEmpty()) {
        new File(logFile).getParentFile().mkdirs();
        // todo should append?
        fileHandler = new FileHandler(logFile, true);
        Formatter formatter = new SimpleFormatter();
        fileHandler.setFormatter(formatter);
        logger.addHandler(fileHandler);
        logger.setUseParentHandlers(false);
    }
    logger.info(config.toString());
    File output = new File(config.getString("output.folder"));
    output.mkdirs();
    if (config.getBoolean("calibrate")) {
        calibrate(config, logger);
    }
    if (config.getBoolean("validate")) {
        report(config, config.getString("validFolder"));
    }
    if (config.getBoolean("tuneThreshold")) {
        tuneThreshold(config, logger);
    }
    if (config.getBoolean("test")) {
        report(config, config.getString("testFolder"));
        testAutomation(config, logger);
    }
    if (fileHandler != null) {
        fileHandler.close();
    }
}
Also used : Config(edu.neu.ccs.pyramid.configuration.Config) SimpleFormatter(java.util.logging.SimpleFormatter) Formatter(java.util.logging.Formatter) SimpleFormatter(java.util.logging.SimpleFormatter) Logger(java.util.logging.Logger) File(java.io.File) FileHandler(java.util.logging.FileHandler)

Example 38 with Formatter

use of java.util.logging.Formatter in project pyramid by cheng-li.

the class AppCTFT method main.

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        throw new IllegalArgumentException("Please specify a properties file.");
    }
    Config config = new Config(args[0]);
    Logger logger = Logger.getAnonymousLogger();
    String logFile = config.getString("output.log");
    FileHandler fileHandler = null;
    if (!logFile.isEmpty()) {
        new File(logFile).getParentFile().mkdirs();
        // todo should append?
        fileHandler = new FileHandler(logFile, true);
        Formatter formatter = new SimpleFormatter();
        fileHandler.setFormatter(formatter);
        logger.addHandler(fileHandler);
        logger.setUseParentHandlers(false);
    }
    logger.info(config.toString());
    File output = new File(config.getString("output.folder"));
    output.mkdirs();
    logger.info("start tuning CTFT ");
    String validReportPath = config.getString("validReportPath");
    String testReportPath = config.getString("testReportPath");
    Stream<Pair<Double, Double>> validStream = ReportUtils.getConfidenceF1(validReportPath).stream();
    List<Pair<Double, Double>> testList = ReportUtils.getConfidenceF1(testReportPath);
    CTFT.Summary summaryValid = CTFT.findThreshold(validStream, config.getDouble("CTFT.targetF1"));
    double ctft = summaryValid.getConfidenceThreshold();
    double ctft_clipped = ctft;
    if (ctft_clipped > config.getDouble("CTFT.upperBound")) {
        ctft_clipped = config.getDouble("CTFT.upperBound");
    }
    if (ctft_clipped < config.getDouble("CTFT.lowerBound")) {
        ctft_clipped = config.getDouble("CTFT.lowerBound");
    }
    FileUtils.writeStringToFile(Paths.get(config.getString("output.folder"), config.getString("CTFT.name") + "_unclipped").toFile(), "" + ctft);
    FileUtils.writeStringToFile(Paths.get(config.getString("output.folder"), config.getString("CTFT.name") + "_clipped").toFile(), "" + ctft_clipped);
    CTFT.Summary summaryTest = CTFT.applyThreshold(testList.stream(), ctft);
    CTFT.Summary summaryTest_clipped = CTFT.applyThreshold(testList.stream(), ctft_clipped);
    logger.info("tuning CTFT is done");
    logger.info("*****************");
    logger.info("autocoding performance with unclipped CTFT " + summaryTest.getConfidenceThreshold());
    logger.info("autocoding percentage = " + summaryTest.getAutoCodingPercentage());
    logger.info("autocoding accuracy = " + summaryTest.getAutoCodingAccuracy());
    logger.info("autocoding F1 = " + summaryTest.getAutoCodingF1());
    logger.info("number of autocoded documents = " + summaryTest.getNumAutoCoded());
    logger.info("number of correct autocoded documents = " + summaryTest.getNumCorrectAutoCoded());
    logger.info("*****************");
    logger.info("autocoding performance with clipped CTFT " + summaryTest_clipped.getConfidenceThreshold());
    logger.info("autocoding percentage = " + summaryTest_clipped.getAutoCodingPercentage());
    logger.info("autocoding accuracy = " + summaryTest_clipped.getAutoCodingAccuracy());
    logger.info("autocoding F1 = " + summaryTest_clipped.getAutoCodingF1());
    logger.info("number of autocoded documents = " + summaryTest_clipped.getNumAutoCoded());
    logger.info("number of correct autocoded documents = " + summaryTest_clipped.getNumCorrectAutoCoded());
    if (fileHandler != null) {
        fileHandler.close();
    }
}
Also used : CTFT(edu.neu.ccs.pyramid.calibration.CTFT) Config(edu.neu.ccs.pyramid.configuration.Config) SimpleFormatter(java.util.logging.SimpleFormatter) Formatter(java.util.logging.Formatter) SimpleFormatter(java.util.logging.SimpleFormatter) Logger(java.util.logging.Logger) FileHandler(java.util.logging.FileHandler) File(java.io.File) Pair(edu.neu.ccs.pyramid.util.Pair)

Example 39 with Formatter

use of java.util.logging.Formatter in project pyramid by cheng-li.

the class AppEnsemble method main.

public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        throw new IllegalArgumentException("Please specify a properties file.");
    }
    Config config = new Config(args[0]);
    Logger logger = Logger.getAnonymousLogger();
    String logFile = config.getString("output.log");
    FileHandler fileHandler = null;
    if (!logFile.isEmpty()) {
        new File(logFile).getParentFile().mkdirs();
        // todo should append?
        fileHandler = new FileHandler(logFile, true);
        Formatter formatter = new SimpleFormatter();
        fileHandler.setFormatter(formatter);
        logger.addHandler(fileHandler);
        logger.setUseParentHandlers(false);
    }
    logger.info(config.toString());
    File output = new File(config.getString("output.folder"));
    output.mkdirs();
    List<String> modelPaths = config.getStrings("modelPaths");
    List<String> modelNames = config.getStrings("modelNames");
    String ensembleName = config.getString("ensembleModelName");
    String testFolder = config.getString("testFolder");
    String validFolder = config.getString("validFolder");
    double targetValue = config.getDouble("threshold.targetValue");
    logger.info("start loading all reports and getting ground truth");
    List<Map<String, DocumentReport>> testlistMaps = new ArrayList<>();
    List<Map<String, DocumentReport>> validlistMaps = new ArrayList<>();
    Map<String, String> groundTruthTest;
    Map<String, String> groundTruthValid;
    String dataSetPath = modelPaths.get(0).split("model_predictions")[0] + "data_sets/";
    String testSetPath = dataSetPath + testFolder;
    String validSetPath = dataSetPath + validFolder;
    MultiLabelClfDataSet testSetModel0 = TRECFormat.loadMultiLabelClfDataSet(testSetPath, DataSetType.ML_CLF_SPARSE, true);
    MultiLabelClfDataSet validSetModel0 = TRECFormat.loadMultiLabelClfDataSet(validSetPath, DataSetType.ML_CLF_SPARSE, true);
    groundTruthTest = ReportUtils.getIDGroundTruth(testSetModel0);
    groundTruthValid = ReportUtils.getIDGroundTruth(validSetModel0);
    for (int i = 0; i < modelPaths.size(); i++) {
        Map<String, DocumentReport> testmap = loadReportCSV(Paths.get(modelPaths.get(i), "predictions", testFolder + "_reports", "report.csv").toString(), modelNames.get(i));
        testlistMaps.add(testmap);
        Map<String, DocumentReport> validmap = loadReportCSV(Paths.get(modelPaths.get(i), "predictions", validFolder + "_reports", "report.csv").toString(), modelNames.get(i));
        validlistMaps.add(validmap);
    }
    logger.info("finish loading all reports and getting ground truth");
    logger.info("start generating ensemble test report");
    LabelTranslator newLabelTranslatorTest = getLabelTranslatorEnsemble(config, testFolder);
    List<String> testDocIds = ReportUtils.getDocIds(Paths.get(modelPaths.get(0), "predictions", testFolder + "_reports", "report.csv").toString());
    generateReport(config, groundTruthTest, testlistMaps, ensembleName, testFolder, testDocIds, newLabelTranslatorTest);
    logger.info("ensemble test report generated");
    logger.info("start generating ensemble validation report");
    LabelTranslator newLabelTranslatorValid = getLabelTranslatorEnsemble(config, validFolder);
    List<String> validDocIds = ReportUtils.getDocIds(Paths.get(modelPaths.get(0), "predictions", validFolder + "_reports", "report.csv").toString());
    generateReport(config, groundTruthValid, validlistMaps, ensembleName, validFolder, validDocIds, newLabelTranslatorValid);
    logger.info("ensemble validation report generated");
    logger.info("classification performance on dataset " + testFolder);
    MlMeasureInfo measureInfo_test = getmlMeasureInfo(config, testSetModel0, testFolder, newLabelTranslatorTest);
    MLMeasures mlMeasures = new MLMeasures(measureInfo_test.numClasses, measureInfo_test.multiLabels, measureInfo_test.predictions);
    logger.info(mlMeasures.toString());
    if (config.getBoolean("tuneThreshold")) {
        logger.info("start tuning confidence threshold");
        Stream<Pair<Double, Double>> streamValid;
        double threshold = 1.1;
        if (config.getString("threshold.targetMetric").equals("accuracy")) {
            streamValid = ReportUtils.getConfidenceCorrectness(Paths.get(config.getString("output.folder"), "model_predictions", ensembleName, "predictions", validFolder + "_reports", "report.csv").toString()).stream();
            CTAT.Summary validSummary = CTAT.findThreshold(streamValid, targetValue);
            threshold = validSummary.getConfidenceThreshold();
        }
        if (config.getString("threshold.targetMetric").equals("f1")) {
            streamValid = ReportUtils.getConfidenceF1(Paths.get(config.getString("output.folder"), "model_predictions", ensembleName, "predictions", validFolder + "_reports", "report.csv").toString()).stream();
            CTFT.Summary summary_valid = CTFT.findThreshold(streamValid, targetValue);
            threshold = summary_valid.getConfidenceThreshold();
        }
        FileUtils.writeStringToFile(Paths.get(config.getString("output.folder"), "model_predictions", ensembleName, "models", "threshold", config.getString("threshold.name")).toFile(), "" + threshold);
        double confidenceThresholdClipped = CTAT.clip(threshold, config.getDouble("threshold.lowerBound"), config.getDouble("threshold.upperBound"));
        FileUtils.writeStringToFile(Paths.get(config.getString("output.folder"), "model_predictions", ensembleName, "models", "threshold", config.getString("threshold.name") + "_clipped").toFile(), "" + confidenceThresholdClipped);
        logger.info("tuning threshold is done");
        List<Pair<Double, Double>> testStream;
        if (config.getString("threshold.targetMetric").equals("accuracy")) {
            testStream = ReportUtils.getConfidenceCorrectness(Paths.get(config.getString("output.folder"), "model_predictions", ensembleName, "predictions", testFolder + "_reports", "report.csv").toString());
            CTAT.Summary testSummary_unclipped = CTAT.applyThreshold(testStream.stream(), threshold);
            CTAT.Summary testSummary_clipped = CTAT.applyThreshold(testStream.stream(), confidenceThresholdClipped);
            logger.info("*****************");
            logger.info("autocoding performance with unclipped CTAT " + testSummary_unclipped.getConfidenceThreshold());
            logger.info("autocoding percentage = " + testSummary_unclipped.getAutoCodingPercentage());
            logger.info("autocoding accuracy = " + testSummary_unclipped.getAutoCodingAccuracy());
            logger.info("number of autocoded documents = " + testSummary_unclipped.getNumAutoCoded());
            logger.info("number of correct autocoded documents = " + testSummary_unclipped.getNumCorrectAutoCoded());
            logger.info("*****************");
            logger.info("autocoding performance with clipped CTAT " + testSummary_clipped.getConfidenceThreshold());
            logger.info("autocoding percentage = " + testSummary_clipped.getAutoCodingPercentage());
            logger.info("autocoding accuracy = " + testSummary_clipped.getAutoCodingAccuracy());
            logger.info("number of autocoded documents = " + testSummary_clipped.getNumAutoCoded());
            logger.info("number of correct autocoded documents = " + testSummary_clipped.getNumCorrectAutoCoded());
        }
        if (config.getString("threshold.targetMetric").equals("f1")) {
            testStream = ReportUtils.getConfidenceF1(Paths.get(config.getString("output.folder"), "model_predictions", ensembleName, "predictions", testFolder + "_reports", "report.csv").toString());
            CTFT.Summary summary_test = CTFT.applyThreshold(testStream.stream(), threshold);
            CTFT.Summary summary_test_clipped = CTFT.applyThreshold(testStream.stream(), confidenceThresholdClipped);
            logger.info("*****************");
            logger.info("autocoding performance with unclipped CTFT " + summary_test.getConfidenceThreshold());
            logger.info("autocoding percentage = " + summary_test.getAutoCodingPercentage());
            logger.info("autocoding accuracy = " + summary_test.getAutoCodingAccuracy());
            logger.info("autocoding F1 = " + summary_test.getAutoCodingF1());
            logger.info("number of autocoded documents = " + summary_test.getNumAutoCoded());
            logger.info("number of correct autocoded documents = " + summary_test.getNumCorrectAutoCoded());
            logger.info("*****************");
            logger.info("autocoding performance with clipped CTFT " + summary_test_clipped.getConfidenceThreshold());
            logger.info("autocoding percentage = " + summary_test_clipped.getAutoCodingPercentage());
            logger.info("autocoding accuracy = " + summary_test_clipped.getAutoCodingAccuracy());
            logger.info("autocoding F1 = " + summary_test_clipped.getAutoCodingF1());
            logger.info("number of autocoded documents = " + summary_test_clipped.getNumAutoCoded());
            logger.info("number of correct autocoded documents = " + summary_test_clipped.getNumCorrectAutoCoded());
        }
    }
    if (fileHandler != null) {
        fileHandler.close();
    }
}
Also used : Config(edu.neu.ccs.pyramid.configuration.Config) SimpleFormatter(java.util.logging.SimpleFormatter) Formatter(java.util.logging.Formatter) Logger(java.util.logging.Logger) MLMeasures(edu.neu.ccs.pyramid.eval.MLMeasures) Pair(edu.neu.ccs.pyramid.util.Pair) CTFT(edu.neu.ccs.pyramid.calibration.CTFT) SimpleFormatter(java.util.logging.SimpleFormatter) FileHandler(java.util.logging.FileHandler) CTAT(edu.neu.ccs.pyramid.calibration.CTAT) File(java.io.File)

Example 40 with Formatter

use of java.util.logging.Formatter in project incubator-juneau by apache.

the class Microservice method init.

// -----------------------------------------------------------------------------------------------------------------
// Abstract lifecycle methods.
// -----------------------------------------------------------------------------------------------------------------
/**
 * Initializes this microservice.
 *
 * <p>
 * This method can be called whenever the microservice is not started.
 *
 * <p>
 * It will initialize (or reinitialize) the console commands, system properties, and logger.
 *
 * @return This object.
 * @throws ParseException Malformed input encountered.
 * @throws IOException Couldn't read a file.
 */
public synchronized Microservice init() throws IOException, ParseException {
    // --------------------------------------------------------------------------------
    // Set system properties.
    // --------------------------------------------------------------------------------
    Set<String> spKeys = config.getKeys("SystemProperties");
    if (spKeys != null)
        for (String key : spKeys) System.setProperty(key, config.get("SystemProperties/" + key).orElse(null));
    // --------------------------------------------------------------------------------
    // Initialize logging.
    // --------------------------------------------------------------------------------
    this.logger = builder.logger;
    LogConfig logConfig = builder.logConfig != null ? builder.logConfig : new LogConfig();
    if (this.logger == null) {
        LogManager.getLogManager().reset();
        this.logger = Logger.getLogger("");
        String logFile = firstNonNull(logConfig.logFile, config.get("Logging/logFile").orElse(null));
        if (isNotEmpty(logFile)) {
            String logDir = firstNonNull(logConfig.logDir, config.get("Logging/logDir").orElse("."));
            File logDirFile = resolveFile(logDir);
            mkdirs(logDirFile, false);
            logDir = logDirFile.getAbsolutePath();
            System.setProperty("juneau.logDir", logDir);
            boolean append = firstNonNull(logConfig.append, config.get("Logging/append").asBoolean().orElse(false));
            int limit = firstNonNull(logConfig.limit, config.get("Logging/limit").asInteger().orElse(1024 * 1024));
            int count = firstNonNull(logConfig.count, config.get("Logging/count").asInteger().orElse(1));
            FileHandler fh = new FileHandler(logDir + '/' + logFile, limit, count, append);
            Formatter f = logConfig.formatter;
            if (f == null) {
                String format = config.get("Logging/format").orElse("[{date} {level}] {msg}%n");
                String dateFormat = config.get("Logging/dateFormat").orElse("yyyy.MM.dd hh:mm:ss");
                boolean useStackTraceHashes = config.get("Logging/useStackTraceHashes").asBoolean().orElse(false);
                f = new LogEntryFormatter(format, dateFormat, useStackTraceHashes);
            }
            fh.setFormatter(f);
            fh.setLevel(firstNonNull(logConfig.fileLevel, config.get("Logging/fileLevel").as(Level.class).orElse(Level.INFO)));
            logger.addHandler(fh);
            ConsoleHandler ch = new ConsoleHandler();
            ch.setLevel(firstNonNull(logConfig.consoleLevel, config.get("Logging/consoleLevel").as(Level.class).orElse(Level.WARNING)));
            ch.setFormatter(f);
            logger.addHandler(ch);
        }
    }
    OMap loggerLevels = config.get("Logging/levels").as(OMap.class).orElseGet(OMap::new);
    for (String l : loggerLevels.keySet()) Logger.getLogger(l).setLevel(loggerLevels.get(l, Level.class));
    for (String l : logConfig.levels.keySet()) Logger.getLogger(l).setLevel(logConfig.levels.get(l));
    return this;
}
Also used : Formatter(java.util.logging.Formatter)

Aggregations

Formatter (java.util.logging.Formatter)44 LogRecord (java.util.logging.LogRecord)18 File (java.io.File)13 SimpleFormatter (java.util.logging.SimpleFormatter)12 IOException (java.io.IOException)11 Logger (java.util.logging.Logger)11 FileHandler (java.util.logging.FileHandler)10 Handler (java.util.logging.Handler)9 Date (java.util.Date)6 Test (org.junit.Test)6 SimpleDateFormat (java.text.SimpleDateFormat)5 ConsoleHandler (java.util.logging.ConsoleHandler)5 Config (edu.neu.ccs.pyramid.configuration.Config)4 JSONLogFormatter (fish.payara.enterprise.server.logging.JSONLogFormatter)4 Level (java.util.logging.Level)4 StreamHandler (java.util.logging.StreamHandler)4 Pair (edu.neu.ccs.pyramid.util.Pair)3 DateFormat (java.text.DateFormat)3 ErrorManager (java.util.logging.ErrorManager)3 Filter (java.util.logging.Filter)3