public class AccessLogHandler extends Object implements Route.Decorator
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 setRouter.setTrustProxy(boolean)
option.
{
decorator(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:
{
decorator("*", new AccessLogHandler(ctx -> {
// retrieve user ID from context.
}));
}
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...
{
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.
You can add extra headers using the requestHeader(String...)
and
responseHeader(String...)
{
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"));
}
Constructor and Description |
---|
AccessLogHandler()
Creates a new
AccessLogHandler without user identifier. |
AccessLogHandler(Function<Context,String> userId)
Creates a new
AccessLogHandler and use the given function and userId provider. |
Modifier and Type | Method and Description |
---|---|
Route.Handler |
apply(Route.Handler next)
Chain the decorator within next handler.
|
AccessLogHandler |
dateFormatter(DateTimeFormatter formatter)
Override the default date formatter.
|
AccessLogHandler |
dateFormatter(Function<Long,String> formatter)
Override the default date formatter.
|
AccessLogHandler |
dateFormatter(ZoneId zoneId)
Keep the default formatter but use the provided timezone.
|
AccessLogHandler |
extended()
Append
Referer and User-Agent entries to the NCSA line. |
AccessLogHandler |
log(Consumer<String> log)
Log an NCSA line to somewhere.
|
AccessLogHandler |
requestHeader(String... names)
Append request headers to the end of line.
|
AccessLogHandler |
responseHeader(String... names)
Append response headers to the end of line.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
then, then
setRoute
public AccessLogHandler(@Nonnull Function<Context,String> userId)
AccessLogHandler
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 AccessLogHandler()
AccessLogHandler
without user identifier.@Nonnull public Route.Handler apply(@Nonnull Route.Handler next)
Route.Decorator
apply
in interface Route.Decorator
next
- Next handler.@Nonnull public AccessLogHandler log(@Nonnull Consumer<String> log)
{
use("*", new RequestLogger()
.log(System.out::println)
);
}
log
- Log callback.@Nonnull public AccessLogHandler dateFormatter(@Nonnull DateTimeFormatter formatter)
formatter
- New formatter to use.@Nonnull public AccessLogHandler dateFormatter(Function<Long,String> formatter)
formatter
- New formatter to use.@Nonnull public AccessLogHandler dateFormatter(@Nonnull ZoneId zoneId)
zoneId
- Zone id.@Nonnull public AccessLogHandler extended()
Referer
and User-Agent
entries to the NCSA line.@Nonnull public AccessLogHandler requestHeader(@Nonnull String... names)
names
- Header names.@Nonnull public AccessLogHandler responseHeader(@Nonnull String... names)
names
- Header names.Copyright © 2020. All rights reserved.