Ratpack is microservices framework written in Java but with very strong Groovy support. Ratpack is set of libraries. There are 2 main tools required for ratpack project: gradle and lazybones. Both could be installed with gvm (Groovy enVironment Manager).
$ gvm install gradle
$ gvm install lazybones
Create project structure with lazybones.
$ lazybones create ratpack project_name
To run default installation run succeeding commands:
$ cd project_name
$ ./gradlew run
Log4j in version 2 added support for asynchronous logging. Open build.gradle file and in dependencies section comment out line with default logging library
runtime 'org.slf4j:slf4j-simple:1.7.7'
Add following, new dependencies:
runtime 'org.apache.logging.log4j:log4j-slf4j-impl:2.0.2'
runtime 'org.apache.logging.log4j:log4j-api:2.0.2'
runtime 'org.apache.logging.log4j:log4j-core:2.0.2'
Dependencies section should look like:
dependencies {
// SpringLoaded enables runtime hot reloading.
// It is not part of the app runtime and is not shipped in the distribution.
springloaded "org.springframework:springloaded:1.2.0.RELEASE"
// Async logging - log4j2 became async logger
runtime 'org.apache.logging.log4j:log4j-slf4j-impl:2.0.2'
runtime 'org.apache.logging.log4j:log4j-api:2.0.2'
runtime 'org.apache.logging.log4j:log4j-core:2.0.2'
testCompile "org.spockframework:spock-core:0.7-groovy-2.0"
}
Create new folder in src/main folder:
$ cd src/main
$ mkdir resources
Add log4j2.xml configuration file:
$ cd resources
$ touch log4j2.xml
Put following content into the log4j.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
<logger name="Ratpack" level="debug">
</logger>
</Loggers>
</Configuration>
The src/ratpack/Ratpack.groovy file has own logger settings with logging level set to DEBUG.
Put slf4j imports into the Ratpack.groovy file
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Define logger in Ratpack.groovy file
final Logger logger = LoggerFactory.getLogger(Ratpack.class)
ratpack {
handlers {
get {
logger.debug("HELLO RATPACK")
render groovyTemplate("index.html", title: "My Ratpack App")
}
}
}
Can I use the annotation @slf4j in my app ?