Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

java - How to log application auditing to separate file on Wildfly 8

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

What I want to do is to write these audit calls to a separate audit log file. I tried implementing this using logback, and with the help of the second answer in this stackoverflow question I finally managed to do this. The first reply, i.e. disabling the system logging, did not work. However, while this solution successfully writes my audit trace to a separate file, all other logging stopped being written to their default files and was only output into the console.

What I want to achieve is to have all regular logging written to the regular file (i.e. server.log) as it is by default, but to have my own custom audit log messages in a separate file (also rolling on a daily basis, renaming the old file to the date it was written).

Whether this is done with Logback, log4j, Wildfly's own logging system or even the Wildfly CLI audit log, is irrelevant as long as it achieves the purpose, with minimal overhead. I am at this stage considering writing it into my own file with a simple outputstream, but that feels rather superfluous when there are solutions that should do this much more efficiently.

This is what my logback 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I finally managed to achieve what I wanted by modifying the standalone.xml file in Wildfly. I added a custom file-handler and logger that uses this file-handler. No need for a custom logback 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...