Search in sources :

Example 1 with LogFileHandler

use of com.yahoo.container.logging.LogFileHandler in project vespa by vespa-engine.

the class LogFileHandlerTestCase method testDeleteFileDuringLogging.

@Test
public void testDeleteFileDuringLogging() {
    String logFilePattern = "./testLogFileG2.txt";
    // create logfilehandler
    LogFileHandler h = new LogFileHandler();
    h.setFilePattern(logFilePattern);
    h.setFormatter(new SimpleFormatter());
    h.setRotationTimes("0 5 ...");
    // write log
    LogRecord lr = new LogRecord(Level.INFO, "testDeleteFileDuringLogging1");
    h.publish(lr);
    h.flush();
    // delete log file
    delete(logFilePattern);
    // write log again
    lr = new LogRecord(Level.INFO, "testDeleteFileDuringLogging2");
    h.publish(lr);
    h.flush();
    new File(logFilePattern).deleteOnExit();
}
Also used : LogRecord(java.util.logging.LogRecord) SimpleFormatter(java.util.logging.SimpleFormatter) LogFileHandler(com.yahoo.container.logging.LogFileHandler) File(java.io.File) Test(org.junit.Test)

Example 2 with LogFileHandler

use of com.yahoo.container.logging.LogFileHandler 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 3 with LogFileHandler

use of com.yahoo.container.logging.LogFileHandler 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 4 with LogFileHandler

use of com.yahoo.container.logging.LogFileHandler in project vespa by vespa-engine.

the class LogFileHandlerTestCase method testSimpleLogging.

@Test
public void testSimpleLogging() {
    String logFilePattern = "./testLogFileG1.txt";
    // create logfilehandler
    LogFileHandler h = new LogFileHandler();
    h.setFilePattern(logFilePattern);
    h.setFormatter(new SimpleFormatter());
    h.setRotationTimes("0 5 ...");
    // write log
    LogRecord lr = new LogRecord(Level.INFO, "testDeleteFileFirst1");
    h.publish(lr);
    h.flush();
    new File(logFilePattern).deleteOnExit();
}
Also used : LogRecord(java.util.logging.LogRecord) SimpleFormatter(java.util.logging.SimpleFormatter) LogFileHandler(com.yahoo.container.logging.LogFileHandler) File(java.io.File) Test(org.junit.Test)

Aggregations

LogFileHandler (com.yahoo.container.logging.LogFileHandler)4 LogRecord (java.util.logging.LogRecord)4 SimpleFormatter (java.util.logging.SimpleFormatter)4 Test (org.junit.Test)4 File (java.io.File)3 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Formatter (java.util.logging.Formatter)2