Java Reference
In-Depth Information
In the standard, the built-in levels include finest , finer , fine , info , warning , and
severe . In this particular case, the input parameters are being logged at info level. To ex-
ecute this script with x and y set to 3 and 4 , use the following code:
Binding b = new Binding(x:3, y:4)
GroovyShell shell = new GroovyShell(b)
shell.evaluate(new File('src/main/groovy/mjg/calc_with_logger.groovy'))
println shell.context.z
The result is similar to this (dates and times may vary):
Jun 24, 2013 12:21:19 AM
org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethod
SiteNoUnwrap invoke
INFO: Received (x,y) = (3,4)
7
The default logger includes a console “appender,” which directs all log output to the con-
sole. The mechanisms for capturing standard output don't work here, though. Instead,
Groovy provides a class called GroovyLogTestCase , which includes a static method
called stringLog for that purpose. The next listing shows a test demonstrating its use.
Listing 6.15. Capturing log output in a test case
class CalcWithLoggerTests extends GroovyLogTestCase {
void testAddition() {
def result = stringLog(Level.INFO, calc_with_logger.class.name) {
Binding b = new Binding()
b.x = 3; b.y = 4
GroovyShell shell = new GroovyShell(b)
shell.evaluate(
new File('src/main/groovy/mjg/calc_with_logger.groovy'))
assert 7 == shell.context.z
}
assert result.contains('INFO: Received (x,y) = (3,4)')
}
}
The stringLog method returns the log output as a string, which is used to check that the
logger is working correctly.
Search WWH ::




Custom Search