public interface RequestLogger extends Handler
Implementations need only implement the log(RequestOutcome)
method.
This interface provides a default implementation of handle()
that calls the log()
method before delegating to the next handler.
The of(Action)
can also be used to create a request logger.
Unless there is a good reason not to, loggers should log to LOGGER
at the “info” logging level.
How this logging manifests can then be controlled by configuring the logging subsystem in use.
import ratpack.handling.RequestLogger;
import ratpack.test.embed.EmbeddedApp;
import static org.junit.Assert.*;
public class Example {
public static void main(String... args) throws Exception {
EmbeddedApp.fromHandlers(c -> c
.all(RequestLogger.ncsa())
.all(ctx -> ctx.render("ok"))
).test(httpClient -> {
assertEquals("ok", httpClient.get().getBody().getText());
});
}
}
Modifier and Type | Field and Description |
---|---|
static org.slf4j.Logger |
LOGGER
The default request logger.
|
static String |
LOGGER_NAME
The name of
LOGGER : "ratpack.request". |
Modifier and Type | Method and Description |
---|---|
default void |
handle(Context ctx)
Adds
log(RequestOutcome) as a context close action , effectively logging the request. |
void |
log(RequestOutcome outcome)
Format the provided
RequestOutcome to the given string builder. |
static RequestLogger |
ncsa()
Calls
ncsa(Logger) with LOGGER . |
static RequestLogger |
ncsa(org.slf4j.Logger logger)
Logs in the NCSA Common Log format.
|
static RequestLogger |
of(Action<? super RequestOutcome> action)
Creates a request logger with the given action as the implementation of the
log(RequestOutcome) method. |
static final String LOGGER_NAME
LOGGER
: "ratpack.request".static final org.slf4j.Logger LOGGER
LOGGER_NAME
static RequestLogger of(Action<? super RequestOutcome> action)
log(RequestOutcome)
method.
import ratpack.handling.RequestLogger;
import ratpack.test.embed.EmbeddedApp;
import static org.junit.Assert.*;
public class Example {
public static void main(String... args) throws Exception {
EmbeddedApp.fromHandlers(c -> c
.all(RequestLogger.of(outcome ->
RequestLogger.LOGGER.info(outcome.getRequest().getUri())
))
.all(ctx -> ctx.render("ok"))
).test(httpClient -> {
assertEquals("ok", httpClient.get().getBody().getText());
});
}
}
Unless there is reason not to, the action should log at info level to LOGGER
.
action
- an action that logs information about the requeststatic RequestLogger ncsa()
ncsa(Logger)
with LOGGER
.static RequestLogger ncsa(org.slf4j.Logger logger)
RequestId
is additionally being added to requests, the value of the request Id will be appended to the end of the request log in the form: id=requestId
The resulting format is thus: "host rfc931 username date:time request statuscode bytes id=requestId"logger
- the logger to log to, at INFO levelvoid log(RequestOutcome outcome) throws Exception
RequestOutcome
to the given string builder.outcome
- the resulting outcome of a received requestException
- anydefault void handle(Context ctx)
log(RequestOutcome)
as a context close action
, effectively logging the request.
The handler calls Context.next()
after adding the context close action.