6.29.6 Enabling Access Logger
In the spirit of apache mod_log_config and Tomcat Access Log Valve, it is possible to enable an access logger for the HTTP server (this works for both HTTP/1 and HTTP/2).
To enable and configure the access logger, in application.yml
set:
Enabling the access logger
micronaut:
server:
netty:
access-logger:
enabled: true # Enables the access logger
logger-name: my-access-logger # A logger name, optional, default is `HTTP_ACCESS_LOGGER`
log-format: common # A log format, optional, default is Common Log Format
Logback Configuration
In addition to enabling the access logger, you must add a logger for the specified or default logger name. For instance using the default logger name for logback:
Logback configuration
<appender
name="httpAccessLogAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>true</append>
<file>log/http-access.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>log/http-access-%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>%msg%n</pattern>
</encoder>
<immediateFlush>true</immediateFlush>
</appender>
<logger name="HTTP_ACCESS_LOGGER" additivity="false" level="info">
<appender-ref ref="httpAccessLogAppender" />
</logger>
The pattern should only have the message marker, as other elements will be processed by the access logger.
Log Format
The syntax is based on Apache httpd log format.
These are the supported markers:
%a - Remote IP address
%A - Local IP address
%b - Bytes sent, excluding HTTP headers, or ‘-‘ if no bytes were sent
%B - Bytes sent, excluding HTTP headers
%h - Remote host name
%H - Request protocol
%{<header>}i - Request header. If the argument is omitted (%i) all headers are printed
%{<header>}o - Response header. If the argument is omitted (%o) all headers are printed
%{<cookie>}C - Request cookie (COOKIE). If the argument is omitted (%C) all cookies are printed
%{<cookie>}c - Response cookie (SET_COOKIE). If the argument is omitted (%c) all cookies are printed
%l - Remote logical username from identd (always returns ‘-‘)
%m - Request method
%p - Local port
%q - Query string (excluding the ‘?’ character)
%r - First line of the request
%s - HTTP status code of the response
%{<format>}t - Date and time. If the argument is omitted, Common Log Format is used (“‘[‘dd/MMM/yyyy:HH:mm:ss Z’]‘“).
If the format starts with begin: (default) the time is taken at the beginning of the request processing. If it starts with end: it is the time when the log entry gets written, close to the end of the request processing.
The format should follow
DateTimeFormatter
syntax.
%{property}u - Remote authenticated user. When micronaut-session is on the classpath, returns the session id if the argument is omitted, or the specified property otherwise prints ‘-‘
%U - Requested URI
%v - Local server name
%D - Time taken to process the request, in milliseconds
%T - Time taken to process the request, in seconds
In addition, you can use the following aliases for common patterns:
common -
%h %l %u %t "%r" %s %b
for Common Log Format (CLF)combined -
%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"
for Combined Log Format