Permalink
Browse files

Issue #28: Using JDK logger to write Tomcat logs to file.

  • Loading branch information...
1 parent 16d3361 commit c11743a6f5c73d178b9a5e69a163656eafc70c3b @bmuschko committed Jul 18, 2012
View
1 README.md
@@ -95,6 +95,7 @@ and `org.apache.jasper.servlet.JspServlet` will be set up.
server has started. When false, this task blocks until the Tomcat server is stopped (defaults to false).
* `configFile`: The path to the Tomcat context XML file (defaults to `src/main/webapp/META-INF/context.xml` for `tomcatRun`,
defaults to `META-INF/context.xml` within the WAR for `tomcatRunWar`).
+* `outputFile`: The file to write Tomcat log messages to. If the file already exists new messages will be appended.
The following example shows how to change the default HTTP/HTTPS ports for the task `tomcatRun`. To enable SSL we set the
convention property `enableSSL` to `true`. Furthermore, we declare a custom context file.
View
19 plugin/src/main/groovy/org/gradle/api/plugins/tomcat/AbstractTomcatRunTask.groovy
@@ -30,6 +30,10 @@ import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
+import java.util.logging.Level
+
+import static org.gradle.api.plugins.tomcat.internal.LoggingHandler.withJdkFileLogger
+
/**
* Base class for all tasks which deploy a web application to an embedded Tomcat web container.
*
@@ -57,6 +61,7 @@ abstract class AbstractTomcatRunTask extends DefaultTask {
@InputFile @Optional File configFile
URL resolvedConfigFile
Boolean enableSSL
+ File outputFile
abstract void setWebApplicationContext()
@@ -106,7 +111,10 @@ abstract class AbstractTomcatRunTask extends DefaultTask {
private void validateConfigurationAndStartTomcat() {
validateConfiguration()
- startTomcat()
+
+ withJdkFileLogger(getOutputFile(), true, Level.INFO) {
+ startTomcat()
+ }
}
/**
@@ -148,6 +156,15 @@ abstract class AbstractTomcatRunTask extends DefaultTask {
else {
LOGGER.info "HTTPS protocol handler classname = ${getHttpsProtocol()}"
}
+
+ if(getOutputFile()) {
+ if(getOutputFile().path == '') {
+ throw new InvalidUserDataException('The provided output file may not be blank')
+ }
+ else {
+ LOGGER.info "Output file = ${getOutputFile().canonicalPath}"
+ }
+ }
}
/**
View
71 plugin/src/main/groovy/org/gradle/api/plugins/tomcat/internal/LoggingHandler.groovy
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2012 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.gradle.api.plugins.tomcat.internal
+
+import java.util.logging.*
+
+/**
+ * Logging handler support.
+ *
+ * @author Benjamin Muschko
+ */
+class LoggingHandler {
+ /**
+ * Adds file handler to JDK root logger for closure execution.
+ *
+ * @param outputFile Output file
+ * @param append Append
+ * @param level Log level
+ * @param c Closure
+ */
+ static void withJdkFileLogger(File outputFile, boolean append, Level level, Closure c) {
+ if(outputFile) {
+ Logger logger
+ Handler fileHandler
+
+ try {
+ logger = Logger.getLogger('')
+ fileHandler = createFileHandler(outputFile.canonicalPath, append, level)
+ logger.addHandler(fileHandler)
+ c()
+ }
+ finally {
+ if(logger && fileHandler) {
+ logger.removeHandler(fileHandler)
+ }
+ }
+ }
+ else {
+ c()
+ }
+ }
+
+ /**
+ * Creates file handler.
+ *
+ * @param pattern Pattern
+ * @param append Append
+ * @param level Log level
+ * @return File handler
+ */
+ private static FileHandler createFileHandler(String pattern, boolean append, Level level) {
+ Handler fileHandler = new FileHandler(pattern, append)
+ fileHandler.formatter = new SimpleFormatter()
+ fileHandler.level = level
+ fileHandler.encoding = 'UTF-8'
+ fileHandler
+ }
+}

0 comments on commit c11743a

Please sign in to comment.