Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

SLF4J and newer jUnit #20

Merged
merged 5 commits into from over 2 years ago

2 participants

Enrique Zamudio Ray Myers
Enrique Zamudio

I updated the Gradle script to use 1.8.3 because it's what I use. It compiled fine, but you might want to keep using an older version. I also updated to jUnit 4.10, which shouldn't be a problem.

I also added SLF4J; the servlet now has a logger instead of using println. The App also has a logger, as does the request delegate, which is cool because now the scripts can use logger.info, logger.error, etc.

Enrique Zamudio

I don't know if this will run on Groovy 1.7.1 which is the version that's originally used for RatPack...

Enrique Zamudio

You might want to keep using 1.7, I don't know.

Enrique Zamudio

Upgrading to jUnit 4.10 shouldn't be a problem

Ray Myers
Collaborator

JUnit update, Groovy version update, and logging approved. We may move to log4j rather than slf in the future but this is a good step. Nice job, chochos!

Ray Myers raymyers merged commit aed31ed into from
Enrique Zamudio

Thanks!

The nice thing about SLF4J is that you can use log4j as a backend, or logback, or JUL; you decide at runtime. But I still don't know of an easy way to configure it; I tried using @Grab in the script and it didn't work, that's why I ended up using @GrabConfig in the startup script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
6 build.gradle
@@ -10,14 +10,16 @@ repositories {
10 10 }
11 11
12 12 dependencies {
13   - groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.7.9'
  13 + groovy group: 'org.codehaus.groovy', name: 'groovy', version: '1.8.3'
14 14
15 15 compile group: 'javax.servlet', name: 'servlet-api', version: '2.5'
16 16 compile group: 'org.json', name: 'json', version: '20090211'
17 17 compile(group: 'org.mortbay.jetty', name: 'jetty', version: '6.1.26') { exclude module: 'servlet-api' }
18 18 compile(group: 'org.mortbay.jetty', name: 'jetty-util', version: '6.1.26') { exclude module: 'servlet-api' }
  19 + compile 'org.slf4j:slf4j-api:1.6.3'
19 20
20   - testCompile group: 'junit', name: 'junit', version: '4.8.2'
  21 + testCompile group: 'junit', name: 'junit', version: '4.10'
  22 + testRuntime 'org.slf4j:slf4j-simple:1.6.3'
21 23 }
22 24
23 25 task copyToLib(type: Copy) {
4 src/main/binaries/ratpack
... ... @@ -1,5 +1,9 @@
1 1 #!/usr/bin/env groovy
2 2 def loader = this.class.classLoader.rootLoader
  3 +@Grapes([
  4 + @Grab('org.slf4j:slf4j-simple:1.6.3'),
  5 + @GrabConfig(systemClassLoader=true)
  6 +])
3 7 def commandPath = getClass().protectionDomain.codeSource.location.path
4 8 def buildPath = new File(commandPath).parentFile.parentFile.parentFile
5 9 def jardir = new File("${buildPath}/ratpack",'lib')
3  src/main/groovy/com/bleedingwolf/ratpack/RatpackApp.groovy
@@ -2,9 +2,12 @@ package com.bleedingwolf.ratpack
2 2
3 3 import com.bleedingwolf.ratpack.routing.Route
4 4 import com.bleedingwolf.ratpack.routing.RoutingTable
  5 +import org.slf4j.LoggerFactory
5 6
6 7 class RatpackApp {
7 8
  9 + final logger = LoggerFactory.getLogger(getClass())
  10 +
8 11 def handlers = [
9 12 'GET': new RoutingTable(),
10 13 'POST': new RoutingTable(),
2  src/main/groovy/com/bleedingwolf/ratpack/RatpackRequestDelegate.groovy
... ... @@ -1,5 +1,6 @@
1 1 package com.bleedingwolf.ratpack
2 2 import org.json.JSONObject
  3 +import org.slf4j.LoggerFactory
3 4
4 5 public class RatpackRequestDelegate {
5 6
@@ -12,6 +13,7 @@ public class RatpackRequestDelegate {
12 13 def request = null
13 14 def response = null
14 15 def requestParamReader = new RatpackRequestParamReader()
  16 + final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass())
15 17
16 18 void setHeader(name, value) {
17 19 response.setHeader(name.toString(), value.toString())
15 src/main/groovy/com/bleedingwolf/ratpack/RatpackServlet.groovy
@@ -8,9 +8,11 @@ import javax.servlet.http.HttpServletResponse
8 8 import org.mortbay.jetty.Server
9 9 import org.mortbay.jetty.servlet.Context
10 10 import org.mortbay.jetty.servlet.ServletHolder
11   -
  11 +import org.slf4j.LoggerFactory
12 12
13 13 class RatpackServlet extends HttpServlet {
  14 + protected final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass())
  15 +
14 16 def app = null
15 17 MimetypesFileTypeMap mimetypesFileTypeMap = new MimetypesFileTypeMap()
16 18
@@ -19,7 +21,7 @@ class RatpackServlet extends HttpServlet {
19 21 def appScriptName = getServletConfig().getInitParameter("app-script-filename")
20 22 def fullScriptPath = getServletContext().getRealPath("WEB-INF/lib/${appScriptName}")
21 23
22   - println "Loading app from script '${appScriptName}'"
  24 + logger.info('Loading app from script "{}"', appScriptName)
23 25 loadAppFromScript(fullScriptPath)
24 26 }
25 27 mimetypesFileTypeMap.addMimeTypes(this.class.getResourceAsStream('mime.types').text)
@@ -51,8 +53,7 @@ class RatpackServlet extends HttpServlet {
51 53 }
52 54 catch(RuntimeException ex) {
53 55
54   - // FIXME: use log4j or similar
55   - println "[ Error] Caught Exception: ${ex}"
  56 + logger.error('Handling {} {}', [ verb, path, ex] as Object[])
56 57
57 58 res.status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR
58 59 output = renderer.renderException(ex, req)
@@ -84,8 +85,7 @@ class RatpackServlet extends HttpServlet {
84 85 stream.flush()
85 86 stream.close()
86 87
87   - // FIXME: use log4j or similar
88   - println "[ ${res.status}] ${verb} ${path}"
  88 + logger.info("[ ${res.status}] ${verb} ${path}")
89 89 }
90 90
91 91 protected boolean staticFileExists(path) {
@@ -125,8 +125,7 @@ class RatpackServlet extends HttpServlet {
125 125 def servlet = new RatpackServlet()
126 126 servlet.app = theApp
127 127
128   - println "Starting Ratpack app with config:"
129   - println theApp.config
  128 + theApp.logger.info('Starting Ratpack app with config:\n{}', theApp.config)
130 129
131 130 def server = new Server(theApp.config.port)
132 131 def root = new Context(server, "/", Context.SESSIONS)

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.