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();
}
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);
}
}
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);
}
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;
}
Aggregations