public class RequestLogger extends Object implements Route.Handler
Log all the matched incoming requested using the NCSA format (a.k.a common log format).
{
use("*", new RequestLogger());
...
}
Output looks like:
127.0.0.1 - - [04/Oct/2016:17:51:42 +0000] "GET / HTTP/1.1" 200 2
You probably want to configure the RequestLogger
logger to save output into a new file:
<appender name="ACCESS" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>access.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>access.%d{yyyy-MM-dd}.log</fileNamePattern> </rollingPolicy> <encoder> <pattern>%msg%n</pattern> </encoder> </appender> <logger name="org.jooby.RequestLogger" additivity="false"> <appender-ref ref="ACCESS" /> </logger>
Due that authentication is provided via module or custom filter, there is no concept of logged/authenticated user. Still you can log the current user by setting an user id provider at construction time:
{
use("*", (req, rsp) -> {
// authenticate user and set local attribute
String userId = ...;
req.set("userId", userId);
});
use("*", new RequestLogger(req -> {
return req.get("userId");
}));
}
Here an application filter set an userId
request attribute and then we provide that
userId
to RequestLogger
.
By default it uses the underlying logging system: logback.
That's why we previously show how to configure the org.jooby.RequestLogger
in
logback.xml
.
If you want to log somewhere else and/or use a different technology then:
{
use("*", new ResponseLogger()
.log(line -> {
System.out.println(line);
}));
}
This is just an example but of course you can log the NCSA
line to database, jms
queue, etc...
{
use("*", new RequestLogger()
.latency());
}
It add a new entry at the last of the NCSA
output that represents the number of
ms
it took to process the incoming release.
Extend the NCSA
by adding the Referer
and User-Agent
headers to the output.
{
use("*", new RequestLogger()
.dateFormatter(ts -> ...));
// OR
use("*", new RequestLogger()
.dateFormatter(DateTimeFormatter...));
}
Override, the default formatter for the request arrival time defined by:
Request.timestamp()
. You can provide a function or an instance of
DateTimeFormatter
.
The default formatter use the default server time zone, provided by
ZoneId.systemDefault()
. It's possible to just override the time zone (not the entirely
formatter) too:
{
use("*", new RequestLogger()
.dateFormatter(ZoneId.of("UTC"));
}
Constructor and Description |
---|
RequestLogger()
Creates a new
RequestLogger without user identifier. |
RequestLogger(Function<Request,String> userId)
Creates a new
RequestLogger and use the given function and userId provider. |
Modifier and Type | Method and Description |
---|---|
RequestLogger |
dateFormatter(DateTimeFormatter formatter)
Override the default date formatter.
|
RequestLogger |
dateFormatter(Function<Long,String> formatter)
Override the default date formatter.
|
RequestLogger |
dateFormatter(ZoneId zoneId)
Keep the default formatter but use the provided timezone.
|
RequestLogger |
extended()
Append
Referer and User-Agent entries to the NCSA line. |
void |
handle(Request req,
Response rsp)
Callback method for a HTTP request.
|
RequestLogger |
latency()
Log latency (how long does it takes to process an incoming request) at the end of the NCSA
line.
|
RequestLogger |
log(Consumer<String> log)
Log an NCSA line to somewhere.
|
RequestLogger |
queryString()
Log full path of the request including query string.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
handle
public RequestLogger(Function<Request,String> userId)
RequestLogger
and use the given function and userId provider. Please
note, if the user isn't present this function is allowed to returns -
(dash
character).userId
- User ID provider.public RequestLogger()
RequestLogger
without user identifier.public void handle(Request req, Response rsp) throws Throwable
Route.Handler
handle
in interface Route.Handler
req
- A HTTP request.rsp
- A HTTP response.Throwable
- If something goes wrong. The exception will processed by Jooby.public RequestLogger log(Consumer<String> log)
{
use("*", new RequestLogger()
.log(System.out::println)
);
}
log
- Log callback.public RequestLogger dateFormatter(DateTimeFormatter formatter)
formatter
- New formatter to use.public RequestLogger dateFormatter(Function<Long,String> formatter)
formatter
- New formatter to use.public RequestLogger dateFormatter(ZoneId zoneId)
zoneId
- Zone id.public RequestLogger latency()
public RequestLogger queryString()
public RequestLogger extended()
Referer
and User-Agent
entries to the NCSA line.Copyright © 2019. All rights reserved.