下面列出了怎么用org.eclipse.jetty.server.RequestLog的API类实例代码及写法,或者点击链接到github查看源代码。
JettyServiceConfig(@Nullable String hostname,
@Nullable Boolean dumpAfterStart, @Nullable Boolean dumpBeforeStop,
@Nullable Long stopTimeoutMillis,
@Nullable Handler handler, @Nullable RequestLog requestLog,
@Nullable Function<? super Server, ? extends SessionIdManager> sessionIdManagerFactory,
Map<String, Object> attrs, List<Bean> beans, List<HandlerWrapper> handlerWrappers,
List<Listener> eventListeners, List<LifeCycle.Listener> lifeCycleListeners,
List<Consumer<? super Server>> configurators) {
this.hostname = hostname;
this.dumpAfterStart = dumpAfterStart;
this.dumpBeforeStop = dumpBeforeStop;
this.stopTimeoutMillis = stopTimeoutMillis;
this.handler = handler;
this.requestLog = requestLog;
this.sessionIdManagerFactory = sessionIdManagerFactory;
this.attrs = Collections.unmodifiableMap(attrs);
this.beans = Collections.unmodifiableList(beans);
this.handlerWrappers = Collections.unmodifiableList(handlerWrappers);
this.eventListeners = Collections.unmodifiableList(eventListeners);
this.lifeCycleListeners = Collections.unmodifiableList(lifeCycleListeners);
this.configurators = Collections.unmodifiableList(configurators);
}
static String toString(
Object holder, @Nullable String hostname, @Nullable Boolean dumpAfterStart,
@Nullable Boolean dumpBeforeStop, @Nullable Long stopTimeout,
@Nullable Handler handler, @Nullable RequestLog requestLog,
@Nullable Function<? super Server, ? extends SessionIdManager> sessionIdManagerFactory,
Map<String, Object> attrs, List<Bean> beans, List<HandlerWrapper> handlerWrappers,
List<Listener> eventListeners, List<LifeCycle.Listener> lifeCycleListeners,
List<Consumer<? super Server>> configurators) {
return MoreObjects.toStringHelper(holder)
.add("hostname", hostname)
.add("dumpAfterStart", dumpAfterStart)
.add("dumpBeforeStop", dumpBeforeStop)
.add("stopTimeoutMillis", stopTimeout)
.add("handler", handler)
.add("requestLog", requestLog)
.add("sessionIdManagerFactory", sessionIdManagerFactory)
.add("attrs", attrs)
.add("beans", beans)
.add("handlerWrappers", handlerWrappers)
.add("eventListeners", eventListeners)
.add("lifeCycleListeners", lifeCycleListeners)
.add("configurators", configurators)
.toString();
}
@Test
public void testAppenderDefined() {
HttpRequestLogAppender requestLogAppender = new HttpRequestLogAppender();
requestLogAppender.setName("testrequestlog");
Logger.getLogger("http.requests.test").addAppender(requestLogAppender);
RequestLog requestLog = HttpRequestLog.getRequestLog("test");
Logger.getLogger("http.requests.test").removeAppender(requestLogAppender);
assertNotNull("RequestLog should not be null", requestLog);
assertEquals("Class mismatch",
CustomRequestLog.class, requestLog.getClass());
}
public static RequestLog getRequestLog(String name) {
String lookup = SERVER_TO_COMPONENT.get(name);
if (lookup != null) {
name = lookup;
}
String loggerName = "http.requests." + name;
Slf4jRequestLog logger = new Slf4jRequestLog();
logger.setLoggerName(loggerName);
return logger;
}
private RequestLog createRequestLog() {
final NCSARequestLog log = new NCSARequestLog();
final File logPath = new File(accessLogFile);
final File parentFile = logPath.getParentFile();
if (parentFile != null) {
parentFile.mkdirs();
}
log.setFilename(logPath.getPath());
log.setAppend(true);
log.setLogTimeZone("GMT");
log.setLogLatency(true);
return log;
}
private void initializeWebServer(String name, String hostName,
ConfigurationSource conf, String[] pathSpecs,
String authFilterConfigPrefix,
boolean securityEnabled) throws IOException {
Preconditions.checkNotNull(webAppContext);
int maxThreads = conf.getInt(HTTP_MAX_THREADS_KEY, -1);
// If HTTP_MAX_THREADS is not configured, QueueThreadPool() will use the
// default value (currently 250).
QueuedThreadPool threadPool = (QueuedThreadPool) webServer.getThreadPool();
threadPool.setDaemon(true);
if (maxThreads != -1) {
threadPool.setMaxThreads(maxThreads);
}
SessionHandler handler = webAppContext.getSessionHandler();
handler.setHttpOnly(true);
handler.getSessionCookieConfig().setSecure(true);
ContextHandlerCollection contexts = new ContextHandlerCollection();
RequestLog requestLog = HttpRequestLog.getRequestLog(name);
handlers.addHandler(contexts);
if (requestLog != null) {
RequestLogHandler requestLogHandler = new RequestLogHandler();
requestLogHandler.setRequestLog(requestLog);
handlers.addHandler(requestLogHandler);
}
handlers.addHandler(webAppContext);
final String appDir = getWebAppsPath(name);
addDefaultApps(contexts, appDir, conf);
webServer.setHandler(handlers);
Map<String, String> xFrameParams = setHeaders(conf);
addGlobalFilter("safety", QuotingInputFilter.class.getName(), xFrameParams);
final FilterInitializer[] initializers = getFilterInitializers(conf);
if (initializers != null) {
conf.set(BIND_ADDRESS, hostName);
org.apache.hadoop.conf.Configuration hadoopConf =
LegacyHadoopConfigurationSource.asHadoopConfiguration(conf);
Map<String, String> filterConfig = getFilterConfigMap(hadoopConf,
authFilterConfigPrefix);
for (FilterInitializer c : initializers) {
if ((c instanceof AuthenticationFilterInitializer) && securityEnabled) {
addFilter("authentication",
AuthenticationFilter.class.getName(), filterConfig);
} else {
c.initFilter(this, hadoopConf);
}
}
}
addDefaultServlets();
if (pathSpecs != null) {
for (String path : pathSpecs) {
LOG.info("adding path spec: {}", path);
addFilterPathMapping(path, webAppContext);
}
}
}
public static RequestLog getRequestLog(String name) {
String lookup = SERVER_TO_COMPONENT.get(name);
if (lookup != null) {
name = lookup;
}
String loggerName = "http.requests." + name;
String appenderName = name + "requestlog";
Log logger = LogFactory.getLog(loggerName);
boolean isLog4JLogger;
try {
isLog4JLogger = logger instanceof Log4JLogger;
} catch (NoClassDefFoundError err) {
// In some dependent projects, log4j may not even be on the classpath at
// runtime, in which case the above instanceof check will throw
// NoClassDefFoundError.
LOG.debug("Could not load Log4JLogger class", err);
isLog4JLogger = false;
}
if (isLog4JLogger) {
Log4JLogger httpLog4JLog = (Log4JLogger) logger;
org.apache.log4j.Logger httpLogger = httpLog4JLog.getLogger();
Appender appender = null;
try {
appender = httpLogger.getAppender(appenderName);
} catch (LogConfigurationException e) {
LOG.warn("Http request log for {} could not be created", loggerName);
throw e;
}
if (appender == null) {
LOG.info("Http request log for {} is not defined", loggerName);
return null;
}
if (appender instanceof HttpRequestLogAppender) {
HttpRequestLogAppender requestLogAppender
= (HttpRequestLogAppender) appender;
AsyncRequestLogWriter logWriter = new AsyncRequestLogWriter();
logWriter.setFilename(requestLogAppender.getFilename());
logWriter.setRetainDays(requestLogAppender.getRetainDays());
return new CustomRequestLog(logWriter,
CustomRequestLog.EXTENDED_NCSA_FORMAT);
} else {
LOG.warn("Jetty request log for {} was of the wrong class", loggerName);
return null;
}
} else {
LOG.warn("Jetty request log can only be enabled using Log4j");
return null;
}
}
@Test
public void testAppenderUndefined() {
RequestLog requestLog = HttpRequestLog.getRequestLog("test");
assertNull("RequestLog should be null", requestLog);
}
@Nullable
RequestLog requestLog() {
return requestLog;
}
private static RequestLog buildRequestLog() {
final Slf4jRequestLog requestLog = new Slf4jRequestLog();
requestLog.setLoggerName(LOG.getName() + ".RequestLog");
requestLog.setExtended(true);
return requestLog;
}
@Test
public void testAppenderDefined() {
RequestLog requestLog = HttpRequestLog.getRequestLog("test");
assertNotNull("RequestLog should not be null", requestLog);
assertEquals("Class mismatch", Slf4jRequestLog.class, requestLog.getClass());
}