18 Logging
Ratpack uses SLF4J for logging, which allows you to easily bind your favorite logging library at compile time.
Library options include:
- No-Op - discards all logs (default)
- Log4J
- Logback - native SLF4J implementation with “zero memory and computational overhead”
- Java Util Logging
- Simple - logs messages at INFO level and higher to System.err
- Jakarta Commons Logging
Simply add one logging library as a dependency and use slf4j syntax to log. If you are currently using another logging library, SLF4J provides a migration tool to automate the transition.
Examples for Java and Groovy are below and more details can be found in the SLF4J manual.
18.1 Java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogExample {
private final static Logger LOGGER = LoggerFactory.getLogger(LogExample.class);
public void log() {
LOGGER.info("Start logging");
LOGGER.warn("Logging with a {} or {}", "parameter", "two");
LOGGER.error("Log an exception", new Exception("Example exception"));
LOGGER.info("Stop logging");
}
}
18.2 Groovy
import groovy.util.logging.Slf4j
@Slf4j
class LogExample {
void log() {
log.info "Start logging"
log.warn "Logging with a {} or {}", "parameter", "two"
log.debug "Detailed information"
log.info "Stop logging"
}
}