How to register application audit for a single file on Wildfly 8

I have a Java EE application running on Wildfly 8 in which I want to enable audit logging. Using InterceptorBinding and Interceptor, I can catch all the relevant API calls.

What I want to do is write these audit calls to a separate audit log file. I tried to implement this using logback, and using the second answer in https://stackoverflow.com/a/3/3129/ ... I finally managed to do this. The first answer, i.e. Disabling system registration did not work. However, although this solution successfully writes my audit trail to a separate file, all other records ceased to be written to the default files and were only displayed on the console.

I want to ensure that all regular entries are written to a regular file (for example, server.log) at my discretion, but to have my own audit log messages in a separate file (also rename the old file on the date it was written on a daily basis).

Whether this is done using Logback, log4j, Wildfly’s own logging system, or even the Wildfly CLI audit trail, it doesn’t matter as long as it reaches the goal with minimal overhead. I am at this stage looking at it in my own file with a simple output stream, but this seems redundant when there are solutions that should make it much more efficient.

This is what my log file looks like:

<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="AUDIT-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>/Applications/wildfly/standalone/log/logback/audit/audit.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logFile.%d{yyyy-MM-dd}.log</FileNamePattern> </rollingPolicy> <encoder> <pattern>%d{HH:mm:ss.SSS}: - %msg%n</pattern> </encoder> </appender> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>/Applications/wildfly/standalone/log/logback/server.log</File> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logFile.%d{yyyy-MM-dd}.log</FileNamePattern> </rollingPolicy> <encoder> <Pattern>%d{HH:mm:ss.SSS} %-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern> </encoder> </appender> <logger name="audit" level="INFO" additivity="false"> <appender-ref ref="AUDIT-FILE"/> </logger> <logger name="org.jboss.resteasy.core.ExceptionHandler" level="ALL"> <appender-ref ref="FILE" /> </logger> <root level="ALL"> <appender-ref ref="FILE"/> </root> </configuration> 
+5
source share
1 answer

I finally managed to achieve what I wanted by changing the standalone.xml file in Wildfly. I added a custom handler and a logger that uses this handler. There is no need for a custom backup implementation or anything like that.

  <subsystem xmlns="urn:jboss:domain:logging:2.0"> <console-handler name="CONSOLE"> <level name="INFO"/> <formatter> <named-formatter name="COLOR-PATTERN"/> </formatter> </console-handler> <periodic-rotating-file-handler name="FILE" autoflush="true"> <formatter> <named-formatter name="PATTERN"/> </formatter> <file relative-to="jboss.server.log.dir" path="server.log"/> <suffix value=".yyyy-MM-dd"/> <append value="true"/> </periodic-rotating-file-handler> <periodic-rotating-file-handler name="MYHANDLER" autoflush="true"> <formatter> <named-formatter name="PATTERN"/> </formatter> <file relative-to="jboss.server.log.dir" path="application-audit.log"/> <suffix value=".yyyy-MM-dd"/> <append value="true"/> </periodic-rotating-file-handler> <logger category="com.mycompany.myapplication"> <level name="INFO"/> <handlers> <handler name="MYHANDLER"/> </handlers> </logger> <logger category="com.arjuna"> <level name="WARN"/> </logger> <logger category="org.apache.tomcat.util.modeler"> <level name="WARN"/> </logger> <logger category="org.jboss.as.config"> <level name="DEBUG"/> </logger> <logger category="sun.rmi"> <level name="WARN"/> </logger> <logger category="jacorb"> <level name="WARN"/> </logger> <logger category="jacorb.config"> <level name="ERROR"/> </logger> <logger category="org.jboss.security"> <level name="TRACE"/> </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> 
+10
source

Source: https://habr.com/ru/post/1209204/


All Articles