Search in sources :

Example 16 with LogMessage

use of com.yahoo.log.LogMessage in project vespa by vespa-engine.

the class HandlerThreadTestCase method testAbortThread.

@Test
public void testAbortThread() throws InvalidLogFormatException, InterruptedException {
    HandlerThread thread = new HandlerThread("myThread");
    LogDispatcherTestCase.MockHandler h1 = new LogDispatcherTestCase.MockHandler();
    thread.registerHandler(h1);
    thread.start();
    String msgstr1 = "1098709001\t" + "nalle.puh.com\t" + "23234\t" + "serviceName\t" + "tst\t" + "info\t" + "this is a test";
    final LogMessage msg1 = LogMessage.parseNativeFormat(msgstr1);
    thread.handle(msg1);
    while (h1.messages.size() < 1) {
        Thread.sleep(10);
    }
    assertEquals(h1.messages.size(), 1);
    thread.interrupt();
    thread.join();
}
Also used : LogMessage(com.yahoo.log.LogMessage) LogDispatcherTestCase(com.yahoo.logserver.test.LogDispatcherTestCase) Test(org.junit.Test)

Example 17 with LogMessage

use of com.yahoo.log.LogMessage in project vespa by vespa-engine.

the class ArchiverHandlerTestCase method testLogging.

/**
 * Log some messages to the handler and verify that they are
 * written.
 */
@Test
public void testLogging() throws java.io.IOException, InvalidLogFormatException {
    File tmpDir = temporaryFolder.newFolder();
    try {
        ArchiverHandler a = new ArchiverHandler(tmpDir.getAbsolutePath(), 1024);
        for (int i = 0; i < msg.length; i++) {
            a.handle(msg[i]);
        }
        a.close();
        // make sure all the log files are there, that all log entries
        // are accounted for and that they match what we logged.
        int messageCount = 0;
        // map of files we've already inspected.  this we need to ensure
        // that we are not inspecting the same files over and over again
        // so the counts get messed up
        Set<String> inspectedFiles = new HashSet<String>();
        for (int i = 0; i < msg.length; i++) {
            String name = a.getPrefix(msg[i]) + "-0";
            // have we inspected this file before?
            if (!inspectedFiles.add(name)) {
                continue;
            }
            File f = new File(name);
            assertTrue(f.exists());
            BufferedReader br = new BufferedReader(new FileReader(f));
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                // primitive check if the messages match
                boolean foundMatch = false;
                for (int k = 0; k < mStrings.length; k++) {
                    if (mStrings[k].equals(line)) {
                        foundMatch = true;
                        break;
                    }
                }
                assertTrue(foundMatch);
                // try to instantiate messages to ensure that they
                // are parseable
                @SuppressWarnings("unused") LogMessage m = LogMessage.parseNativeFormat(line);
                messageCount++;
            }
            br.close();
        }
        // verify that the number of log messages written equals
        // the number of log messages we have in our test
        assertEquals(mStrings.length, messageCount);
    } finally {
        IOUtils.recursiveDeleteDir(tmpDir);
    }
}
Also used : LogMessage(com.yahoo.log.LogMessage) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) HashSet(java.util.HashSet)

Example 18 with LogMessage

use of com.yahoo.log.LogMessage in project vespa by vespa-engine.

the class FormattedBufferCacheTestCase method testCache.

@Test
public void testCache() {
    LogMessage[] msgs = MockLogEntries.getMessages();
    FormattedBufferCache cache = new FormattedBufferCache();
    String[] n = LogFormatterManager.getFormatterNames();
    for (int i = 0; i < n.length; i++) {
        LogFormatter f = LogFormatterManager.getLogFormatter(n[i]);
        for (int j = 0; j < msgs.length; j++) {
            ByteBuffer bb = cache.getFormatted(msgs[j], f);
            assertNotNull(bb);
        }
    }
    assertTrue(cache.getUnderlyingMapOnlyForTesting().size() > 0);
    cache.reset();
    assertTrue(cache.getUnderlyingMapOnlyForTesting().size() == 0);
}
Also used : LogMessage(com.yahoo.log.LogMessage) FormattedBufferCache(com.yahoo.logserver.handlers.replicator.FormattedBufferCache) LogFormatter(com.yahoo.logserver.formatter.LogFormatter) ByteBuffer(java.nio.ByteBuffer)

Example 19 with LogMessage

use of com.yahoo.log.LogMessage in project vespa by vespa-engine.

the class LastErrorsHolder method newConnection.

/**
 * Factory method for creating new connections. Since we just return a result
 * when client connection happens, we also write the result here
 *
 * @param socket   The new SocketChannel
 * @param listener The Listener instance we want to use
 */
public Connection newConnection(SocketChannel socket, Listener listener) {
    if (log.isLoggable(LogLevel.DEBUG)) {
        log.log(LogLevel.DEBUG, "New last-errors-holder connection: " + socket);
    }
    LastErrorsHolderConnection connection = new LastErrorsHolderConnection(socket);
    synchronized (lock) {
        Messages messages = new Messages();
        for (LogMessage error : errors) {
            messages.addMessage(new Message(error.getTime() / 1000, error.getHost(), error.getService(), error.getLevel().getName(), error.getPayload()));
        }
        messages.setNumberOfErrors(numberOfErrors);
        try {
            ObjectMapper mapper = new ObjectMapper();
            StringWriter stringWriter = new StringWriter();
            mapper.writeValue(stringWriter, messages);
            connection.enqueue(StandardCharsets.UTF_8.encode(stringWriter.toString()));
        } catch (IOException e) {
            log.log(LogLevel.WARNING, "Could not enqueue log message", e);
        }
        errors.clear();
        numberOfErrors = 0;
    }
    return connection;
}
Also used : LogMessage(com.yahoo.log.LogMessage) StringWriter(java.io.StringWriter) LogMessage(com.yahoo.log.LogMessage) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

LogMessage (com.yahoo.log.LogMessage)19 InvalidLogFormatException (com.yahoo.log.InvalidLogFormatException)5 BufferedReader (java.io.BufferedReader)5 MalformedEventException (com.yahoo.log.event.MalformedEventException)4 FileReader (java.io.FileReader)4 Test (org.junit.Test)4 Event (com.yahoo.log.event.Event)3 MetricsFilter (com.yahoo.logserver.filter.MetricsFilter)2 TextFormatter (com.yahoo.logserver.formatter.TextFormatter)2 LogMetricsHandler (com.yahoo.logserver.handlers.logmetrics.LogMetricsHandler)2 LogDispatcherTestCase (com.yahoo.logserver.test.LogDispatcherTestCase)2 File (java.io.File)2 IOException (java.io.IOException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 LogDispatcher (com.yahoo.logserver.LogDispatcher)1 LogFilter (com.yahoo.logserver.filter.LogFilter)1 NoMetricsFilter (com.yahoo.logserver.filter.NoMetricsFilter)1 LogFormatter (com.yahoo.logserver.formatter.LogFormatter)1 NullFormatter (com.yahoo.logserver.formatter.NullFormatter)1 AbstractLogHandler (com.yahoo.logserver.handlers.AbstractLogHandler)1