Search in sources :

Example 31 with Formatter

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

the class LogFileHandlerTestCase method testSymlink.

@Test
public void testSymlink() {
    LogFileHandler h = new LogFileHandler();
    h.setFilePattern("./testlogforsymlinkchecking/logfilehandlertest.%Y%m%d%H%M%S%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");
        }
    });
    h.setSymlinkName("symlink");
    LogRecord lr = new LogRecord(Level.INFO, "test");
    h.publish(lr);
    String f1 = h.getFileName();
    String f2 = null;
    try {
        while (f1 == null) {
            Thread.sleep(1);
            f1 = h.getFileName();
        }
        h.rotateNow();
        Thread.sleep(1);
        f2 = h.getFileName();
        while (f1.equals(f2)) {
            Thread.sleep(1);
            f2 = h.getFileName();
        }
        lr = new LogRecord(Level.INFO, "string which is way longer than the word test");
        h.publish(lr);
        Thread.sleep(1000);
        File f = new File(f1);
        long first = f.length();
        f = new File(f2);
        long second = f.length();
        final long secondLength = 72;
        for (int n = 0; n < 20 && second != secondLength; ++n) {
            Thread.sleep(1000);
            second = f.length();
        }
        f = new File("./testlogforsymlinkchecking", "symlink");
        long link = f.length();
        assertEquals(secondLength, link);
        assertEquals(31, first);
        assertEquals(secondLength, second);
    } catch (InterruptedException e) {
    // just let the test pass
    }
    deleteOnExit("./testlogforsymlinkchecking");
    deleteOnExit("./testlogforsymlinkchecking/symlink");
    deleteOnExit(f1);
    if (f2 != null)
        deleteOnExit(f2);
}
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) File(java.io.File) Date(java.util.Date) Test(org.junit.Test)

Example 32 with Formatter

use of java.util.logging.Formatter in project elki by elki-project.

the class LogPane method publish.

/**
 * Publish a log record to the logging pane.
 *
 * @param record
 *        Log record
 * @throws Exception
 */
protected synchronized void publish(LogRecord record) throws BadLocationException {
    // choose an appropriate formatter
    final Formatter fmt;
    final Style style;
    // always format progress messages using the progress formatter.
    if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
        // format errors using the error formatter
        fmt = errformat;
        style = errStyle;
    } else if (record.getLevel().intValue() <= Level.FINE.intValue()) {
        // format debug statements using the debug formatter.
        fmt = debugformat;
        style = dbgStyle;
    } else {
        // default to the message formatter.
        fmt = msgformat;
        style = msgStyle;
    }
    // format
    final String m;
    m = fmt.format(record);
    StyledDocument doc = getStyledDocument();
    if (record instanceof ProgressLogRecord) {
        if (lastNewlinePos < doc.getLength()) {
            doc.remove(lastNewlinePos, doc.getLength() - lastNewlinePos);
        }
    } else {
        // insert a newline, if we didn't see one yet.
        if (lastNewlinePos < doc.getLength()) {
            doc.insertString(doc.getLength(), "\n", style);
            lastNewlinePos = doc.getLength();
        }
    }
    int tail = tailingNonNewline(m, 0, m.length());
    int headlen = m.length() - tail;
    if (headlen > 0) {
        String pre = m.substring(0, headlen);
        doc.insertString(doc.getLength(), pre, style);
    }
    lastNewlinePos = doc.getLength();
    if (tail > 0) {
        String post = m.substring(m.length() - tail);
        doc.insertString(lastNewlinePos, post, style);
    }
}
Also used : Formatter(java.util.logging.Formatter) ErrorFormatter(de.lmu.ifi.dbs.elki.logging.ErrorFormatter) MessageFormatter(de.lmu.ifi.dbs.elki.logging.MessageFormatter) StyledDocument(javax.swing.text.StyledDocument) Style(javax.swing.text.Style) ProgressLogRecord(de.lmu.ifi.dbs.elki.logging.progress.ProgressLogRecord)

Example 33 with Formatter

use of java.util.logging.Formatter in project elki by elki-project.

the class CLISmartHandler method publish.

/**
 * Publish a log record.
 */
@Override
public void publish(final LogRecord record) {
    // determine destination
    final Writer destination;
    if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
        destination = this.err;
    } else {
        destination = this.out;
    }
    // format
    final String m;
    // Progress records are handled specially.
    if (record instanceof ProgressLogRecord) {
        ProgressLogRecord prec = (ProgressLogRecord) record;
        ptrack.addProgress(prec.getProgress());
        Collection<Progress> completed = ptrack.removeCompleted();
        Collection<Progress> progresses = ptrack.getProgresses();
        StringBuilder buf = new StringBuilder();
        if (!completed.isEmpty()) {
            buf.append(OutputStreamLogger.CARRIAGE_RETURN);
            for (Progress prog : completed) {
                // TODO: use formatter, somehow?
                prog.appendToBuffer(buf);
                buf.append(OutputStreamLogger.NEWLINE);
            }
        }
        if (!progresses.isEmpty()) {
            boolean first = true;
            buf.append(OutputStreamLogger.CARRIAGE_RETURN);
            for (Progress prog : progresses) {
                if (first) {
                    first = false;
                } else {
                    buf.append(' ');
                }
                // TODO: use formatter, somehow?
                prog.appendToBuffer(buf);
            }
        }
        m = buf.toString();
    } else {
        // choose an appropriate formatter
        final Formatter fmt;
        // always format progress messages using the progress formatter.
        if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
            // format errors using the error formatter
            fmt = errformat;
        } else if (record.getLevel().intValue() <= Level.FINE.intValue()) {
            // format debug statements using the debug formatter.
            fmt = debugformat;
        } else {
            // default to the message formatter.
            fmt = msgformat;
        }
        try {
            m = fmt.format(record);
        } catch (Exception ex) {
            reportError(null, ex, ErrorManager.FORMAT_FAILURE);
            return;
        }
    }
    // write
    try {
        destination.write(m);
        // always flush (although the streams should auto-flush already)
        destination.flush();
    } catch (Exception ex) {
        reportError(null, ex, ErrorManager.WRITE_FAILURE);
        return;
    }
}
Also used : Progress(de.lmu.ifi.dbs.elki.logging.progress.Progress) Formatter(java.util.logging.Formatter) ProgressLogRecord(de.lmu.ifi.dbs.elki.logging.progress.ProgressLogRecord) Writer(java.io.Writer)

Example 34 with Formatter

use of java.util.logging.Formatter in project moleculer-java by moleculer-java.

the class AsyncFileLogger method run.

public void run() {
    LinkedList<LogRecord> records = new LinkedList<>();
    try {
        Formatter formatter = getFormatter();
        StringBuilder lines = new StringBuilder(512);
        while (true) {
            // Get next records
            synchronized (messages) {
                while (messages.isEmpty()) {
                    messages.wait(15000);
                }
                records.addAll(messages);
                messages.clear();
            }
            // Write records to console and/or file
            writeLines(records, lines, formatter);
            // Waiting for other log records
            Thread.sleep(400);
        }
    } catch (InterruptedException interrupt) {
        return;
    } catch (Exception e) {
        records.clear();
        e.printStackTrace();
    }
}
Also used : LogRecord(java.util.logging.LogRecord) Formatter(java.util.logging.Formatter) LinkedList(java.util.LinkedList)

Example 35 with Formatter

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

the class LibraryCollector method main.

// used for testing purposes
public static void main(String[] args) {
    LibraryCollector collector = new LibraryCollector();
    ConsoleHandler handler = new ConsoleHandler();
    handler.setLevel(Level.FINEST);
    Formatter formatter = new SimpleFormatter();
    handler.setFormatter(formatter);
    Logger log = Logger.getLogger(PUBLIC_LOGGER_NAME);
    log.addHandler(handler);
    log.setLevel(Level.FINEST);
    // assumes args[0] is the path to the core file, args[1] is the type of collection
    CollectorResult result = collector.collectLibrariesFor(args[0], args[1]);
    System.out.println("Collection completed with result : " + result.name());
}
Also used : SimpleFormatter(java.util.logging.SimpleFormatter) Formatter(java.util.logging.Formatter) SimpleFormatter(java.util.logging.SimpleFormatter) Logger(java.util.logging.Logger) ConsoleHandler(java.util.logging.ConsoleHandler)

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