简体   繁体   中英

Change date format in Wildfly(JBoss) access log

access_log in wildfly logs the entries with the following predefined date format, which is produced by %t:

[22/Oct/2019:14:28:36 +0300]

However, I would like to change that to be as below:

[22/10/2019 14:28:36.345]

I have tried to change the pattern in standalone xml file as below:

<access-log pattern="%{dd/MMM/yyyy:HH:mm:ss Z}t %t %h %l %u &quot;%r&quot;s %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot;"/>                       

However, the following is being logged instead:

%{dd/MMM/yyyy:HH:mm:ss Z}t [22/Oct/2019:14:28:36 +0300] 127.0.0.1 - - "GET /favicon.ico HTTP/1.1"s 302 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36"

How can I format the date properly?

I use wildfly-10.1.0. I have also tried the format below, that can be used for the apache access logs: But without any success.

There are other similar questions online , but have not been answered.

The pattern used in the access log configuration is very limited and only recognizes these variables , where the date format isn't customizable as far as I know.

You should however be able to have the desired date format by delegating logging to the logging subsystem which accepts more complex formats:

In the undertow subsystem configuration, remove the date from the pattern and add the user-server-log attribute:

<access-log pattern="%t %h %l %u &quot;%r&quot;s %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot;" use-server-log="true"/>

In the logging subsystem, define a logger which will catch your access logs and an associated handler using a formatter that will display the logs with the desired date format:

<periodic-rotating-file-handler name="ACCESS" autoflush="true">
    <formatter>
        <named-formatter name="%d{dd/MMM/yyyy:HH:mm:ss Z} %s"/>
    </formatter>
    <file relative-to="jboss.server.log.dir" path="access.log"/>
    <suffix value=".yyyy-MM-dd"/>
    <append value="true"/>
</periodic-rotating-file-handler>
[...]
<logger category="io.undertow.accesslog" use-parent-handlers="false">
    <handlers>
        <handler name="ACCESS"/>
    </handlers>
</logger>

My answer is largely based on this other answer which details how to set up access log rotation.

Following a lot of troubleshooting, I have finally found the answer, which might be helpful to other people as well:

It is by adding the word 'time' as another argument in the formatter. As below:

<access-log pattern="[%{time,dd/MM/yyyy HH:mm:ss.SSS}] %h %l %u &quot;%r&quot; %s %b &quot;%{i,Referer}&quot; &quot;%{i,User-Agent}&quot;"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM