6.28.6 Enabling Access Logger

In the spirit of apache mod_log_config and Tomcat Access Log Valve, it is possible to enabled an access logger for the http server (it works for both http/1 and http/2.)

To enable and configure the access logger, in application.yml set:

Enabling the access logger

  1. micronaut:
  2. server:
  3. netty:
  4. access-logger:
  5. enabled: true # Enables the access logger
  6. logger-name: my-access-logger # A logger name, optional, default is `HTTP_ACCESS_LOGGER`
  7. log-format: common # A log format, optional, default is Common Log Format

Logback Configuration

In addition to enabling the access logger, you have to add a logger for the specified or default logger name. For instance using the default logger name for logback:

Logback configuration

  1. <appender
  2. name="httpAccessLogAppender"
  3. class="ch.qos.logback.core.rolling.RollingFileAppender">
  4. <append>true</append>
  5. <file>log/http-access.log</file>
  6. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  7. <!-- daily rollover -->
  8. <fileNamePattern>log/http-access-%d{yyyy-MM-dd}.log
  9. </fileNamePattern>
  10. <maxHistory>7</maxHistory>
  11. </rollingPolicy>
  12. <encoder>
  13. <charset>UTF-8</charset>
  14. <pattern>%msg%n</pattern>
  15. </encoder>
  16. <immediateFlush>true</immediateFlush>
  17. </appender>
  18. <logger name="HTTP_ACCESS_LOGGER" additivity="false" level="info">
  19. <appender-ref ref="httpAccessLogAppender" />
  20. </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.

Here 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 will be printed

  • %{<header>}o - Response header. If the argument is omitted (%o) all headers will be printed

  • %{<cookie>}C - Request cookie (COOKIE). If the argument is omitted (%C) all cookies will be printed

  • %{<cookie>}c - Response cookie (SET_COOKIE). If the argument is omitted (%c) all cookies will be 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 the Common Log Format 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 the DateTimeFormatter syntax.

  • %{property}u - Remote user that was authenticated. 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 millis

  • %T - Time taken to process the request, in seconds

In addition, you can use the following aliases for commonly utilized patterns: