Package ratpack.core.handling
Interface RequestLogger
-
- All Superinterfaces:
Handler
public interface RequestLogger extends Handler
A handler that logs information about the request.Implementations need only implement the
log(RequestOutcome)
method. This interface provides a default implementation ofhandle()
that calls thelog()
method before delegating to the next handler. Theof(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.core.handling.RequestLogger; import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.*; 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()); }); } }
-
-
Field Summary
Fields Modifier and Type Field Description static org.slf4j.Logger
LOGGER
The default request logger.static String
LOGGER_NAME
The name ofLOGGER
: "ratpack.request".
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default void
handle(Context ctx)
Addslog(RequestOutcome)
as acontext close action
, effectively logging the request.void
log(RequestOutcome outcome)
Format the providedRequestOutcome
to the given string builder.static RequestLogger
ncsa()
Callsncsa(Logger)
withLOGGER
.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 thelog(RequestOutcome)
method.
-
-
-
Field Detail
-
LOGGER_NAME
static final String LOGGER_NAME
The name ofLOGGER
: "ratpack.request".- See Also:
- Constant Field Values
-
LOGGER
static final org.slf4j.Logger LOGGER
The default request logger.- See Also:
LOGGER_NAME
-
-
Method Detail
-
of
static RequestLogger of(Action<? super RequestOutcome> action)
Creates a request logger with the given action as the implementation of thelog(RequestOutcome)
method.import ratpack.core.handling.RequestLogger; import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.*; 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
.- Parameters:
action
- an action that logs information about the request- Returns:
- a request logger implementation
-
ncsa
static RequestLogger ncsa()
Callsncsa(Logger)
withLOGGER
.- Returns:
- a new request logger
-
ncsa
static RequestLogger ncsa(org.slf4j.Logger logger)
Logs in the NCSA Common Log format. The format for the request log is "host rfc931 username date:time request statuscode bytes" as defined by the NCSA Common (access logs) format (see link). However, if theRequestId
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"- Parameters:
logger
- the logger to log to, at INFO level- Returns:
- a new request logger
- See Also:
- NCSA Common Log format documentation.
-
log
void log(RequestOutcome outcome) throws Exception
Format the providedRequestOutcome
to the given string builder.- Parameters:
outcome
- the resulting outcome of a received request- Throws:
Exception
- any
-
handle
default void handle(Context ctx)
Addslog(RequestOutcome)
as acontext close action
, effectively logging the request.The handler calls
Context.next()
after adding the context close action.
-
-