如何将应用程序审计记录到 Wildfly 8 上的单独文件中 [英] How to log application auditing to separate file on Wildfly 8

查看:24
本文介绍了如何将应用程序审计记录到 Wildfly 8 上的单独文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 Wildfly 8 上运行的 Java EE 应用程序,我想在其中启用审计日志记录.使用 InterceptorBinding 和 Interceptor,我可以捕获所有相关的 API 调用.

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.

我想做的是将这些审计调用写入单独的审计日志文件.我尝试使用 logback 实现这一点,并借助 this stackoverflow question 我终于做到了.第一个回复,即禁用系统日志记录,不起作用.然而,虽然此解决方案成功地将我的审计跟踪写入单独的文件,但所有其他日志记录停止写入其默认文件,仅输出到控制台.

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.

我想要实现的是将所有常规日志记录写入常规文件(即 server.log),因为它默认情况下,但在单独的文件中拥有我自己的自定义审核日志消息(也每天滚动)基础上,将旧文件重命名为写入日期).

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).

无论是使用 Logback、log4j、Wildfly 自己的日志系统还是 Wildfly CLI 审计日志来完成,只要达到目的,并且开销最小就无关紧要.在这个阶段,我正在考虑使用一个简单的输出流将其写入我自己的文件中,但如果有解决方案可以更有效地执行此操作,那感觉是多余的.

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.

这是我的 logback 文件的样子:

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>

推荐答案

我终于通过修改 Wildfly 中的 standalone.xml 文件实现了我想要的.我添加了一个使用这个文件处理程序的自定义文件处理程序和记录器.不需要自定义 logback 实现或类似的东西.

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>

这篇关于如何将应用程序审计记录到 Wildfly 8 上的单独文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆