- All Implemented Interfaces:
Route.Aware
,Route.Filter
Log incoming requested using the NCSA format (a.k.a common log format).
If you run behind a reverse proxy that has been configured to send the X-Forwarded-* header,
please consider to set Router.setTrustProxy(boolean)
option.
usage
{
use(new AccessLogHandler());
...
}
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 AccessLogHandler
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="io.jooby.AccessLogHandler" additivity="false"> <appender-ref ref="ACCESS" /> </logger>
By defaults it log the available user context: Context.getUser()
. To override this:
{
use("*", new AccessLogHandler(ctx -> {
// retrieve user ID from context.
}));
}
custom log function
By default it uses the underlying logging system: logback.
That's why we previously show how to configure the io.jooby.AccessLogHandler
in
logback.xml
.
If you want to log somewhere else and/or use a different method then:
{
use("*", new AccessLogHandler()
.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...
latency
{
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.
request and response headers
You can add extra headers using the requestHeader(String...)
and
responseHeader(String...)
dateFormatter
{
use("*", new RequestLogger()
.dateFormatter(ts -> ...));
// OR
use("*", new RequestLogger()
.dateFormatter(DateTimeFormatter...));
}
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"));
}
- Since:
- 2.5.2
- Author:
- edgar
-
Constructor Summary
ConstructorDescriptionCreates a newAccessLogHandler
without user identifier.AccessLogHandler
(Function<Context, String> userId) Creates a newAccessLogHandler
and use the given function and userId provider. -
Method Summary
Modifier and TypeMethodDescriptionapply
(Route.Handler next) Chain the filter within next handler.dateFormatter
(DateTimeFormatter formatter) Override the default date formatter.dateFormatter
(ZoneId zoneId) Keep the default formatter but use the provided timezone.dateFormatter
(Function<Long, String> formatter) Override the default date formatter.extended()
AppendReferer
andUser-Agent
entries to the NCSA line.Log an NCSA line to somewhere.requestHeader
(String... names) Append request headers to the end of line.responseHeader
(String... names) Append response headers to the end of line.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.jooby.Route.Aware
setRoute
Methods inherited from interface io.jooby.Route.Filter
then, then
-
Constructor Details
-
AccessLogHandler
Creates a newAccessLogHandler
and use the given function and userId provider. Please note, if the user isn't present this function is allowed to returns-
(dash character).- Parameters:
userId
- User ID provider.
-
AccessLogHandler
public AccessLogHandler()Creates a newAccessLogHandler
without user identifier.
-
-
Method Details
-
apply
Description copied from interface:Route.Filter
Chain the filter within next handler.- Specified by:
apply
in interfaceRoute.Filter
- Parameters:
next
- Next handler.- Returns:
- A new handler.
-
log
Log an NCSA line to somewhere.{ use("*", new RequestLogger() .log(System.out::println) ); }
- Parameters:
log
- Log callback.- Returns:
- This instance.
-
dateFormatter
Override the default date formatter.- Parameters:
formatter
- New formatter to use.- Returns:
- This instance.
-
dateFormatter
Override the default date formatter.- Parameters:
formatter
- New formatter to use.- Returns:
- This instance.
-
dateFormatter
Keep the default formatter but use the provided timezone.- Parameters:
zoneId
- Zone id.- Returns:
- This instance.
-
extended
AppendReferer
andUser-Agent
entries to the NCSA line.- Returns:
- This instance.
-
requestHeader
Append request headers to the end of line.- Parameters:
names
- Header names.- Returns:
- This instance.
-
responseHeader
Append response headers to the end of line.- Parameters:
names
- Header names.- Returns:
- This instance.
-