For wildfly (on linux) I need following logging scenario: daily rotation of server.log
and removing old log files which are older than 90 days. I don't see a way to configure this in wildfly/log4j (the problem is here to remove old log files, but I will be happy for tips to do this directly with wildfly configuration). So I have to use linux logrotate for this. I have following logrotate configuration file:
/var/log/wildfly/capp/*.log {
missingok
daily
notifempty
rotate 90
maxage 90
dateext
dateformat -%Y%m%d
}
The server.log
will be rotated successfully in the early morning. But: wildfly is still writing the log messages into the already logrotated file (see at the timestamps of the last write access):
#> ls -la
-rw-r--r-- 1 wildfly-capp wildfly-capp 0 4. Apr 03:40 server.log
-rw-r--r-- 1 wildfly-capp wildfly-capp 368909 4. Apr 07:00 server.log-20190404
Is there a way to force wildfly to use the server.log
file instead the already rotated file (without restarting wildfly)? Or is it possible to change the wildfly logging configuration to remove logging files which are older than x
days?
The wildfly logging configuration is:
<subsystem xmlns="urn:jboss:domain:logging:5.0">
<console-handler name="CONSOLE">
<level name="INFO"/>
<formatter>
<named-formatter name="COLOR-PATTERN"/>
</formatter>
</console-handler>
<file-handler name="FILE" autoflush="true">
<formatter>
<named-formatter name="PATTERN"/>
</formatter>
<file relative-to="jboss.server.log.dir" path="server.log"/>
<append value="true"/>
</file-handler>
<logger category="com.arjuna">
<level name="WARN"/>
</logger>
<logger category="org.jboss.as.config">
<level name="DEBUG"/>
</logger>
<logger category="sun.rmi">
<level name="WARN"/>
</logger>
<root-logger>
<level name="INFO"/>
<handlers>
<handler name="CONSOLE"/>
<handler name="FILE"/>
</handlers>
</root-logger>
<formatter name="PATTERN">
<pattern-formatter pattern="%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%e%n"/>
</formatter>
<formatter name="COLOR-PATTERN">
<pattern-formatter pattern="%K{level}%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%e%n"/>
</formatter>
</subsystem>
After you rotate logs, you need to tell wildfly that logs are rotated and that it should start to write into new log file. Usually, that is done with HUP signal sent to the daemon, or you can just restart it. Otherwise, daemon will keep the filehandle of open file and it will write in the old file. That is done by adding postrotate section telling what needs to be done after logs are rotated. Take a look at examples for postrotate section in logrotate config file. Here are some examples from my computer for ufw and samba:
For wildfly you will have to write your own command(s) to make wildfly reopen log files.
The point is that I don't want restarting wildfly. Thanks to "nobody" for his answer. But now I know logrotate is not right thing for my case.
A workaround is using the "size-rotating-file-handler" (instead of "file-handler") in wildfly. To switch to the size rotating handler I have to edit the
standalone.xml
and replacing the<file-handler name="FILE" autoflush="true">
section of<subsystem xmlns="urn:jboss:domain:logging:5.0">
with:This will rotate the
server.log
file after the log file is greater than 1MB and will keep only 10 log backup files. All older log files will be removed.I think if you want have a daily rotation log file appender which removes log files older than
x
days, you must write a ownDailyRotateFileAppender
which can also removes old log files. Then you must integrate your new file appender into wildfly (wildfly must find the class file and thestandalone.xml
configuration must be changed so that wildfly will be used the new file appender). I think this should work. For me, however, the time required to do this is too large ...Can you combine logrotate and JBOSS options? For standalone configuration change
logging.properties
:And only use logrotate for files with an
-
in the filename: