Polish Undertow access logs contribution

- Apply project’s code formatting and conventions
 - Don’t use the IO and worker thread configuration when creating the
   worker for the AccessLogReceiver. The IO and worker thread
   configuration is for HTTP request processing and a worker in its
   default configuration should be sufficient for the access log
   receiver.
 - Don’t use a temporary directory as the default for the access log
   directory. A temporary directory makes (some) sense for Tomcat as it
   requires a directory for its basedir. Undertow has no such
   requirement and using a temporary directory makes it hard to locate
   the logs. The default has been updated to a directory named logs,
   created in the current working directory.
 - Document the new properties in the application properties appendix

Closes gh-3014
This commit is contained in:
Andy Wilkinson 2015-05-20 10:31:27 +01:00
parent 90e43737bb
commit 95f31b0d30
6 changed files with 56 additions and 60 deletions

View File

@ -582,7 +582,7 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
/**
* Format pattern for access logs.
*/
private String accessLogPattern;
private String accessLogPattern = "common";
/**
* Enable access log.
@ -590,9 +590,9 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
private boolean accessLogEnabled = false;
/**
* Undertow access log directory. If not specified a temporary directory will be used.
* Undertow access log directory.
*/
private File accessLogDir;
private File accessLogDir = new File("logs");
public Integer getBufferSize() {
return this.bufferSize;
@ -635,7 +635,7 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
}
public String getAccessLogPattern() {
return accessLogPattern;
return this.accessLogPattern;
}
public void setAccessLogPattern(String accessLogPattern) {
@ -643,7 +643,7 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
}
public boolean isAccessLogEnabled() {
return accessLogEnabled;
return this.accessLogEnabled;
}
public void setAccessLogEnabled(boolean accessLogEnabled) {
@ -651,7 +651,7 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
}
public File getAccessLogDir() {
return accessLogDir;
return this.accessLogDir;
}
public void setAccessLogDir(File accessLogDir) {

View File

@ -95,6 +95,9 @@ content into your application; rather pick only the properties that you need.
server.tomcat.max-http-header-size= # maximum size in bytes of the HTTP message header
server.tomcat.max-threads = 0 # number of threads in protocol handler
server.tomcat.uri-encoding = UTF-8 # character encoding to use for URL decoding
server.undertow.access-log-enabled=false # if access logging is enabled
server.undertow.access-log-pattern=common # log pattern of the access log
server.undertow.access-log-dir=logs # access logs directory
# SPRING MVC ({sc-spring-boot-autoconfigure}/web/WebMvcProperties.{sc-ext}[WebMvcProperties])
spring.mvc.locale= # set fixed locale, e.g. en_UK

View File

@ -1,2 +1,3 @@
server.undertow.access-log-enabled=true
server.undertow.access-log-dir=target/logs
server.undertow.access-log-pattern=combined

View File

@ -32,7 +32,6 @@ import org.apache.commons.logging.LogFactory;
*
* @author Phillip Webb
* @author Dave Syer
* @author Marcos Barbero
*/
public abstract class AbstractEmbeddedServletContainerFactory extends
AbstractConfigurableEmbeddedServletContainer implements
@ -79,25 +78,6 @@ public abstract class AbstractEmbeddedServletContainerFactory extends
return file;
}
/**
* Returns the absolute temp dir for given web server.
* @param prefix webserver name
* @return The temp dir for given web server.
*/
protected File createTempDir(String prefix) {
try {
File tempFolder = File.createTempFile(prefix + ".", "." + getPort());
tempFolder.delete();
tempFolder.mkdir();
tempFolder.deleteOnExit();
return tempFolder;
}
catch (IOException ex) {
throw new EmbeddedServletContainerException(
String.format("Unable to create %s tempdir", prefix), ex);
}
}
private File getExplodedWarFileDocumentRoot() {
File file = getCodeSourceArchive();
if (this.logger.isDebugEnabled()) {

View File

@ -79,7 +79,6 @@ import org.springframework.util.StringUtils;
* @author Brock Mills
* @author Stephane Nicoll
* @author Andy Wilkinson
* @author Marcos Barbero
* @see #setPort(int)
* @see #setContextLifecycleListeners(Collection)
* @see TomcatEmbeddedServletContainer
@ -388,6 +387,25 @@ public class TomcatEmbeddedServletContainerFactory extends
this.resourceLoader = resourceLoader;
}
/**
* Returns the absolute temp dir for given web server.
* @param prefix webserver name
* @return The temp dir for given web server.
*/
protected File createTempDir(String prefix) {
try {
File tempFolder = File.createTempFile(prefix + ".", "." + getPort());
tempFolder.delete();
tempFolder.mkdir();
tempFolder.deleteOnExit();
return tempFolder;
}
catch (IOException ex) {
throw new EmbeddedServletContainerException(
"Unable to create Tomcat tempdir", ex);
}
}
/**
* Set the Tomcat base directory. If not specified a temporary directory will be used.
* @param baseDirectory the tomcat base directory

View File

@ -22,6 +22,7 @@ import io.undertow.UndertowMessages;
import io.undertow.server.HandlerWrapper;
import io.undertow.server.HttpHandler;
import io.undertow.server.handlers.accesslog.AccessLogHandler;
import io.undertow.server.handlers.accesslog.AccessLogReceiver;
import io.undertow.server.handlers.accesslog.DefaultAccessLogReceiver;
import io.undertow.server.handlers.resource.ClassPathResourceManager;
import io.undertow.server.handlers.resource.FileResourceManager;
@ -76,8 +77,8 @@ import org.springframework.util.ResourceUtils;
import org.xnio.OptionMap;
import org.xnio.Options;
import org.xnio.SslClientAuthMode;
import org.xnio.XnioWorker;
import org.xnio.Xnio;
import org.xnio.XnioWorker;
/**
* {@link EmbeddedServletContainerFactory} that can be used to create
@ -366,47 +367,40 @@ public class UndertowEmbeddedServletContainerFactory extends
deploymentInfo.addInitialHandlerChainWrapper(new HandlerWrapper() {
@Override
public HttpHandler wrap(HttpHandler handler) {
try {
String formatString = (accessLogPattern != null) ? accessLogPattern
: "common";
DefaultAccessLogReceiver accessLogReceiver = new DefaultAccessLogReceiver(
createWorker(), getLogsDir(), "access_log");
return new AccessLogHandler(handler, accessLogReceiver, formatString,
Undertow.class.getClassLoader());
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
return createAccessLogHandler(handler);
}
});
}
private AccessLogHandler createAccessLogHandler(HttpHandler handler) {
try {
createAccessLogDirectoryIfNecessary();
AccessLogReceiver accessLogReceiver = new DefaultAccessLogReceiver(
createWorker(), this.accessLogDirectory, "access_log");
String formatString = (this.accessLogPattern != null) ? this.accessLogPattern
: "common";
return new AccessLogHandler(handler, accessLogReceiver, formatString,
Undertow.class.getClassLoader());
}
catch (IOException ex) {
throw new IllegalStateException("Failed to create AccessLogHandler", ex);
}
}
private void createAccessLogDirectoryIfNecessary() {
Assert.notNull(this.accessLogDirectory, "accesslogDirectory must not be null");
if (!this.accessLogDirectory.isDirectory() && !this.accessLogDirectory.mkdirs()) {
throw new IllegalStateException("Failed to create access log directory '"
+ this.accessLogDirectory + "'");
}
}
private XnioWorker createWorker() throws IOException {
Xnio xnio = Xnio.getInstance(Undertow.class.getClassLoader());
OptionMap.Builder builder = OptionMap.builder();
if(this.ioThreads != null && this.ioThreads > 0) {
builder.set(Options.WORKER_IO_THREADS, ioThreads);
}
if(this.workerThreads != null && this.workerThreads > 0) {
builder.set(Options.WORKER_TASK_CORE_THREADS, workerThreads);
builder.set(Options.WORKER_TASK_MAX_THREADS, workerThreads);
}
return xnio.createWorker(builder.getMap());
}
private File getLogsDir() {
File logsDir;
if (accessLogDirectory != null) {
logsDir = accessLogDirectory;
if (!logsDir.isDirectory() && !logsDir.mkdirs()) {
throw new IllegalStateException("Failed to create logs dir '" + logsDir + "'");
}
} else {
logsDir = createTempDir("undertow");
}
return logsDir;
}
private void registerServletContainerInitializerToDriveServletContextInitializers(
DeploymentInfo deployment, ServletContextInitializer... initializers) {
ServletContextInitializer[] mergedInitializers = mergeInitializers(initializers);
@ -517,7 +511,7 @@ public class UndertowEmbeddedServletContainerFactory extends
}
public boolean isAccessLogEnabled() {
return accessLogEnabled;
return this.accessLogEnabled;
}
/**