Request logging
Only available if AcrossWebModule is present. |
In a web context the LoggingModule will register a servlet filter that logs request information. Every request will get a unique id assigned and attributes like the handler name, request url and request mapping will be logged if they can be determined.
Configuration
By default request logging for all requests is enabled through using a servlet filter. This can be altered by setting the logging.request.logger property.
Additionally there are some properties to finetune the request logging, or you can create your own RequestLoggerConfiguration
instance.
Advanced configuration can be used to specify:
-
to which servlets the filter should apply (defaults to all)
-
the path patterns that requests should match before they are logged
-
the log level threshold that allows you to configure different log levels to be used when requests exceed certain durations
logging.request.excluded-path-patterns=/across/**
LoggingModule loggingModule = new LoggingModule();
RequestLoggerConfiguration requestLoggerConfiguration = RequestLoggerConfiguration.allRequests();
requestLoggerConfiguration.setExcludedPathPatterns( Arrays.asList( "/across/**" ) );
loggingModule.setProperty( LoggingModuleSettings.REQUEST_LOGGER_CONFIGURATION, requestLoggerConfiguration );
The RequestLogger.INTERCEPTOR option can be used to enable request logging without the use of the servlet filter.
Though it still works, this option is no longer actively supported.
|
MDC attributes
The RequestLoggerFilter
adds two attributes to the MDC:
- requestId
-
A generated id that uniquely identifies the request. By adding the requestId to every log appender, log statements can be traced back to a specific request.
- applicationInstanceId
-
A configured value identifying the application instance. Only if the ApplicationInfoModule is active.
Logging output
A request log entry is a tab-separated log message sent to the SLF4J logger named com.foreach.across.modules.logging.request.RequestLogger
.
The tab-separated log message contains the following fields:
-
Remote address
-
HTTP method
-
URL
-
Servlet path
-
Best matching request mapping
-
Handler name
-
View name
-
HTTP status code
-
Duration (ms)
Depending on the application configuration or type of request, certain fields might not be available. In that case a field-dependent default value (either empty, 0 or null) will be output, but the position of the field will always exist so tab-based parsing does not fail.
Logback configuration
The following section provides a common Logback example for configuration of request logging, and the resulting output.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="pages" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{ISO8601}\t[%X{requestId}]\t%t\t%level\t%m%n</pattern>
</encoder>
</appender>
<logger name="com.foreach.across.modules.logging.request.RequestLogger" level="debug" additivity="false">
<appender-ref ref="pages"/>
</logger>
</configuration>
2015-11-02 12:58:15,212 [5f18be5e-5b64-4c3a-9843-740f16c32641] http-apr-8080-exec-4 DEBUG 127.0.0.1 GET http://localhost:8080/logging/debug/across/browser/info/0 /debug/across/browser/info/0 /debug/across/browser/info/{index} AcrossInfoController.showContextInfo(List,int,Model) th/debugweb/layouts/acrossBrowser 200 784
Timestamp |
2015-11-02 12:58:15,212 |
Request id |
5f18be5e-5b64-4c3a-9843-740f16c32641 |
Thread |
http-apr-8080-exec-4 |
Log level |
DEBUG |
Remote address |
127.0.0.1 |
HTTP method |
GET |
Request URL |
|
Servlet path |
/debug/across/browser/info/0 |
Request mapping |
/debug/across/browser/info/{index} |
Handler name |
AcrossInfoController.showContextInfo(List,int,Model) |
View name |
th/debugweb/layouts/acrossBrowser |
HTTP status code |
200 |
Duration |
784 |