Package ratpack.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
LOGGERat 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()); }); } }
-
-
Field Summary
Fields Modifier and Type Field Description static LoggerLOGGERThe default request logger.static java.lang.StringLOGGER_NAMEThe name ofLOGGER: "ratpack.request".
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default voidhandle(Context ctx)Addslog(RequestOutcome)as acontext close action, effectively logging the request.voidlog(RequestOutcome outcome)Format the providedRequestOutcometo the given string builder.static RequestLoggerncsa()Callsncsa(Logger)withLOGGER.static RequestLoggerncsa(Logger logger)Logs in the NCSA Common Log format.static RequestLoggerof(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 java.lang.String LOGGER_NAME
The name ofLOGGER: "ratpack.request".- See Also:
- Constant Field Values
-
LOGGER
static final 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.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.- 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(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 theRequestIdis 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 java.lang.Exception
Format the providedRequestOutcometo the given string builder.- Parameters:
outcome- the resulting outcome of a received request- Throws:
java.lang.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.
-
-