How to customize access log rotation (e.g. the timing of log rotation) in EAP 7
Environment
- Red Hat JBoss Enterprise Application Platform (JBoss EAP)
- 7
Issue
- How to enable/disable access log rotation on EAP 7
- How to customize access log rotation on EAP 7
- Is it possible to configure the access log rotation interval other than daily? For example, size-based log rotation or hourly log rotation.
Resolution
Daily log rotation is enabled by default when you activate the access log setting inside undertow subsystem in EAP 7. See this article for details about enabling access logging in EAP 7. (If you want to disable this log rotation, you can set a rotate attribute of <access-log> to false)
When you want to rotate access log at intervals other than daily, you can set use-server-log attribute to true to use logging subsystem to write access logs. Then configure a custom handler (size-rotating-file-handler for size-based rotation and periodic-rotating-file-handler for custom interval) and add a logger for io.undertow.accesslog which uses the handler in logging subsystem:
-
Enable access logging (and also enable response time for http-listner):
/subsystem=undertow/server=default-server/host=default-host/setting=access-log:add(pattern="%h %l %u %t \"%r\" %s %b \"%{i,Referer}\" \"%{i,User-Agent}\" \"%{i,COOKIE}\" \"%{o,SET-COOKIE}\" %S \"%I\" %T") /subsystem=undertow/server=default-server/http-listener=default:write-attribute(name=record-request-start-time,value=true) -
Add a log format1 for access logging:
/subsystem=logging/pattern-formatter=access-log-formatter:add(pattern="%s%n") -
Add a custom handler for access logging:
- Use
size-rotating-file-handlerfor size-based log rotation. The following output to$JBOSS_HOME/standalone/log/access.logwith rotating every 10 MB size and 10 backup files:
/subsystem=logging/size-rotating-file-handler=access-log:add(autoflush=true, append=true, named-formatter=access-log-formatter, rotate-size=10m, max-backup-index=10, file={path=access.log, relative-to=jboss.server.log.dir})or
- Use
periodic-rotating-file-handlerfor custom interval log rotation. The following output to$JBOSS_HOME/standalone/log/access.logwith hourly log rotating by the settingsuffix=".yyyy-MM-dd-HH".
/subsystem=logging/periodic-rotating-file-handler=access-log:add(autoflush=true, append=true, named-formatter=access-log-formatter, suffix=".yyyy-MM-dd-HH", file={path=access.log, relative-to=jboss.server.log.dir}) - Use
-
Add a logger for
io.undertow.accesslogwhich uses the above handler withuse-parent-handlers=falsenot to output to server.log:/subsystem=logging/logger=io.undertow.accesslog:add(handlers=[access-log], use-parent-handlers=false) -
Enable
use-server-logattribute2 totrueof<access-log>setting in undertow subsystem:/subsystem=undertow/server=default-server/host=default-host/setting=access-log:write-attribute(name=use-server-log, value=true) -
Execute
:reloadin CLI or restart JBoss
After the above commands are executed successfully, you will see the configuration like the following:
<subsystem xmlns="urn:jboss:domain:logging:3.0">
...(snip)...
<!-- size-based log rotation -->
<size-rotating-file-handler name="access-log" autoflush="true">
<formatter>
<named-formatter name="access-log-formatter"/>
</formatter>
<file relative-to="jboss.server.log.dir" path="access.log"/>
<rotate-size value="10m"/>
<max-backup-index value="10"/>
<append value="true"/>
</size-rotating-file-handler>
<! -- or custom interval (hourly) log rotation
<periodic-rotating-file-handler name="access-log-custom" autoflush="true">
<formatter>
<named-formatter name="access-log-formatter"/>
</formatter>
<file relative-to="jboss.server.log.dir" path="access.log"/>
<suffix value=".yyyy-MM-dd-HH"/>
<append value="true"/>
</periodic-rotating-file-handler>
--->
...(snip)...
<logger category="io.undertow.accesslog" use-parent-handlers="false">
<handlers>
<handler name="access-log"/>
</handlers>
</logger>
...(snip)...
<formatter name="access-log-formatter">
<pattern-formatter pattern="%s%n"/>
</formatter>
</subsystem>
...(snip)...
<subsystem xmlns="urn:jboss:domain:undertow:3.1" statistics-enabled="true">
...(snip)...
<server name="default-server">
<http-listener name="default" socket-binding="http" redirect-socket="https" record-request-start-time="true"/>
<host name="default-host" alias="localhost">
...(snip)...
<access-log use-server-log="true" pattern="%h %l %u %t "%r" %s %b "%{i,Referer}" "%{i,User-Agent}" "%{i,COOKIE}" "%{o,SET-COOKIE}" %S "%I" %T"/>
...(snip)...
</host>
</server>
...(snip)...
</subsystem>
See the section "12.3.5. Log Formatters" of EAP 7 Configuration Guide for details about log formatter syntax.
See "access-log Attributes" section of "APPENDIX A. REFERENCE MATERIAL" in EAP 7 Configuration Guide
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.