Search in sources :

Example 11 with Formatter

use of java.util.logging.Formatter in project vespa by vespa-engine.

the class LogFileHandlerTestCase method testIt.

/**
 * The scenario
 */
@Test
public void testIt() {
    LogFileHandler h = new LogFileHandler();
    h.setFilePattern("./logfilehandlertest.%Y%m%d%H%M%S");
    h.setFormatter(new Formatter() {

        public String format(LogRecord r) {
            DateFormat df = new SimpleDateFormat("yyyy.MM.dd:HH:mm:ss.SSS");
            String timeStamp = df.format(new Date(r.getMillis()));
            return ("[" + timeStamp + "]" + " " + formatMessage(r) + "\n");
        }
    });
    long now = System.currentTimeMillis();
    long millisPerDay = 60 * 60 * 24 * 1000;
    long tomorrowDays = (now / millisPerDay) + 1;
    long tomorrowMillis = tomorrowDays * millisPerDay;
    assertEquals(tomorrowMillis, h.getNextRotationTime(now));
    long[] rTimes = { 1000, 2000, 10000 };
    h.setRotationTimes(rTimes);
    assertEquals(tomorrowMillis + 1000, h.getNextRotationTime(tomorrowMillis));
    assertEquals(tomorrowMillis + 10000, h.getNextRotationTime(tomorrowMillis + 3000));
    // don't want regular unit tests to create tiles....
    boolean okToWrite = false;
    if (okToWrite) {
        LogRecord lr = new LogRecord(Level.INFO, "test");
        h.publish(lr);
        h.publish(new LogRecord(Level.INFO, "another test"));
        h.rotateNow();
        h.publish(lr);
        h.flush();
    }
}
Also used : LogRecord(java.util.logging.LogRecord) SimpleFormatter(java.util.logging.SimpleFormatter) Formatter(java.util.logging.Formatter) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) LogFileHandler(com.yahoo.container.logging.LogFileHandler) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 12 with Formatter

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

the class AppCTAT 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 CTAT ");
    String validReportPath = config.getString("validReportPath");
    String testReportPath = config.getString("testReportPath");
    Stream<Pair<Double, Double>> validStream = ReportUtils.getConfidenceCorrectness(validReportPath).stream();
    List<Pair<Double, Double>> testList = ReportUtils.getConfidenceCorrectness(testReportPath);
    CTAT.Summary summaryValid = CTAT.findThreshold(validStream, config.getDouble("CTAT.targetAccuracy"));
    double ctat = summaryValid.getConfidenceThreshold();
    double ctat_clipped = ctat;
    if (ctat_clipped > config.getDouble("CTAT.upperBound")) {
        ctat_clipped = config.getDouble("CTAT.upperBound");
    }
    if (ctat_clipped < config.getDouble("CTAT.lowerBound")) {
        ctat_clipped = config.getDouble("CTAT.lowerBound");
    }
    FileUtils.writeStringToFile(Paths.get(config.getString("output.folder"), config.getString("CTAT.name") + "_unclipped").toFile(), "" + ctat);
    FileUtils.writeStringToFile(Paths.get(config.getString("output.folder"), config.getString("CTAT.name") + "_clipped").toFile(), "" + ctat_clipped);
    CTAT.Summary summaryTest = CTAT.applyThreshold(testList.stream(), ctat);
    CTAT.Summary summaryTest_clipped = CTAT.applyThreshold(testList.stream(), ctat_clipped);
    logger.info("tuning CTAT is done");
    logger.info("*****************");
    logger.info("autocoding performance with unclipped CTAT " + summaryTest.getConfidenceThreshold());
    logger.info("autocoding percentage = " + summaryTest.getAutoCodingPercentage());
    logger.info("autocoding accuracy = " + summaryTest.getAutoCodingAccuracy());
    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 CTAT " + summaryTest_clipped.getConfidenceThreshold());
    logger.info("autocoding percentage = " + summaryTest_clipped.getAutoCodingPercentage());
    logger.info("autocoding accuracy = " + summaryTest_clipped.getAutoCodingAccuracy());
    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 : 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) CTAT(edu.neu.ccs.pyramid.calibration.CTAT) File(java.io.File) Pair(edu.neu.ccs.pyramid.util.Pair)

Example 13 with Formatter

use of java.util.logging.Formatter in project tomee by apache.

the class SimpleTomEEFormatterTest method formatNullThrown.

@Test
public void formatNullThrown() throws Exception {
    final String previousLineSeparatorProperty = System.getProperty(LINE_SEPARATOR_KEY);
    try {
        final String lineSeparatorValue = "\n";
        final String logMessage = "An example log record";
        final Level level = Level.FINEST;
        System.setProperty(LINE_SEPARATOR_KEY, lineSeparatorValue);
        final LogRecord logRecordInput = new LogRecord(level, logMessage);
        logRecordInput.setThrown(null);
        final Formatter formatter = new SimpleTomEEFormatter();
        final String actualFormatOutput = formatter.format(logRecordInput);
        final String expectedFormatOutput = level.getLocalizedName() + " - " + logMessage + "\n";
        assertEquals(expectedFormatOutput, actualFormatOutput);
    } finally {
        System.setProperty(LINE_SEPARATOR_KEY, previousLineSeparatorProperty);
    }
}
Also used : LogRecord(java.util.logging.LogRecord) Formatter(java.util.logging.Formatter) Level(java.util.logging.Level) Test(org.junit.Test)

Example 14 with Formatter

use of java.util.logging.Formatter in project tomee by apache.

the class SimpleTomEEFormatterTest method formatNotNullThrown.

@Test
public void formatNotNullThrown() throws Exception {
    final String previousLineSeparatorProperty = System.getProperty(LINE_SEPARATOR_KEY);
    try {
        final String lineSeparatorValue = "\n";
        final String logMessage = "An example log record";
        final Level level = Level.CONFIG;
        final String exceptionMessage = "An example exception";
        final Throwable thrown = new Exception(exceptionMessage);
        System.setProperty(LINE_SEPARATOR_KEY, lineSeparatorValue);
        final LogRecord logRecordInput = new LogRecord(level, logMessage);
        logRecordInput.setThrown(thrown);
        final Formatter formatter = new SimpleTomEEFormatter();
        final String actualFormatOutput = formatter.format(logRecordInput);
        final String expectedFormatOutput = level.getLocalizedName() + " - " + logMessage + lineSeparatorValue + ExceptionUtils.getStackTrace(thrown);
        assertEquals(expectedFormatOutput, actualFormatOutput);
    } finally {
        System.setProperty(LINE_SEPARATOR_KEY, previousLineSeparatorProperty);
    }
}
Also used : LogRecord(java.util.logging.LogRecord) Formatter(java.util.logging.Formatter) Level(java.util.logging.Level) Test(org.junit.Test)

Example 15 with Formatter

use of java.util.logging.Formatter in project tomee by apache.

the class LocalFileHandler method configure.

private void configure() {
    date = currentDate();
    // allow classes to override
    final String className = getClass().getName();
    final ClassLoader cl = Thread.currentThread().getContextClassLoader();
    dateCheckInterval = new Duration(getProperty(className + ".dateCheckInterval", String.valueOf(dateCheckInterval))).asMillis();
    filenamePattern = replace(getProperty(className + ".filenamePattern", filenamePattern));
    limit = new Size(getProperty(className + ".limit", String.valueOf("10 Mega"))).asBytes();
    final int lastSep = Math.max(filenamePattern.lastIndexOf('/'), filenamePattern.lastIndexOf('\\'));
    String fileNameReg = lastSep >= 0 ? filenamePattern.substring(lastSep + 1) : filenamePattern;
    // date.
    fileNameReg = fileNameReg.replace("%s", "\\d{4}\\-\\d{2}\\-\\d{2}");
    {
        // file rotation index
        final int indexIdxStart = fileNameReg.indexOf('%');
        if (indexIdxStart >= 0) {
            final int indexIdxEnd = fileNameReg.indexOf('d', indexIdxStart);
            if (indexIdxEnd >= 0) {
                fileNameReg = fileNameReg.substring(0, indexIdxStart) + "\\d*" + fileNameReg.substring(indexIdxEnd + 1, fileNameReg.length());
            }
        }
    }
    filenameRegex = Pattern.compile(fileNameReg);
    compressionLevel = Integer.parseInt(getProperty(className + ".compressionLevel", String.valueOf(Deflater.DEFAULT_COMPRESSION)));
    archiveExpiryDuration = new Duration(getProperty(className + ".archiveOlderThan", String.valueOf("-1 days"))).asMillis();
    archiveDir = new File(replace(getProperty(className + ".archiveDirectory", "${catalina.base}/logs/archives/")));
    archiveFormat = replace(getProperty(className + ".archiveFormat", archiveFormat));
    archiveFilenameRegex = Pattern.compile(fileNameReg + "\\." + archiveFormat);
    purgeExpiryDuration = new Duration(getProperty(className + ".purgeOlderThan", String.valueOf("-1 days"))).asMillis();
    try {
        bufferSize = (int) new Size(getProperty(className + ".bufferSize", "-1 b")).asBytes();
    } catch (final NumberFormatException ignore) {
    // no-op
    }
    final String encoding = getProperty(className + ".encoding", null);
    if (encoding != null && encoding.length() > 0) {
        try {
            setEncoding(encoding);
        } catch (final UnsupportedEncodingException ex) {
        // no-op
        }
    }
    setLevel(Level.parse(getProperty(className + ".level", "" + Level.ALL)));
    final String filterName = getProperty(className + ".filter", null);
    if (filterName != null) {
        try {
            setFilter(Filter.class.cast(cl.loadClass(filterName).newInstance()));
        } catch (final Exception e) {
        // Ignore
        }
    }
    final String formatterName = getProperty(className + ".formatter", null);
    if (formatterName != null) {
        try {
            setFormatter(Formatter.class.cast(cl.loadClass(formatterName).newInstance()));
        } catch (final Exception e) {
            setFormatter(newSimpleFormatter(className));
        }
    } else {
        setFormatter(newSimpleFormatter(className));
    }
    setErrorManager(new ErrorManager());
    lastTimestamp = System.currentTimeMillis();
}
Also used : ErrorManager(java.util.logging.ErrorManager) FilenameFilter(java.io.FilenameFilter) Filter(java.util.logging.Filter) Formatter(java.util.logging.Formatter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) File(java.io.File) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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