Skip to content

Instantly share code, notes, and snippets.

@zedar
Last active March 2, 2021 03:50
Show Gist options
  • Save zedar/fe917c050142ac3f6134 to your computer and use it in GitHub Desktop.
Save zedar/fe917c050142ac3f6134 to your computer and use it in GitHub Desktop.
ratpack & log4j2 basic configuration

Install ratpack

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

Add log4j configuration

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")
    }
  }
}
@musikele
Copy link

musikele commented Jan 3, 2017

Can I use the annotation @slf4j in my app ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment