Search in sources :

Example 26 with LogRecord

use of java.util.logging.LogRecord in project reversehttp by tonyg.

the class HttpServer method complain.

protected void complain(String msg, Throwable thrown) {
    LogRecord r = new LogRecord(Level.SEVERE, msg);
    r.setThrown(thrown);
    Logger.getLogger(this.getClass().getName()).log(r);
}
Also used : LogRecord(java.util.logging.LogRecord)

Example 27 with LogRecord

use of java.util.logging.LogRecord in project reversehttp by tonyg.

the class ServiceContainer method handleRequest.

public void handleRequest(HttpRequest req) {
    String targetLocalname = req.getRawPath().substring(1);
    Address target = new Address(targetLocalname, domain);
    String senderStr = req.getHeader("X-SMQP-Sender");
    Address sender = senderStr == null ? null : Address.parse(senderStr);
    String contentType = req.getHeader("Content-type");
    String method = req.getHeader("X-SMQP-Method", "send");
    Message msg = new Message(sender, target, req.getBody(), contentType, method);
    try {
        if (pathMap.containsKey(targetLocalname)) {
            MessageHandler handler = pathMap.get(targetLocalname);
            int responseCode = handler.handleMessage(msg);
            req.setResponse(responseCode, "");
        } else {
            req.setResponse(404, "Destination not found");
        }
    } catch (Exception e) {
        LogRecord r = new LogRecord(Level.SEVERE, "Exception at " + target);
        r.setThrown(e);
        Logger.getLogger(this.getClass().getName()).log(r);
        req.setResponse(500, "Internal error");
    }
}
Also used : LogRecord(java.util.logging.LogRecord)

Example 28 with LogRecord

use of java.util.logging.LogRecord in project commons by twitter.

the class JULBridgeHandlerTest method checkMessageWithResourceBundleIsFormatted.

@Test
public void checkMessageWithResourceBundleIsFormatted() {
    ResourceBundle bundle = new ListResourceBundle() {

        @Override
        protected Object[][] getContents() {
            return new Object[][] { { "test is successful", "le test fonctionne" } };
        }
    };
    LogRecord record = new LogRecord(Level.FINEST, "test is successful");
    record.setResourceBundle(bundle);
    assertThat(JULBridgeHandler.formatMessage(record), is("le test fonctionne"));
}
Also used : LogRecord(java.util.logging.LogRecord) ListResourceBundle(java.util.ListResourceBundle) ListResourceBundle(java.util.ListResourceBundle) ResourceBundle(java.util.ResourceBundle) Test(org.junit.Test)

Example 29 with LogRecord

use of java.util.logging.LogRecord in project commons by twitter.

the class JULBridgeHandlerTest method checkGetLoggerReturnsLoggerWithSameName.

@Test
public void checkGetLoggerReturnsLoggerWithSameName() {
    LogRecord record = new LogRecord(Level.FINEST, "test message");
    record.setLoggerName("test.checkGetLogger");
    assertThat(new JULBridgeHandler().getLogger(record).getName(), is("test.checkGetLogger"));
}
Also used : JULBridgeHandler(com.twitter.common.logging.julbridge.JULBridgeHandler) LogRecord(java.util.logging.LogRecord) Test(org.junit.Test)

Example 30 with LogRecord

use of java.util.logging.LogRecord in project commons by twitter.

the class JULBridgeHandlerTest method checkToLoggingEvent.

@Test
public void checkToLoggingEvent() {
    LogRecord record = new LogRecord(Level.FINEST, "test is {0}");
    record.setParameters(new Object[] { "successful" });
    record.setThreadID(42);
    Throwable t = new Throwable();
    record.setThrown(t);
    // source class and method names are usually inferred, but because there's no JUL in the stack
    // frame, it won't work as expected.
    record.setSourceClassName(getClass().getName());
    record.setSourceMethodName("checkToLoggingEvent");
    Logger log4jLogger = new JULBridgeHandler().getLogger(record);
    org.apache.log4j.Level log4jLevel = JULBridgeLevelConverter.toLog4jLevel(Level.FINEST);
    LoggingEvent event = JULBridgeHandler.toLoggingEvent(record, log4jLogger, log4jLevel, false);
    assertThat(event.getLogger(), is((Category) log4jLogger));
    assertThat(event.getLevel(), is(log4jLevel));
    assertThat(event.getMessage(), is((Object) "test is successful"));
    assertThat(event.getThreadName(), is("42"));
    assertThat(event.getTimeStamp(), is(record.getMillis()));
    assertThat(event.getThrowableInformation().getThrowable(), is(sameInstance(t)));
    LocationInfo info = event.getLocationInformation();
    assertThat(info.getClassName(), is(getClass().getName()));
    assertThat(info.getMethodName(), is("checkToLoggingEvent"));
}
Also used : JULBridgeHandler(com.twitter.common.logging.julbridge.JULBridgeHandler) LoggingEvent(org.apache.log4j.spi.LoggingEvent) Category(org.apache.log4j.Category) LogRecord(java.util.logging.LogRecord) Logger(org.apache.log4j.Logger) LocationInfo(org.apache.log4j.spi.LocationInfo) Test(org.junit.Test)

Aggregations

LogRecord (java.util.logging.LogRecord)289 Test (org.junit.Test)53 Logger (java.util.logging.Logger)45 ByteArrayOutputStream (java.io.ByteArrayOutputStream)16 Handler (java.util.logging.Handler)16 StreamHandler (java.util.logging.StreamHandler)14 ConsoleHandler (java.util.logging.ConsoleHandler)13 File (java.io.File)12 Properties (java.util.Properties)11 SimpleFormatter (java.util.logging.SimpleFormatter)10 LogRecordCollectingLogger (alma.acs.testsupport.LogRecordCollectingLogger)9 Formatter (java.util.logging.Formatter)9 ArrayList (java.util.ArrayList)8 IOException (java.io.IOException)7 Subscribe (com.google.common.eventbus.Subscribe)6 HashMap (java.util.HashMap)6 Level (java.util.logging.Level)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 FileHandler (java.util.logging.FileHandler)5 Filter (java.util.logging.Filter)4