use of org.eclipse.jetty.server.CustomRequestLog in project dropwizard by dropwizard.
the class LogbackClassicRequestLogFactory method build.
@Override
public RequestLog build(String name) {
final Logger logger = (Logger) LoggerFactory.getLogger("http.request");
logger.setAdditive(false);
final LoggerContext context = logger.getLoggerContext();
final LevelFilterFactory<ILoggingEvent> levelFilterFactory = new NullLevelFilterFactory<>();
final AsyncAppenderFactory<ILoggingEvent> asyncAppenderFactory = new AsyncLoggingEventAppenderFactory();
final LayoutFactory<ILoggingEvent> layoutFactory = (c, tz) -> new RequestLogLayout(c);
final AppenderAttachableImpl<ILoggingEvent> attachable = new AppenderAttachableImpl<>();
for (AppenderFactory<ILoggingEvent> appender : appenders) {
attachable.addAppender(appender.build(context, name, layoutFactory, levelFilterFactory, asyncAppenderFactory));
}
return new CustomRequestLog(new DropwizardSlf4jRequestLogWriter(attachable), ClassicLogFormat.pattern(timeZone));
}
use of org.eclipse.jetty.server.CustomRequestLog in project dropwizard by dropwizard.
the class LogbackClassicRequestLogFactoryTest method testLogFormat.
@Test
void testLogFormat() throws Exception {
final LogbackClassicRequestLogFactory factory = new LogbackClassicRequestLogFactory();
@SuppressWarnings("unchecked") final Appender<ILoggingEvent> appender = mock(Appender.class);
final Request request = mock(Request.class);
final Response response = mock(Response.class, RETURNS_DEEP_STUBS);
final HttpChannelState channelState = mock(HttpChannelState.class);
factory.setAppenders(Collections.singletonList((context, applicationName, layoutFactory, levelFilterFactory, asyncAppenderFactory) -> appender));
final String tz = TimeZone.getAvailableIDs((int) TimeUnit.HOURS.toMillis(-5))[0];
factory.setTimeZone(TimeZone.getTimeZone(tz));
CustomRequestLog logger = null;
try {
when(channelState.isInitial()).thenReturn(true);
when(request.getRemoteHost()).thenReturn("10.0.0.1");
// Jetty log format compares against System.currentTimeMillis, so there
// isn't a way for us to set our own clock
when(request.getTimeStamp()).thenReturn(System.currentTimeMillis());
when(request.getMethod()).thenReturn("GET");
when(request.getRequestURI()).thenReturn("/test/things");
when(request.getProtocol()).thenReturn("HTTP/1.1");
when(request.getHttpChannelState()).thenReturn(channelState);
when(response.getCommittedMetaData().getStatus()).thenReturn(200);
when(response.getHttpChannel().getBytesWritten()).thenReturn(8290L);
final ArgumentCaptor<ILoggingEvent> captor = ArgumentCaptor.forClass(ILoggingEvent.class);
logger = (CustomRequestLog) factory.build("my-app");
logger.log(request, response);
verify(appender, timeout(1000)).doAppend(captor.capture());
final ILoggingEvent event = captor.getValue();
assertThat(event.getFormattedMessage()).startsWith("10.0.0.1").doesNotContain("%").contains("\"GET /test/things HTTP/1.1\"").contains("-0500");
} catch (Exception e) {
if (logger != null) {
logger.stop();
}
throw e;
}
}
use of org.eclipse.jetty.server.CustomRequestLog in project kafka by apache.
the class JsonRestServer method start.
/**
* Start the JsonRestServer.
*
* @param resources The path handling resources to register.
*/
public void start(Object... resources) {
log.info("Starting REST server");
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(new JacksonJsonProvider(JsonUtil.JSON_SERDE));
for (Object resource : resources) {
resourceConfig.register(resource);
log.info("Registered resource {}", resource);
}
resourceConfig.register(RestExceptionMapper.class);
ServletContainer servletContainer = new ServletContainer(resourceConfig);
ServletHolder servletHolder = new ServletHolder(servletContainer);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.addServlet(servletHolder, "/*");
RequestLogHandler requestLogHandler = new RequestLogHandler();
Slf4jRequestLogWriter slf4jRequestLogWriter = new Slf4jRequestLogWriter();
slf4jRequestLogWriter.setLoggerName(JsonRestServer.class.getCanonicalName());
CustomRequestLog requestLog = new CustomRequestLog(slf4jRequestLogWriter, CustomRequestLog.EXTENDED_NCSA_FORMAT + " %{ms}T");
requestLogHandler.setRequestLog(requestLog);
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] { context, new DefaultHandler(), requestLogHandler });
StatisticsHandler statsHandler = new StatisticsHandler();
statsHandler.setHandler(handlers);
jettyServer.setHandler(statsHandler);
/* Needed for graceful shutdown as per `setStopTimeout` documentation */
jettyServer.setStopTimeout(GRACEFUL_SHUTDOWN_TIMEOUT_MS);
jettyServer.setStopAtShutdown(true);
try {
jettyServer.start();
} catch (Exception e) {
throw new RuntimeException("Unable to start REST server", e);
}
log.info("REST server listening at " + jettyServer.getURI());
}
Aggregations